realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.79k stars 577 forks source link

App crashing with proguard enabled in react-native 0.57.X - couldn't find DSO to load: librealmreact.so #1960

Closed ferrannp closed 5 years ago

ferrannp commented 6 years ago

Goals

Trying to build a production ready apk (Android).

Actual Results

App crashes if proguard is enabled (def enableProguardInReleaseBuilds = true).

    java.lang.UnsatisfiedLinkError: couldn't find DSO to load: librealmreact.so
        at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:522)
        at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:420)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:370)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:335)
        at io.realm.react.RealmReactModule.<clinit>(RealmReactModule.java:59)

It crashes no matter if you split in several apks or only one.

This is my proguard config:

# Disabling obfuscation is useful if you collect stack traces from production crashes
# (unless you are using a system that supports de-obfuscate the stack traces).
-dontobfuscate

# React Native

# Keep our interfaces so they can be used by other ProGuard rules.
# See http://sourceforge.net/p/proguard/bugs/466/
-keep,allowobfuscation @interface com.facebook.proguard.annotations.DoNotStrip
-keep,allowobfuscation @interface com.facebook.proguard.annotations.KeepGettersAndSetters
-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.proguard.annotations.DoNotStrip class *
-keep @com.facebook.common.internal.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.proguard.annotations.DoNotStrip *;
    @com.facebook.common.internal.DoNotStrip *;
}

-keepclassmembers @com.facebook.proguard.annotations.KeepGettersAndSetters class * {
  void set*(***);
  *** get*();
}

-keep class * extends com.facebook.react.bridge.JavaScriptModule { *; }
-keep class * extends com.facebook.react.bridge.NativeModule { *; }
-keepclassmembers,includedescriptorclasses class * { native <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.UIProp <fields>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactProp <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactPropGroup <methods>; }

-dontwarn com.facebook.react.**

# okhttp

-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

# okio

-keep class sun.misc.Unsafe { *; }
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn okio.**

Version of Realm and Tooling

kneth commented 6 years ago

Please try to unzip your apk and see what .so it contains.

I have tried to enable Proguard on a small app, and the .so is added correctly.

OleksandrKucherenko commented 6 years ago

in soLoader lib changed api method signature loadLibrary():bool instead of expected loadLibrary():void... soLoader highest compatible lib version is 0.4.+

Solution is downgrade the Fresco and soLoader libs versions. 10.9.+ and 0.4.+

kneth commented 6 years ago

@OleksandrKucherenko Thanks for the suggestions.

serjiogunit commented 6 years ago

@OleksandrKucherenko hello. I have these versions

compile 'com.facebook.fresco:fresco:1.9.0'
compile 'com.facebook.soloader:soloader:0.3.0'

android/app/build.gradle

android {
...
  defaultConfig {
  ...
     ndk {
        abiFilters "armeabi-v7a", "x86"
     }
  ...
  }
  splits {
    abi {  
       ...
       include "armeabi-v7a", "x86"
       ...
     }
  }
...
}

apk contains 2 files librealmreact.so in lib/armeabi-v7a and lib/x86

but it doesn't help to solve issue

09-17 10:10:09.619 10283 10304 E AndroidRuntime: java.lang.UnsatisfiedLinkError: couldn't find DSO to load: librealmreact.so
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:522)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:420)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:370)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:335)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at io.realm.react.RealmReactModule.<clinit>(RealmReactModule.java:59)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at io.realm.react.RealmReactPackage.createNativeModules(RealmReactPackage.java:31)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.NativeModuleRegistryBuilder.processPackage(NativeModuleRegistryBuilder.java:109)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.processPackage(ReactInstanceManager.java:1175)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.processPackages(ReactInstanceManager.java:1145)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1086)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:116)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:916)
09-17 10:10:09.619 10283 10304 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:760)
OleksandrKucherenko commented 6 years ago

2 @serjiogunit : it sounds like librealmreact.so library is not a part of the final android binary.

Just use Android Studio feature ' analyze APK..." and check that inside the final binary librealmreact.so file exists. Also, analyze how proguard rename the classes, are they included into first DEX file or into second/third one.

next step is to validate which ABI used on the device... x64 vs x86, etc.

next step is to try to follow - https://github.com/facebook/fresco/issues/2049#issuecomment-375257032

ferrannp commented 5 years ago

I still have same issue with "react-native": "^0.57.7" and "realm": "^2.21.0",. @kneth I unzipped the .apk, it seems the librealmreact.so is there:

lib/armeabi-v7a/librealmreact.so

(there is only armeabi-v7ain lib folder)

But the error says:

2018-12-21 18:15:25.050 7434-7449/? E/SoLoader: couldn't find DSO to load: librealmreact.so caused by: Error: Cannot load /data/app/com.my.package-0uWDsr7mBpTMJ4J4ENo9cw==/lib/arm/librealmreact.so

Can be wrong path somehow? Any ideas?

8BallBomBom commented 5 years ago

Also experiencing the same issue with Realm 2.21.1 and React Native 0.57.8 when compiling with proguard.

ferrannp commented 5 years ago

I wonder if somebody tried this with R8 (that should deprecate proguard) ?

New code shrinker

R8 is a new tool for code shrinking and obfuscation that replaces ProGuard. You can start using the preview version of R8 by including the following in your project’s gradle.properties file:

android.enableR8 = true

See https://developer.android.com/studio/releases/?utm_source=android-studio&utm_medium=studio-assistant#R8

8BallBomBom commented 5 years ago

I'll have a lookie, see if anything changes. Just annoying really. Having to keep Realm at 2.2.20 as anything i have tried so far above that version gives this exact issue.

cmelchior commented 5 years ago

@ferrannp It says /lib/arm/librealmreact.so instead of arm-v7a. What Android device are you trying to run APK on?

cmelchior commented 5 years ago

Another suggestion might be that it could be related to App Bundles if you are shipping your app using that? Realm Java had a bug that could look similar to what you are seeing: https://github.com/realm/realm-java/issues/5977

8BallBomBom commented 5 years ago

Pretty much got the app to split with these filters -> "armeabi-v7a", "x86" Not currently using the new App Bundles feature. Also running tests on a Samsung Galaxy s7 Edge and Galaxy s5.

ferrannp commented 5 years ago

Yes I got the two splits two:

app-armeabi-v7a-release.apk
app-x86-release.apk

Without proguard is fine. It does not matter if you split or not, the resulted .apk crashes.

8BallBomBom commented 5 years ago

Would be nice to see a fix for this as we can't upgrade without disabling proguard. @kneth Any resolution?

kneth commented 5 years ago

It might be a good time to switch to ReLinker.

adyanced commented 5 years ago

@ferrannp Same issue here. Any solutions? What I wondered is why android load librealmreact.so from "xxx/lib/arm/" but not "xxx/lib/armeabi-v7a"?

adyanced commented 5 years ago

Hi, I add "-keep class io.realm.react.**" into the file proguard-rules.pro. Now it runs well

8BallBomBom commented 5 years ago

Sadly that doesn't help :/ Have had that in my proguard rules file for a long while.

adyanced commented 5 years ago

@8BallBomBom Below is my version of realm and tools Realm: 2.22.0 React Native: 0.57.2 React: 16.5.1 And build environment: compileSdkVersion = 27 buildToolsVersion = "27.0.3" minSdkVersion = 19 targetSdkVersion = 26 supportLibVersion = "27.1.1" Hope this will be helpful

8BallBomBom commented 5 years ago

Very close to what i'm running. Thanks, will have a mess :)

ferrannp commented 5 years ago

So I updated realm to 2.22.0 and the error seems to be gone. This is what I have now:

"react-native": "0.57.8",
"realm": "^2.22.0",

And I only had to add one line to my proguard-rules.pro:

-keep class io.realm.react.util.SSLHelper

@adyanced your .apk will be smaller just with that line instead of the whole realm.react.

@kneth did anything change that this is fixed? Anyway we can avoid extra line in proguard file? (or should be documented or added to the realm proguard itself?).

kneth commented 5 years ago

@ferrannp I think #2177 might have helped.

@cmelchior Do you know if we can get rid of that proguard line?

mnlbox commented 5 years ago

Any update on this? I can't build my application :disappointed:

reachtokish commented 5 years ago

Any update on this ? Getting the following error and app is crashing immediately after opening. Happening only for arm64-v8a apk

java.lang.UnsatisfiedLinkError: couldn't find DSO to load: librealmreact.so at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:703) at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:564) at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:500) at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:455) at io.realm.react.RealmReactModule.(RealmReactModule.java:56) at io.realm.react.RealmReactPackage.createNativeModules(RealmReactPackage.java:31) at com.facebook.react.ReactPackageHelper.getNativeModuleIterator(ReactPackageHelper.java:42) at com.facebook.react.NativeModuleRegistryBuilder.processPackage(NativeModuleRegistryBuilder.java:40) at com.facebook.react.ReactInstanceManager.processPackage(ReactInstanceManager.java:1212) at com.facebook.react.ReactInstanceManager.processPackages(ReactInstanceManager.java:1182) at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1120) at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:123) at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:943) at java.lang.Thread.run(Thread.java:764)

kneth commented 5 years ago

@reachtokish We currently don't support 64 bit. You need to include 32 bit only. See for example https://github.com/realm/realm-js/issues/2221#issuecomment-482131628

AppMedecine commented 5 years ago

react-native : "0.59.3" Adding : ndk { abiFilters "armeabi-v7a", "x86" } To build.gradle defaultConfig fixed it for me.

ferrannp commented 5 years ago

I will close this issue because it is creating confusion. Realm works with proguard (see comments), what it does not work is 64bits yet.

8BallBomBom commented 5 years ago

Sorry i didn't get back but yes the issue i had was resolved by upgrading React Native and running clean builds.

seth-shi commented 4 years ago

So I updated realm to 2.22.0 and the error seems to be gone. This is what I have now:

"react-native": "0.57.8",
"realm": "^2.22.0",

And I only had to add one line to my proguard-rules.pro:

-keep class io.realm.react.util.SSLHelper

@adyanced your .apk will be smaller just with that line instead of the whole realm.react.

@kneth did anything change that this is fixed? Anyway we can avoid extra line in proguard file? (or should be documented or added to the realm proguard itself?).

If onlyenableProguardInReleaseBuilds = true, then this setting is effective, it solved my problem -keep class io.realm.react.util.SSLHelper

saracasticshailz commented 10 months ago

Simplest way to find the issue is, create an apk with debuggable enabled & run it over device while connected to android studio & check logcat, in there you will see which package/lib exactly you need to add in -keep.