【问题标题】:ProGuard: can't find referenced class com.google.android.gms.RProGuard:找不到引用的类 com.google.android.gms.R
【发布时间】:2013-09-09 21:53:13
【问题描述】:

在 Android SDK 管理器中进行一些更新后,我尝试制作已签名的 apk 并得到以下信息:

ProGuard: [] Warning: com.google.android.gms.auth.GoogleAuthUtil: 
  can't find referenced class com.google.android.gms.R
ProGuard: [] Warning: com.google.android.gms.auth.GoogleAuthUtil: 
  can't find referenced class com.google.android.gms.R$string
...
etc.

如果设置-dontwarn com.google.android.gms.** 编译是可以的。但运行后我收到许多这样的错误报告(来自许多设备):

Caused by: android.view.InflateException: Binary XML file line #32: 
  Error inflating class com.google.android.gms.common.SignInButton

在我的设备上一切正常。在更新之前,我没有 ProGuard 警告并且一切正常。怎么解决的?

【问题讨论】:

标签: android proguard google-play-services


【解决方案1】:

虽然将其添加到 proguard-project.txt 文件有效,但它会保留所有类。

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

我更喜欢这个,它可以让 apk 文件更小:

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

还请在此处注意最新的 Google Play Proguard 通知:http://developer.android.com/google/play-services/setup.html#Proguard

-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;
}

【讨论】:

  • 您的第二个建议帮助我节省了几 Mb。谢谢!
  • 经过 5 天的挣扎和流血的眼睛,你救了我,谢谢
  • 这仍然相关吗?播放服务的 proguard 部分说不需要特殊配置。
  • 我已经删除了这两行,APK 的生成没有任何问题。 Firebase 分析和邀请工作正常,还没有尝试其他任何方法。
  • 我仍然不断收到关于 Xamarin 的崩溃报告 Didn't find class "com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver" on path DexPathList,所以我将实施这些行,看看它是否适合我
【解决方案2】:

您需要像编译一样忽略,但您还需要保留该类,以便它可以在运行时找到它。

将这两行添加到您的 proguard 配置文件中:

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

【讨论】:

  • 这太多了。
  • @rds 显然它使用通配符,您可以通过提供更具体的类命名来选择您想要的内容。当您开始删除 GMS 类时,您可能会遇到很多意外的用户崩溃问题。
  • 我们是否应该将 keep 与 dontwarn 一起用于我们想要忽略的任何警告?
【解决方案3】:

我遇到了类似的问题,最终发现我已经更新了 Google Play 服务模块,但是我没有将该模块重新添加到我在 Android Studio 中的主模块中。添加回来解决了我的问题。

【讨论】:

    【解决方案4】:

    如果你使用proguard,你需要保留一些GMS(Google Play Services)类。希望它们带有 @com.google.android.gms.common.annotation.KeepName 注释。

    # Proguard config for project using GMS
    
    -keepnames @com.google.android.gms.common.annotation.KeepName class
        com.google.android.gms.**,
        com.google.ads.**
    
    -keepclassmembernames class
        com.google.android.gms.**,
        com.google.ads.** {
        @com.google.android.gms.common.annotation.KeepName *;
    }
    
    # Called by introspection
    -keep class
        com.google.android.gms.**,
        com.google.ads.**
        extends java.util.ListResourceBundle {
        protected java.lang.Object[][] getContents();
    }
    
    
    # This keeps the class name as well as the creator field, because the
    # "safe parcelable" can require them during unmarshalling.
    -keepnames class
        com.google.android.gms.**,
        com.google.ads.**
        implements android.os.Parcelable {
        public static final ** CREATOR;
    }
    
    # com.google.android.gms.auth.api.signin.SignInApiOptions$Builder
    # references these classes but no implementation is provided.
    -dontnote com.facebook.Session
    -dontnote com.facebook.FacebookSdk
    -keepnames class com.facebook.Session {}
    -keepnames class com.facebook.FacebookSdk {}
    
    # android.app.Notification.setLatestEventInfo() was removed in
    # Marsmallow, but is still referenced (safely)
    -dontwarn com.google.android.gms.common.GooglePlayServicesUtil
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 2014-06-23
      • 2012-10-13
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多