由于 proguard 规则,生成签名 APK 时出错

Posted

技术标签:

【中文标题】由于 proguard 规则,生成签名 APK 时出错【英文标题】:Error generating signed APK due to proguard rules 【发布时间】:2019-05-01 12:45:00 【问题描述】:

在我将 android Studio 更新到 3.3-rc01 并启用 R8 代码收缩后,生成签名的 APK 出错。

它给出以下proguard错误:

Error: ~/app/proguard-rules.pro, offset: 2613, line: 74, column: 7, Expected char '-' at ~/app/proguard-rules.pro:74:7
-dump class_files.txt

在 android studio 3.3-rc01 之前并且没有 R8 代码收缩它工作得非常好。

任何解决方法,否则我现在必须禁用 proguard 规则。

以下是我的 proguard-rules.pro 文件

    # Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the javascript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview 
#   public *;
#

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Retrofit does reflection on generic parameters and InnerClass is required to use Signature.
-keepattributes Signature, InnerClasses

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * 
    @retrofit2.http.* <methods>;


# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.-KotlinExtensions


# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform


-dontwarn javax.xml.stream.**
-dontwarn rx.internal.util.unsafe.**


#Glide proguard rules
-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.resource.bitmap.ImageHeaderParser$** 
  **[] $VALUES;
  public *;


-keep class android.support.v7.widget.LinearLayoutManager  *; 

-keep public class * extends android.support.v7.widget.RecyclerView$LayoutManager 
    public <init>(...);


##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt

-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-repackageclasses ''

-keep class com.qikcircle.qiketask.models.**  *; 

-keep class android.support.v4.** *;
-keep public class * extends android.app.Activity
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable 
    static final long serialVersionUID;
    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();


# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * 
    native <methods>;


-keepclasseswithmembernames class * 
    public <init>(android.content.Context, android.util.AttributeSet);


-keepclasseswithmembernames class * 
    public <init>(android.content.Context, android.util.AttributeSet, int);


# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* 
  public static <fields>;


# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * 
    public static **[] values();
    public static ** valueOf(java.lang.String);


-keep public class * 
    public protected *;


-keep class * implements android.os.Parcelable 
  public static final android.os.Parcelable$Creator *;

##---------------End: proguard configuration common for all Android apps ----------

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.

# For using GSON @Expose annotation
# Gson specific classes
-keep class sun.misc.Unsafe  *; 
#-keep class com.google.gson.stream.**  *; 

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.**  *; 

##---------------End: proguard configuration for Gson  ----------

#removing logs
-assumenosideeffects class android.util.Log 
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);


#for getting crash reports
-assumenosideeffects class android.util.Log 
    public static int v(...);
    public static int d(...);


#Crashlytics proguard rules
-keepattributes SourceFile,LineNumberTable

-keep public class * extends java.lang.Exception

-ignorewarnings
-keep class * 
    public private *;

【问题讨论】:

嗨!你还有这个错误吗?你试过 AS 3.3-rc02 吗? 是的,即使在 AS 3.3-rc03 中也仍然面临同样的问题 您可以发布您的proguard-rules.pro 文件吗?看来您在 keep 指令中缺少 '-' 。实际上,今天我已经尝试过 R8(全模式)并且一切正常……至少乍一看……^_^ 它是空的,因为你只使用了收缩吗? 我打开了issue,他们确认他们还不支持它,但他们不应该解析失败^_^ 【参考方案1】:

我试过dump directiveProguard(我以前没用过),一切正常。

[...]

## Strip Log.d
-assumenosideeffects class android.util.Log 
    public static *** d(...);


-dump foo.txt

[...]

我在./app/foo.txt 下找到了我的foo.txt

Printing classes to [./app/foo.txt]...

然后我在传统模式下启用R8

# For the bravests
android.enableR8=true
# For the crazyest. Must be enable also the previous setting
#android.enableR8.fullMode=true

我得到了你的错误。

没有这一行,即使在完整模式下我也可以编译我的 APK,而不会出现任何(明显)问题。

所以我确认此指令不受支持。请记住,R8 仍处于测试阶段。如果您愿意,可以在我在评论中发布的链接上提交问题。

打开问题here。

【讨论】:

【参考方案2】:

对我来说,我必须删除我的 prograuard.pro 文件中的第一行“public private *;”

【讨论】:

以上是关于由于 proguard 规则,生成签名 APK 时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 3 中激活 proguard 生成签名 Apk 时出错

无法使用 proguard 生成签名的 apk

启用 ProGuard 会导致签名 APK 生成失败?

Android Studio 无法在 proguard 错误配置中生成签名的 APK

Android - 使用 Proguard 创建签名的 APK

签名的 apk 在 proguard enable 中崩溃