【问题标题】:proguard can't find referenced classproguard 找不到引用的类
【发布时间】:2014-06-23 01:02:43
【问题描述】:

我正在尝试在我的发行版 apk 上运行 proguard,但我在控制台中收到很多“找不到引用的类”错误。

[2014-05-06 17:35:05 - DesiDime] Warning: library class android.net.http.AndroidHttpClient extends or implements program class org.apache.http.client.HttpClient
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.http.protobuf.ProtoHttpContent: can't find referenced class com.google.protobuf.MessageLite
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonFactory: can't find referenced class com.google.gson.stream.JsonReader
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonFactory: can't find referenced class com.google.gson.stream.JsonReader
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonFactory: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonFactory: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonGenerator: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonGenerator: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonGenerator: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonGenerator: can't find referenced class com.google.gson.stream.JsonWriter
[2014-05-06 17:35:05 - DesiDime] Warning: com.google.api.client.json.gson.GsonGenerator: can't find referenced class com.google.gson.stream.JsonWriter

下面是我的 proguard-project.txt 文件,取自here

-injars      bin/classes
-injars      libs
-outjars     bin/classes-processed.jar
-libraryjars /home/vihaan/Downloads/adt-bundle-linux-x86-20131030/sdk/platforms/android-19/android.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Activity
-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.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

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

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

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

我在我的 project.properties 文件中取消了对这一行的注释

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

我的 libs 目录包含所需的库

我不明白为什么会出现这些错误?

【问题讨论】:

    标签: android release proguard


    【解决方案1】:

    我可以看到您在使用 Gson 和 protobuf 时遇到了问题。 为了将 Gson 与 proguard 一起使用,请在您的 proguard-project.txt 文件中包含填充行:

    # 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.
    -keepattributes Signature
    
    # For using GSON @Expose annotation
    -keepattributes *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.** { *; }
    

    整个例子可以在here找到

    为了修复 protobuf 错误,请尝试:

     -keep class com.google.protobuf.** { *; }
    

    【讨论】:

    • 我已经放置了示例文件,但我仍然收到警告。
    • 尝试取消注释 -keep class com.google.gson.stream.** { ;并更改特定于您的项目的模型类的名称:更改 -> -keep class com.google.gson.examples.android.model.* { *; }
    猜你喜欢
    • 2012-09-16
    • 2012-10-13
    • 2011-10-21
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多