Skip to content

fix(android): switch on double fails to compile β€” cast to intΒ #341

@FerRiv3ra

Description

@FerRiv3ra

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch react-native-zip-archive@7.1.0 for the project I'm working on.

Problem

getCompressionLevel(double compressionLevel) uses a switch statement with a double selector, which is a compile error in Java. The Java language only supports switch on int, char, byte, short, String, and enums β€” not double.

This causes the following build failure with newer JDK/AGP versions (related to #340):

error: constant label of type int is not compatible with switch selector type double
    case -1:
         ^
... (12 errors total)

Fix

Cast compressionLevel to int before the switch. This is safe because all valid values (-1, 0–9) are whole numbers passed from JS as number.

Here is the diff that solved my problem:

diff --git a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java
index 14bd68f..cccbd99 100644
--- a/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java
+++ b/android/src/main/java/com/rnziparchive/RNZipArchiveModule.java
@@ -470,7 +470,7 @@ public class RNZipArchiveModule extends ReactContextBaseJavaModule {
   }
 
   private static CompressionLevel getCompressionLevel(double compressionLevel) {
-    switch (compressionLevel) {
+    switch ((int) compressionLevel) {
       case -1:
         return CompressionLevel.NORMAL;
       case 0:

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions