I am trying to add custom Proguard rules for my app, but I am not sure into which file I should add those custom rules. I know of three files
1)Android/sdk/tools/proguard/proguard-android.txt
2)Android/sdk/tools/proguard/proguard-project.txt
3)Project --> app --> proguard-rules.pro
First of all you will use the
3)Project --> app --> proguard-rules.pro
and
few generic custom rules which are safe to apply on app and wouldn't
hinder the functioning of it
depends on the code in your app but if you will set minifyEnabled to true(which is recommended) this could lead to a lot of issues which can be solved through proguard rules file.
This is my proguard file for one of my projects where I'm using Fabric, httpApacheClient, shareActionProvider and some other libraries that needs specific rules for proguard.
# This will ignore warnings for missing translations and some other wanrinings
-ignorewarnings
# Keeping line numbers for easy error tracking :
-keepattributes SourceFile,LineNumberTable
# Support ShareActionProvider will not work without this line :
-keep class android.support.v7.widget.ShareActionProvider { *; }
# Flurry Crashlytics
-keep class com.flurry.** { *; }
-dontwarn com.flurry.**
-keepattributes *Annotation*,EnclosingMethod
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int); }
# Preserve Flurry mediation classes for DFP/AdMob Ads keep public class com.google.ads.mediation.flurry.**
# Google Play Services library
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents(); }
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL; }
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
#com.google.android.gms.common.annotation.KeepName *; }
-keepnames class * implements android.os.Parcelable { public static final ** CREATOR;
}
-keepattributes InnerClasses, EnclosingMethod
-keep class com.ironsource.mobilcore.**{ *; }
-dontwarn org.apache.http.**
-dontwarn android.net.http.AndroidHttpClient
-dontwarn android.app.Notification
-keep class com.dianxinos.DXStatService.stat.TokenManager {
public static java.lang.String getToken(android.content.Context);
}
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.app.Activity
-keep public class * extends android.content.ContentProvider
And almost you will find related proguard settings in the documentation of any external library you use.
in
3)Project --> app --> proguard-rules.pro
There are not generic proguard rules. All those generic ones already included with SDK, It depends on what features and libraries are you using in your project. Also there is no safeway of doing it. You have to apply rules and test app -> fix any issues then repeat same process.
Related
This error is making me sick, when ever I get this error I have to build-clean my project for like 5-8 times and after that my app runs else it does not. I have been facing this issue after I used calligraphy for custom fonts. If there is any other library better than this then please recommend I am just tired of this error.
I have enabled mulltidex in my application
Proguard file for reference.
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.
-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);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator 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.**
# Understand the #Keep support annotation.
-keep class android.support.annotation.Keep
-keep #android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
#android.support.annotation.Keep <init>(...);
}
-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**
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've been struggling with this one for a while now. I'm trying to Generate Signed APK in IntelliJ IDEA 13.1.3 and Run ProGuard 4.7 on the APK but I can't get rid of the following error:
Error: ProGuard: [project_name] Error: Can't read [C:\Program Files (x86)\Android\android-sdk\tools\proguard\libs] (No such file or directory)
The error message is preceded by this message:
Information: ProGuard: [project_name] Reading library directory [C:\Program Files (x86)\Android\android-sdk\tools\proguard\libs]
There is only a lib directory in that location containing proguard.jar and the two other jar files. I created an empty libs directory but got even more errors after that.
Otherwise the obfuscation seems to work properly, and without the obfuscation the signed APK is generated correctly.
The Android SDK Build-tools version is 20.
This is the proguard-project file:
-libraryjars libs
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class * extends java.util.ListResourceBundle
{
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable
{
public static final *** NULL;
}
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class *
{
#com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable
{
public static final ** CREATOR;
}
UPDATE
It seems that this problem boils down to the line -libraryjars libs in the proguard-project file. I managed to properly Generate Signed APK with a simple test project when I removed that line from proguard-project.txt. However, when -libraryjars libs wasn't removed, the obfuscation failed.
If that line is stripped when trying to run ProGuard on the actual project, it gives even more errors.
Thanks for your answer, Eric.
Actually, I already solved the problem. Just hadn't updated the answer here until now.
Yes, -libraryjars in proguard-project.txt was the root of all evil. I just included it there because of some example configurations I came across.
Here's the properly working configuration:
-keep class * extends java.util.ListResourceBundle
{
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable
{
public static final *** NULL;
}
-keepnames #com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class *
{
#com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable
{
public static final ** CREATOR;
}
-keep public class com.google.android.gms.ads.**
{
public *;
}
-keep public class com.google.ads.**
{
public *;
}
-keep class com.android.vending.billing.**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-dontwarn com.google.android.gms.**
You should have -libraryjars or -injars in your configuration. The Android build process (Ant, Eclipse, Gradle, and presumably IntelliJ IDEA as well) internally already specifies those options for you.
I haven't come across the Android SDK adding -libraryjars to proguard-project.txt (and specified paths are relative to the directory of the configuration file itself, not to .../tools/proguard), but maybe you should update to the latest version of the SDK to avoid the problem.
I'm trying to configure Proguard, but I can't manage to get it working.
This is the error:
I've tried things like:
-keep class com.android.auth.TwitterHandle.** { *; }
-keep class oauth.** { *; }
Without any luck.
Anyways, I don't really think ignoring is the answer. Because that might mean something is broken.
Any tips?
Thanks!
The warnings indicate that the AndroidQuery library depends on the OAuth library. Apparently, you're using the former library in your project, but the latter library is missing. You could add the missing library, but if your application is working fine without it in debug mode, you can just tell ProGuard to ignore the missing dependency. In this case:
-dontwarn com.androidquery.auth.**
or, to the same effect:
-dontwarn oauth.signpost.**
See the ProGuard manual > Troubleshooting > Warning: can't find referenced class
(I am the developer of ProGuard)
Add these lines to your proguard file.
-dontwarn oauth.**
-dontwarn com.android.auth.TwitterHandle.**
-keep class oauth.** { *; }
-keep class com.android.auth.TwitterHandle.** { *; }
Edit:
Anyways, I don't really think ignoring is the answer. Because that
might mean something is broken.
If you want to use Proguard and you are having some errors such as class not found, then you must disable/ignore their obfuscation. Because Proguard renames names, fields and methods of classes while obfuscating. This becomes a big problem if reflection is used for that classes. So you have to say proguard to ignore(not obfuscate) some classes to prevent this problem.
Please try this post https://stackoverflow.com/a/15477898/1665964
Insert to skip obfuscated in ProGuard your libs, jars, Classes and Subclasses with this example
-optimizationpasses 5
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-repackageclasses ''
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.MapActivity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-libraryjars libs/commons-io-2.2.jar
-libraryjars libs/gson-2.2.2.jar
-keep public class org.apache.commons.io.**
-keep public class com.google.gson.**
-keep public class com.google.gson.** {public private protected *;}
##---------------Begin: proguard configuration for Gson ----------
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.mypackage.ActivityMonitor.ClassMultiPoints.** { *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints { public protected *; }
-keep public class com.mypackage.ActivityMonitor$ClassMultiPoints$ClassPoints$ClassPoint { public protected *; }
##---------------End: proguard configuration for Gson ----------
The reason for error must be that you might be using an external jar file (in libs folder).
If this is the case, adding the following line before -keep class ... lines should solve your problem.
-libraryjars libs/<jar_filename>
If u are unfamiliar with proguard cmd tool then you can try proguard gui located at tools/proguard/lib/proguardgui of your android SDK folder.
open proguardgui and load your configuration file.
then in Input / output section add your input and output jar as well as add your android lib used in your project.
***** your main problem is that you havent added your library (Aquery.jar and others) jar that is used in your project , so that proguard can't find the required Classes.***
Application crashes after proguard (proguard generated by eclipse IDE).
logcat stacktrace
W/SupportMenuInflater(13657): Cannot instantiate class: android.support.v7.widget.ShareActionProvider
W/SupportMenuInflater(13657): java.lang.ClassNotFoundException: android.support.v7.widget.ShareActionProvider
W/SupportMenuInflater(13657): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(13657): FATAL EXCEPTION: main
E/AndroidRuntime(13657): java.lang.NullPointerException
E/AndroidRuntime(13657): at com.mypack.app.MainActivity.onCreateOptionsMenu(Unknown Source)
E/AndroidRuntime(13657): at android.app.Activity.onCreatePanelMenu(Activity.java:2571)`
default version of proguard.cfg file
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.
-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#-keep public class android.support.v7.widget.ShareActionProvider
# 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.**
`
EDIT: to resolve the problem you need to add this lines to proguard.cfg and have no errors. New lines tells proguard to ignore android.support libraries.
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
In your proguard config, you have commented out the keep of the class that it can't find:
#-keep public class android.support.v7.widget.ShareActionProvider
Did you try uncommenting that and building again?
EDIT: since that didn't solve your problem, maybe try the catch all described here:
Android Proguard configuration for the v7 Support Library ActionBar
specifically, try adding:
-keep class android.support.v7.internal.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
You can then make it more specific if that works, because this obviously just keeps all the support library classes, even ones you don't use.