Proguard and Google SDK / Facebook SDK missing methods

1k Views Asked by At

I have hard time making my customer's app compile with proguard on (i installed the latest version of proguard in the correct folder). --edit--: i was not using the latest version as multiple copies were installed and the build script did not use the correct one.

I've created a proguard.cfg file which resolves most of compile errors. The app targets latest android SDK (8) and have a minSdk set to 21.

There is 2 compile errors left:

#1>PROGUARD : warning : com.google.android.gms.internal.zzx: can't find referenced method 'void addHeader(java.lang.String,java.lang.String)' in program class com.google.android.gms.internal.zzx$zza
#1>PROGUARD : warning : com.google.android.gms.internal.zzx$zza: can't find referenced method 'void setURI(java.net.URI)' in program class com.google.android.gms.internal.zzx$zza

On stackoverflow, on java, they resolve the problem by adding something to Graddle. On Xamarin ... you can't.

I was able to ignore the 2 warnings by adding a dontwarn instruction in the proguard file. It then compile, deploy and run nearly fine. Nearly all the app does run fine.
Except:
- Google auth, crashing
- facebook auth, crashing
- Google location (fuse) crashing.

The crashes are caused by missing methods. Methods that have been removed by proguard.

For facebook:

NoSuchMethodError: no non-static method"Lcom/facebook/internal/CallbackManagerImpl;.onActivityResult(IILandroid/content/Intent;)Z"
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <e3048811891c45499b4d89daf4d10667>:0
at Java.Interop.JniEnvironment+InstanceMethods.GetMethodID (Java.Interop.JniObjectReference type, System.String name, System.String signature) [0x0005b] in <48117e3895d549baa70c8cbd8592b31c>:0
at Android.Runtime.JNIEnv.GetMethodID (System.IntPtr kls, System.String name, System.String signature) [0x00007] in <758a804725c84b16bcab28b784c87cae>:0
at Xamarin.Facebook.ICallbackManagerInvoker.OnActivityResult (System.Int32 requestCode, System.Int32 resultCode, Android.Content.Intent data) [0x00015] in <53b39e4821ad43cba06dc6bebd7ae5f1>:0
at Appname.Droid.Views.Signup.SignInActivity.OnActivityResult

For google auth:

NoSuchMethodError: no non-static method "Lcom/google/android/gms/auth/api/signin/internal/zzc;.silentSignIn(Lcom/google/android/gms/common/api/GoogleApiClient;)Lcom/google/android/gms/common/api/OptionalPendingResult;"

For google location (fuse):

NoSuchMethodError: no non-static method "Lcom/google/android/gms/internal/zzary;.requestLocationUpdates(Lcom/google/android/gms/common/api/GoogleApiClient;Lcom/google/android/gms/location/LocationRequest;Lcom/google/android/gms/location/LocationListener;Landroid/os/Looper;)Lcom/google/android/gms/common/api/PendingResult;"

I have also tryed with the preview nugets of google play services. No luck. Any idea how to resolve these errors ?

** EDIT 1 ** Using the link from John Douglas in the comments, and the links in the answer from eugen, and a detailed diagnostic build, after 2 hours of fixing errors the proguard file is finally working fine for both Facebook and Google SDK !

Ty so much !

1

There are 1 best solutions below

4
On

This is proguard config from Facebook SDK for Android 4.23:

-keepclassmembers class * implements java.io.Serializable {
    private 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: facebook-android-sdk on Github

This is proguard config from Google Play services 11.2:

# Keep SafeParcelable value, needed for reflection. This is required to support backwards
# compatibility of some classes.
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

# Needed for Parcelable/SafeParcelable classes & their creators to not get renamed, as they are
# found via reflection.
-keep class com.google.android.gms.common.internal.ReflectedParcelable
-keepnames class * implements com.google.android.gms.common.internal.ReflectedParcelable
-keepclassmembers class * implements android.os.Parcelable {
  public static final *** CREATOR;
}

# Keep the classes/members we need for client functionality.
-keep @interface android.support.annotation.Keep
-keep @android.support.annotation.Keep class *
-keepclasseswithmembers class * {
  @android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
  @android.support.annotation.Keep <methods>;
}

# Keep the names of classes/members we need for client functionality.
-keep @interface com.google.android.gms.common.annotation.KeepName
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
  @com.google.android.gms.common.annotation.KeepName *;
}

# Keep Dynamite API entry points
-keep @interface com.google.android.gms.common.util.DynamiteApi
-keep @com.google.android.gms.common.util.DynamiteApi public class * {
  public <fields>;
  public <methods>;
}

# Needed when building against pre-Marshmallow SDK.
-dontwarn android.security.NetworkSecurityPolicy

# Needed when building against Marshmallow SDK.
-dontwarn android.app.Notification

# Protobuf has references not on the Android boot classpath
-dontwarn sun.misc.Unsafe
-dontwarn libcore.io.Memory

# Internal Google annotations for generating Proguard keep rules.
-dontwarn com.google.android.apps.common.proguard.UsedBy*

Source: com.google.android.gms:play-services-basement:11.2.0 artifact from Google repo

Proguard setup for Google Play services is also described in Set Up Google Play Services > Add Google Play Services to Your Project > Other. The configuration looks a bit different from what's found in the library .aar, try this one first.