I want to use Proguard mainly for obfuscation reasons.
My problem is that I have three libraries, Twitter4J and two signpost libraries. These libraries caused errors when I tried to create an signed APK. To get over this I put the following in the proguard.config file...
-dontwarn org.apache.commons.codec.binary.**
-dontwarn org.slf4j.**
-dontwarn com.sun.syndication.io.**
-dontwarn com.sun.syndication.feed.synd.*
While this got rid of the errors in the console, when i loaded my signed APK onto my mobile phone it instantly crashed. The DDMS said this was due to a class not found in Twitter4J.
Getting rid of the "dontwarns" above did not help. Neither did adding dontshrink dontoptimise.
I would like Proguard to completely ignore the libraries (as they are open source anyway). Is this possible?
Try this:
-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }
Cf post from #CaspNZ:
Android Proguard with external jar
You should be able to add to the proguard.cfg the following lines to exclude all classes within a package (and subpackages)
-keep class org.apache.commons.codec.binary.**
-keep interface org.apache.commons.codec.binary.**
-keep enum org.apache.commons.codec.binary.**
-keep class org.slf4j.**
-keep interface org.slf4j.**
-keep enum org.slf4j.**
-keep class com.sun.syndication.io.**
-keep interface com.sun.syndication.io.**
-keep enum com.sun.syndication.io.**
-keep class com.sun.syndication.feed.synd.**
-keep interface com.sun.syndication.feed.synd.**
-keep enum com.sun.syndication.feed.synd.**
I'd like to add that you should sync your project with Gradle files after adding proguard rules, otherwise they may not work.
Related
I know there is an answer on another thread but I feel it is unrelated.
(Or it might be, but I'd appreciate a more elaborate answer)
So I have these in my proguard.rules:
-dontwarn com.google.android.instantapps.supervisor.InstantAppsRuntime
-keep class com.google.android.instantapps.supervisor.InstantAppsRuntime
-dontwarn com.google.android.gms.ads.AdView
-keep class com.google.android.gms.ads.AdView
-dontwarn com.google.android.gms.tagmanager.TagManagerService
-keep class com.google.android.gms.tagmanager.TagManagerService
-dontwarn io.grpc.netty.NettyChannelProvider
-keep class io.grpc.netty.NettyChannelProvider
-dontwarn io.opencensus.impl.tags.TagsComponentImpl
-keep class io.opencensus.impl.tags.TagsComponentImpl
-dontwarn io.opencensus.impllite.tags.TagsComponentImplLite
-keep class io.opencensus.impllite.tags.TagsComponentImplLite
-dontwarn io.opencensus.impl.stats.StatsComponentImpl
-keep class io.opencensus.impl.stats.StatsComponentImpl
-dontwarn io.opencensus.impllite.stats.StatsComponentImplLite
-keep class io.opencensus.impllite.stats.StatsComponentImplLite
But when compiling the app, I still get these warnings for all of them:
W/ProGuard: The class 'io.opencensus.stats.Stats' is calling Class.forName to retrieve
the class 'io.opencensus.impllite.stats.StatsComponentImplLite', but the latter could not be found.
It may have been obfuscated or shrunk.
You should consider preserving the class with its original name,
with a setting like:
-keep class io.opencensus.impllite.stats.StatsComponentImplLite
(that is an example)
Why is that happening? Is ProGuard actually keep the classes or did it not process by proguard.rules?
It is probably because you are missing the opencensus library dependency from your project.
Add below line to your gradle dependencies, e.g.
dependencies {
implementation 'io.opencensus:opencensus-api:0.18.0'
}
I use ProGuard and Joda Time in my Android application (of course, I use a version optimized for Android: https://github.com/dlew/joda-time-android). I ran into the problem that with optimization and obfuscation turned on, I get an error when I try to use PeriodFormat:
java.util.MissingResourceException: can't find resource for bundle java.util.PropertyResourceBundle, key PeriodFormat.space
I compared the contents of the APK when building with and without proguard and saw that the org/joda/time/format directory in the non-optimized version contains 15 properties files, and only 9 in the optimized version. In these files I saw the PeriodFormat.space keys, so leaves no doubt that the exception caused by the loss of these files.
How to configure proguard so that the contents of this directory are transferred to the optimized APK without any optimizations?
I already use the following rules for Joda Time:
-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.** { *; }
-keep interface org.joda.** { *; }
-keepdirectories org.joda.**
However, they are not enough, because the files still disappear.
java.util.MissingResourceException: can't find resource for bundle
java.util.PropertyResourceBundle, key PeriodFormat.space
Don't
-keep class org.joda.** { *; }
-keep interface org.joda.** { *; }
Do
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
Your proguard will be
-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
Read -keep options.
I am using IBM's mobilefirst worklight version 6.3 for push notification. Everything works fine when i dont apply proguard.
When i apply proguard and run the build while subscribing for the push notification only i get the following exception.
java.lang.RuntimeException: Failed to find the icon resource. Add the icon file under the /res/drawable folder.
I have the push.png named file in the drawable folder.
Any suggestions on how to handle that on the proguard or is it a worklights bug?
had the same issue with another third party library but it was resolved when i added keep class com.classname.** {*;} i did the same for worklight as well -keep class com.worklight.** {*;} but it is of no use.
below is the proguard configuration that i have used
-keepclassmembers class * {
#android.webkit.JavascriptInterface <methods>;
}
-keep class com.google.gson.Gson
-keep class com.billdesk.** {*;}
-keep public class com.worklight.** {*;}
-dontwarn com.worklight.**
-dontwarn com.auth0.jwt.**
-dontwarn com.squareup.picasso.**
-dontwarn com.viewpagerindicator.**
-dontwarn org.bouncycastle.**
MobileFirst 6.3 does not officially support obfuscation using Proguard.
Even so, an Android project obfuscated using Proguard works fine without issues in most cases.
I am not able to recreate the issue you mention.I tested the MFP 6.3 Eventsource notifications sample after obfuscating with Proguard and the application worked fine. No runtime exceptions were seen.
Android SDK Tools : 25.1.1
Target API Level : 19
Proguard version : 4.7
To begin with:
Ensure that push.png is present in all drawable folders and not just the generic one.
Check the proguard obfusction logs to see if "push.png" is being crunched in all folders and look for error messages.
Modify the proguard configuration to contain-
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepattributes InnerClasses
-keep class **.R
-keep class **.R$* {
<fields>;
}
-keep class org.apache.cordova.** { *; }
-keep public class * extends org.apache.cordova.CordovaPlugin
-keep class com.worklight.androidgap.push.** { *; }
-keep class com.worklight.wlclient.push.** { *; }
-keep class com.worklight.common.security.AppAuthenticityToken { *; }
-keep class com.google.** { *;}
-dontwarn com.google.common.**
-dontwarn com.google.ads.**
-dontwarn com.worklight.androidgap.push.GCMIntentService
-dontwarn com.worklight.androidgap.plugin.WLInitializationPlugin
-dontwarn com.worklight.wlclient.push.GCMIntentService
-dontwarn org.bouncycastle.**
-dontwarn com.worklight.nativeandroid.common.WLUtils
-dontwarn com.worklight.wlclient.push.WLBroadcastReceiver
-dontwarn com.worklight.wlclient.push.common.*
-dontwarn com.worklight.wlclient.api.WLPush
I try using Proguard in android studio, but seems like Proguard is not obfuscating the class name, for example, my app structure, and the config:
and config
but when i try trigger the exception in the app:
the exception is listed in ADB console:
only the methods are obfuscated, the MainActivity.class is not
This is an expected behaviour because the class is an activity!
All classes that are mentioned in AndroidManifest.xml have to keep their names (activities, services, providers, receivers, application, instrumentation). Otherwise the system won't be able to find them.
Gradle build automatically generates some rules for your ProGuard configuration to achieve this. It scans AndroidManifest.xml and adds rules for each class found there.
If you want to see all the rules that are used, add this line to your ProGuard rules:
-printconfiguration "build/outputs/mapping/configuration.txt"
It will create configuration.txt file containing all the rules.
There should be something like this:
# view AndroidManifest.xml #generated:50
-keep class com.github.browep.proguard.MainActivity {
<init>(...);
}
I was facing the same problems,
After updating my Android plugin for Gradle, Proguard stop obfuscating my utility and other class files.
After few searching, I found that Android studio gradle now uses newer version of Proguard.
And according to this stack-overflow answer, which stated that:
proguard automatically add rules specific for android/google package.
Therefore, After few rule changes in my app, Proguard obfuscated the class names again.
Old proguard-rules.pro:
#support-v4
##link https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
#support-v7
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
#https://stackoverflow.com/a/34895791/4754141
-keep class !android.support.v7.view.menu.**
-keep interface android.support.v7.* { *; }
#support design
##link https://stackoverflow.com/a/31028536
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
#error : Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
#solution : #link https://stackoverflow.com/a/14463528
-dontnote com.google.vending.licensing.ILicensingService
-dontnote **ILicensingService
#updating to Gradle 2.14.1 caused error : https://stackoverflow.com/q/17141832/4754141
-keepattributes EnclosingMethod
#render script
##link https://stackoverflow.com/questions/22161832/renderscript-support-library-crashes-on-x86-devices
-keepclasseswithmembernames class * { native <methods>; }
-keep class android.support.v8.renderscript.** { *; }
New proguard-rules.pro:
#https://stackoverflow.com/a/41901653/4754141
#https://stackoverflow.com/a/23840049/4754141
-keep class android.support.** { *; }
-keep interface android.support.** { *; }
I have an Android app which uses Google Volley as my download broker. I just tried to use ProGuard to obfuscate the code, and find out the volley download starts failing at runtime.
Here's my ProGuard config:
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keep class com.android.volley.** { *; }
-keep interface com.android.volley.** { *; }
-keepattributes *Annotation*
-dontwarn org.apache.**
and here is the error I saw in the code:
Async download FAILED. Exception message: The chosen LogFactory implementation does not extend LogFactory. Please check your configuration. (Caused by java.lang.ClassCastException: The application has specified that a custom LogFactory implementation should be used but Class 'org.apache.commons.logging.impl.LogFactoryImpl' cannot be converted to 'a.a.a.b.c'. Please check the custom implementation. Help can be found #http://commons.apache.org/logging/troubleshooting.html.)
I was wondering if I did some proguard config caused some dependency problem. Please help out.
The Apache logging library uses some reflection on its log factories. Keeping their names should be sufficient:
-keep class org.apache.commons.logging.**
Side-note on your configuration: -keep class ..... always implies -keep interface ....., so you can leave out the latter.