I'm facing some issues when compiling the release version of my app.
I'm currently using Room and I have my model classes are under:
com.example.room.entity
and in order to let the app working I'm using a proguard rule
-keepattributes *Annotation*,SourceFile,LineNumberTable
-keep class com.example.room.entity.**.* { *; }
Once I build the version and I execute on my phone the app crash and I can see
Caused by java.lang.ClassNotFoundException
com.roommate.example.room.entity.UserEntity
The output of -printusage shows me that the the class I'm trying to preserve is somewhat touched by R8.
What can I do to enforce my rules?
Try to change second line to:
-keep class com.roommate.example.room.entity.** { *; }
Related
When I update to Android Studio 4.0 proguard-rules it shows warning Unresolved class name. Below is example but I sure it still waring the exist class in my project.
-keep class com.squareup.haha.** { *; }
-keep class com.squareup.leakcanary.** { *; }
If I change from ** to * this warning is gone.
-keep class com.squareup.haha.* { *; }
-keep class com.squareup.leakcanary.* { *; }
Does anyone get this? Should I ignore this warning or it's bug of Android Studio 4.0?
Update
I find it's bug, it already assigned but not resolved https://issuetracker.google.com/issues/153616200
Update July
Google team already analysis and increase priory of this bug, it may be related to a newer version of R8.
Update August
Fixed in AS 4.2 Canary 9
Check this issue: https://issuetracker.google.com/issues/147802433
`If you right-click on error, there is option "suppress for statement", after that AS adds a comment such as:
noinspection ShrinkerUnresolvedReference
-keep class not.existing
And with this comment there is no error for "not.existing".`
Replace .** with ** as workaround. No compile error and classes are kept.
-keep class com.squareup.haha** { *; }
-keep class com.squareup.leakcanary** { *; }
This issue is fixed in Android Studio 4.2 Canary 8. Please find the release notes here https://androidstudio.googleblog.com/2020/08/android-studio-42-canary-8-available.html and check for this issue id #153616200
A folder and its subfile. I have tested this:
com.xx.xx.* { *; }
I guess that it may contain multiply folders and subfile. I've not tested this:
com.xx.xx.**.* {*;}
All of these answers are mere non-sense because:
LeakCanary is being added as debugImplementation.
it makes no sense to have rules for classes which do not exist in release builds.
it makes even less sense to obfuscate debug builds.
In case you may require any of these rules, you've added it to the wrong build-type.
replace
.**
with
.*.*
result
-keep class com.squareup.haha.*.* { *; }
-keep class com.squareup.leakcanary.*.* { *; }
I have an Android App that works well in debug mode, but that has some kind of memory leak in release mode. Basically, it is really slow and the following line appears repeatedly in logcat:
dalvikvm D WAIT_FOR_CONCURRENT_GC blocked 91ms
If I don't obfuscate, don't shrink and don't optimize in proguard, then the release works. If I do one of them, I have this error. I use the following lines:
-dontshrink
-dontoptimize
-dontobfuscate
I am using multiple external libraries that use some native (i.e. JNI) code, and I have no idea what could be wrong.
How can I identify where the issue comes from?
Can I tell proguard to optimize/shrink/obfuscate only one package, so that I can try to isolate the problem?
You can tell Proguard to not interfere with, I mean obfuscate, packages and subpackages using instructions like this in the proguard config file:
-keep class com.google.** { *; }
-keep class okio.** { *; }
You can also tell it to keep classes annotated with a specific annotation using:
-keep class com.your.annotation
-keep #com.your.annotation class * { *; }
The first keep instruction makes sure it doesn't remove the annotation itself, and the second keeps anything annotated with it.
The full Proguard manual with other examples can be found here.
I added Crashlytics to my android app (via Idea plugin).
When my app starts I get next error:
java.lang.AbstractMethodError: abstract method not implemented
at io.fabric.sdk.android.ActivityLifecycleManager$ActivityLifecycleCallbacksWrapper$1.onActivityStarted(ActivityLifecycleManager.java)
at android.app.Application.dispatchActivityStarted(Application.java:199)
at android.app.Activity.onStart(Activity.java:1048)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:536)
I completely can't figure out why this happens. Maybe someone faced with this issue?
As mentioned above (by Chris), proguard stripping out something it isn't able to discern as being used. And it really stripping Crashlytics/Fabric classes.
For avoid this, just need add these lines to proguard config file:
-keep class com.crashlytics.** { *; }
-keep class io.fabric.** { *; }
-dontwarn com.crashlytics.**
I've seen a couple questions regarding this issue, but they are for older versions of Netty.
I have tried their answers, switching org.jboss.netty out with io.netty, but the same error occurs.
I'm trying to compile an Android app that uses Netty 5.0.0Alpha2 (build #16) with Proguard enabled.
Without Proguard, the app runs fine.
As soon as I enable Proguard, I get this exception when it tries to use Netty:
java.lang.IllegalStateException: unknown type parameter 'I': class io.netty.channel.SimpleChannelInboundHandler
at io.netty.util.internal.TypeParameterMatcher.find0(Unknown Source)
at io.netty.util.internal.TypeParameterMatcher.find(Unknown Source)
at io.netty.channel.SimpleChannelInboundHandler.<init>(Unknown Source)
at io.netty.channel.SimpleChannelInboundHandler.<init>(Unknown Source)
...
This is my Proguard config:
# billing
-keep class com.android.vending.billing.**
# butterknife
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector {
*;
}
-keepnames class * {
#butterknife.InjectView *;
}
# admob
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}
# logging
-assumenosideeffects class android.util.Log
# netty (partial)
-dontwarn io.netty.**
-dontwarn sun.**
I have tested it without the -dontwarn options to see if the warnings would point me in the right direction, but it's all missing optional dependencies like slf4j and Tomcat.
I have also tried excluding all the Netty classes like so:
-keep class io.netty.** {
*;
}
...but that does not appear to fix it either.
I have fixed this issue with some carefully* applied Proguard rules after reading through parts of the rather huge Netty sources:
-keepattributes Signature,InnerClasses
-keepclasseswithmembers class io.netty.** {
*;
}
-keepnames class io.netty.** {
*;
}
My original exception was caused by the type variables being removed from the bytecode, which Netty uses via reflection. Signature in -keepattributes keeps this information.
You get a slightly different exception if you only do Signature on -keepattributes - adding InnerClasses fixes this one by bringing back even more information in the class files.
Later, I got java.lang.NoSuchFieldException: ctl; that's what -keepnames is for. This way, the field is still called ctl like Netty expects.
Finally, some members (like ctl, seen earlier) were being removed by Proguard because Netty only uses them via reflection. The final rule, -keepclasseswithmembers, makes sure Proguard doesn't remove them.
If you take this approach, I strongly recommend you use only the Netty jars you need, instead of the -all jar. Switching from -all to just the required Netty jars brought my method count way down after I had gone past the 65k limit. Reducing your jars requires bit of trial-and-error though as the separation is unclear and there's not really any resources saying what's what.
* not carefully at all, I just slapped rules into the file and removed them if they did nothing. There's probably a better way to do this that doesn't keep this information in the entire program, but instead just Netty.
Una's answer keep too many classes, which makes my app 1MB larger than usual. So I use the rules below:
# netty
-keepclassmembernames class io.netty.buffer.AbstractByteBufAllocator {
*;
}
-keepclassmembernames class io.netty.buffer.AdvancedLeakAwareByteBuf {
*;
}
-keep public class io.netty.util.ReferenceCountUtil {
*;
}
I'm having troubles compiling an app that uses ProGuard and Google Analytics v4. A single other thread that seems to be similar is here, except there aren't very many details.
I've tried the following to no avail:
Doing keep com.google.** { *; }
Doing a -keep android.support.** { *; } package. This error continues to crop up with every other bit of obfuscated code, as if joining the newly obfuscated code with the pre-obfuscated Google Analytics code always causes errors.
Using -keepattributes Signature in case there's an issue with generics.
Using an obfuscated dictionary of four-letter words (such as rNqp, sSlq) in case something to do with Android dexing causes the pre-obfuscated Google Analytics classes to conflict with the obfuscated project files.
Here is the exception:
11-08 22:45:01.644 2206-2226/com.example.helloworld E/GAV3﹕ Thread[GAThread,5,main]: Error on GAThread: java.lang.ClassCastException: android.net.ConnectivityManager cannot be cast to android.support.v4.d.a
at com.google.a.a.a.at.a(Unknown Source)
at com.google.a.a.a.am.a(Unknown Source)
at com.google.a.a.a.s.h(Unknown Source)
at com.google.a.a.a.s.g(Unknown Source)
at com.google.a.a.a.s.a(Unknown Source)
at com.google.a.a.a.u.run(Unknown Source)
at com.google.a.a.a.ab.run(Unknown Source)
11-08 22:45:01.644 2206-2226/com.example.helloworld E/GAV3﹕ Thread[GAThread,5,main]: Google Analytics is shutting down.
Proguard configuration file (99.9% vanilla):
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
# Optimizations: If you don't want to optimize, use the
# proguard-android.txt configuration file instead of this one, which
# turns off the optimization flags. Adding optimization introduces
# certain risks, since for example not all optimizations performed by
# ProGuard works on all versions of Dalvik. The following flags turn
# off various optimizations known to have issues, but the list may not
# be complete or up to date. (The "arithmetic" optimization can be
# used if you are only targeting Android 2.0 or later.) Make sure you
# test thoroughly if you go this route.
#-optimizations !code/simplification/cast,!field/*,!class/merging/*
#-optimizationpasses 5
#-allowaccessmodification
#-dontpreverify
# The remainder of this file is identical to the non-optimized version
# of the Proguard configuration file (except that the other file has
# flags to turn off optimization).
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
}
# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontwarn android.support.**
-keepattributes Signature
It seems like you're using a copy of proguard-android-optimize.txt (a more common approach is to refer to the original, maintained file in the Android SDK). You can try disabling optimization:
-dontoptimize
If this indeed avoids the problem, you can report the issue in ProGuard's bug tracker or to me directly.
Make sure that you are using the latest version of ProGuard (version 5.1 at this time of writing) and the latest version of the GMS library.
(I am the developer of ProGuard)
I've found the solution. It just involves not obfuscating the ConnectivityManager. I've used:
-keep android.net.** { *; }
which is working fine.