问题
When I use the -dontobfuscate
option in my existing android app with a single React Native screen, release build works fine.
(I had to also set react native configurations for pro-guard from https://github.com/facebook/react-native/blob/master/local-cli/generator-android/templates/src/app/proguard-rules.pro)
However, I want to obfuscate my existing app and ignore react-native obfuscation only (since it's not supported as per : https://github.com/facebook/react-native/issues/7530)
After commenting out -dontobfuscate
, I got these errors:
Warning: com.facebook.react.views.recyclerview.NotAnimatedItemAnimator: can't find referenced method 'void dispatchRemoveStarting(android.support.v7.widget.RecyclerView$ViewHolder)' in program class com.facebook.react.views.recyclerview.NotAnimatedItemAnimator
Warning: com.facebook.react.views.recyclerview.NotAnimatedItemAnimator: can't find referenced method 'void dispatchRemoveFinished(android.support.v7.widget.RecyclerView$ViewHolder)' in program class com.facebook.react.views.recyclerview.NotAnimatedItemAnimator
...
I tried below configurations and NONE of them work. They either throw warnings during proguard phase of assembleRelease
or once built, throws exception on first launch of react native screen.
First Config Try:
-keep class com.facebook.react.** { public protected private *; }
Second Config Try:
-dontwarn android.support.v7.** -keep class android.support.v7.** { *; } -keep interface android.support.v7.** { *; }
Third config try:
-keep class android.support.v7.internal.** { *; } -keep interface android.support.v7.internal.** { *; }
Fourth config Try:
support-v7 -dontwarn android.support.v7.** -keep class android.support.v7.internal.** { *; } -keep interface android.support.v7.internal.** { *; } -keep class android.support.v7.** { *; }
Exceptions like:
Caused by: java.lang.IllegalAccessError: Method 'void android.support.v4.net.ConnectivityManagerCompat.<init>()' is inaccessible to class 'com.facebook.react.modules.netinfo.NetInfoModule' (declaration of 'com.facebook.react.modules.netinfo.NetInfoModule' appears in /data/app/com.sampleapp-1/base.apk)
at com.facebook.react.modules.netinfo.NetInfoModule.<init>(NetInfoModule.java:55)
Anyone with working release apk (Existing android app) with react-native, can you please share your proguard configurations?
回答1:
Here is a working configuration to make things work with ReactNative 0.27.2 version. (Takes care of native modules).
Most of the samples and the react-native init projects have the react native settings as listed below. 2 Changes are - 1. remove dontobfuscate and 2. -keep class com.facebook.** { *; }
#-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>; }
-keep class com.facebook.** { *; }
-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.**
回答2:
You should try Bg+ Anti Decompiler/Obfuscator It's can obfuscate your project: effective & easy (UI setting, not command line config). It supports:
- Hide string value (helpful when you keep some sensitive info in java source, ex: "Hello world" ->ߤª )
- Obfuscate with unicode characters (file-names, main-active class, class, functions, variable,... )
- Hide packagename
- Add fakecode to trap the decompiler tools
- Check resource-string (helpful when someone try to edit the resources of your APK)
来源:https://stackoverflow.com/questions/38400620/any-working-proguard-configuration-for-android-app-with-react-native-with-obfusc