Proguard error after adding Glide v4 - android

I am not able to generate the apk with proguard enabled after adding glide v4 library.
I have added the below lines in proguard-rules
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
It's showing the below error while building.
Warning:com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool:
can't find referenced field 'android.graphics.Bitmap$Config HARDWARE'
in library class android.graphics.Bitmap$Config

It's save to ignore these warning using:
-dontwarn com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
-dontwarn com.bumptech.glide.load.resource.bitmap.Downsampler
-dontwarn com.bumptech.glide.load.resource.bitmap.HardwareConfigState
See this post

Proguard
If you use proguard, you may need to add the following lines to your proguard.cfg:
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
If you're targeting any API level less than Android API 27, also include:
-dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder
VideoDecoder uses API 27 APIs which may cause proguard warnings even though the newer APIs won’t be called on devices with older versions of Android.

I assume you take your proguard rules from this glide page but it is miswritten. Replace below line
-keep public class * extends com.bumptech.glide.AppGlideModule
with this:
-keep public class * extends com.bumptech.glide.module.AppGlideModule
as you see, module package is missing. It is also properly documented on README page. You can also check your AppGlideModule class path from External Libraries.

You can as well increase your compileSdkVersion to 26. This takes care of the new features that Glide v4 uses.

Related

Where to add Proguard Custom Rules?

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.

Proguard fails with Google Play Services library

I'm unable to build my package with Proguard enabled after updating Google Play Services library. My project minSdkVersion is 9. I am using following version of services lib:
android:versionCode="4323030"
android:versionName="4.3.23 (1069729-030)" >
The library states minSdk also 9.
And getting error:
Unexpected error while performing partial evaluation:
Class = [com/google/android/gms/common/GooglePlayServicesUtil]
Method = [showErrorDialogFragment(ILandroid/app/Activity;ILandroid/content/DialogInterface$OnCancelListener;)Z]
Exception = [java.lang.IllegalArgumentException] (Can't find any super classes of [com/google/android/gms/common/ErrorDialogFragment] (not even immediate super class [android/app/DialogFragment]))
java.lang.IllegalArgumentException: Can't find any super classes of [com/google/android/gms/common/ErrorDialogFragment] (not even immediate super class [android/app/DialogFragment])
Following related to gms is found in my proguard file:
-dontwarn com.google.android.gms.**
-keep class com.google.android.gms.** { *; }
-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;
}
DialogFragment was added in API level 11. Is this a failure with the services library or am I missing something?
Thanks.
You should build against API level 11 that contains the missing class. The library itself probably has a fallback mode for older APIs, but ProGuard still needs to process the entire application, including the newer code.

Phonegap functionality not working after eclipse apk export - proguard issue?

When I test my app on the device using Run as --> Android Application, Phonegap functionality works fine.
When I export a .apk file using Eclipse and run it on the device, Phonegap functionality is not working. I assume it's likely a Proguard issue. How do I solve this?
proguard-properties.txt
-keep public class * extends com.phonegap.api.Plugin
-keep public class * extends org.apache.cordova.api.Plugin
-keep public class org.apache.cordova.DroidGap
-keep public class org.apache.cordova.**
-libraryjars /path/to/adt-bundle-mac/sdk/tools/lib/commons-codec-1.4.jar
-dontwarn android.webkit.*
This worked:
-keep public class * extends com.phonegap.api.Plugin
-keep public class * extends org.apache.cordova.api.Plugin
-keep public class org.apache.cordova.DroidGap
-keep public class org.apache.cordova.**
-keep public class org.apache.**
-dontwarn android.webkit.*
-dontwarn org.apache.**
-keep public class * extends org.apache.cordova.api.CordovaPlugin
-keep class org.apache.cordova.** { *; }
(Phonegap 2.4 Android Proguard config)
I used :
-keep class org.apache.cordova.** { *; }
while proguarding my cordova application but it did not work. But when I wrote the same statement after '-keep' statements of all java files then it worked absolutely fine.(It might be because after keep of all cordova functions, keep of java functions overriding and excluding the cordova functions again).
But one of my project changing of order did not matter while in one it worked only after the order change i.e., by writing -keep class org.apache.cordova.** { *; } statement at the end in proguard config file.

admob ads are not shown with pro guard

I developed android app and I used admob sdk 6.2.1 for showing ads, and when I tested the app on my real device and on the emulator,ads are shown.
but when I apply pro guard to my app and test it on my device(I tested it on another devices also) ads are not shown up, any help please!
Add this to your proguard:
-keep class com.google.ads.** # Don't proguard AdMob classes
-dontwarn com.google.ads.** # Temporary workaround for v6.2.1. It gives a warning that you can ignore
EDIT 2020:
For some time now, the Google Mobile Ads SDK has distributed its own proguard rules that get manifest merged into your app, so you don't need to add anything yourselves. The merged rules also keep around methods from third-party mediation adapters that are needed to make mediation work.
From the docs:
To safely use ProGuard with Google Mobile Ads, add the following to your ProGuard config:
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}
This will prevent all public methods on public classes from being obfuscated.
If you use AdMob Mediation with other ad networks, you should keep public methods from public classes for each SDK and adapter library as well.
Proguard Rules for Admob in Android
-keep public class com.google.android.gms.ads.**{
public *;
}
# For old ads classes
-keep public class com.google.ads.**{
public *;
}
# For mediation
-keepattributes *Annotation*
# Other required classes for Google Play Services
# Read more at http://developer.android.com/google/play-services/setup.html
-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;
}

Google maps aren't loaded after ProGuard Obfuscation in Eclipse

I am developing an app that uses google maps. The app works fine up until I Export the signed app package .After the code has been obfuscated the maps are no longer loaded. I am pretty new to ProGuard,I've tried modifying proguard.cfg but soo far nothing has worked.
Here are some of the different things that i've tried:
-libraryjars C:/Program Files/Android/android-sdk/add-ons/addon-google_apis-google_inc_-10/libs/maps.jar
-keep class android.location.** { *; }
-keepnames class com.google.android.maps.** {*;}
-keep public class com.google.android.maps.** {*;}
-dontwarn com.google.android.maps.GeoPoint
-dontwarn com.google.android.maps.MapActivity
-dontwarn com.google.android.maps.MapView
-dontwarn com.google.android.maps.MapController
-dontwarn com.google.android.maps.Overlay
Thanks
Edit: For Google Maps For Android V2 Specifically:
Just in case anyone sees this and has a similar issue - I was getting a Parcelable RuntimeException when I tried to view a second map (as in, Activity A had a map instance, then moving to Activity B, another instance with different params), pointing to what i was assuming was an obfuscated class name in the google package.
after i added
-keep class com.google.android.gms.maps.** { *; }
-keep interface com.google.android.gms.maps.** { *; }
to my proguard-project.txt everything seemed to keep working as normal
It is most likely issue with the Maps API Key. You should generate the Key with the signature used while exporting the app and use it in your map view.
Its unlikely that Obfuscation using ProGuard will affect the map rendering.
It might help:
-optimizations !code/simplification/variable
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Fragment
-keep public class * extends android.support.v4.app.Fragment
# The Maps API uses custom Parcelables.
# Use this rule (which is slightly broader than the standard recommended one)
# to avoid obfuscating them.
-keepclassmembers class * implements android.os.Parcelable {
static *** CREATOR;
}
# The Maps API uses serialization.
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
Source:
https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/proguard-rules.pro
I've had a similar issue using the Google Maps Roads API - it didn't work in obfuscated code. Turns out, the API library uses reflection under the hood, so the solution was to add
-keep class com.google.maps.** { *; }
to proguard-rules.pro.
(Adding this answer here specifically, because this question came up as the most related when I was searching for a solution to my issue, and finding the answer here would've saved me a couple hours of further searching.)
this is better
-keep class * implements com.google.android.gms.maps { *; }
-keep class com.google.android.gms.maps.** { *; }
-keep interface com.google.android.gms.maps.** { *; }

Categories

Resources