使用 Android proguard 忽略外部库

Posted

技术标签:

【中文标题】使用 Android proguard 忽略外部库【英文标题】:Ignore external libraries with Android proguard 【发布时间】:2013-11-02 07:10:42 【问题描述】:

我想使用 Proguard 主要是出于混淆的原因。

我对 android 中的 proguard 有疑问。我使用 simpleframework 来解析 XML;它的外部。

在 proguard cfg 中:

    -keepattributes *Annotation*,EnclosingMethod    
    -keep public class org.simpleframework.** *; 
    -keep public class org.simpleframework.xml.** *; 
    -keep public class org.simpleframework.xml.core.** *; 
    -keep public class org.simpleframework.xml.util.** *; 
    -keep public class org.simpleframework.xml.stream.** *; 
    -keep public class javax.**  *; 
    -keep public class javax.xml.stream.** *; 

    -keep public class org.simpleframework.** 
      public void set*(***);
      public *** get*();
    

错误:

Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLInputFactory
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLInputFactory
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLInputFactory
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLInputFactory
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLInputFactory
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamProvider: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.XMLEventReader
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Entry: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.Location
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.StartElement
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.StartElement
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.Location
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.StartElement
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.StartElement
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.Location
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Start: can't find referenced class javax.xml.stream.events.Attribute
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.Characters
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.Characters
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.Characters
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.XMLEvent
Warning: org.simpleframework.xml.stream.StreamReader$Text: can't find referenced class javax.xml.stream.events.XMLEvent

我不知道如何解决这个问题。 gradle cfg 的路径没问题。

在毕业典礼中:

    buildTypes 
        release  
            runProguard true
            proguardFile file('proguard.txt')
            signingConfig signingConfigs.release
        
    

【问题讨论】:

【参考方案1】:

感谢 eXistPierre 的帖子,这是我让它工作所需的最低要求:

-libraryjars <java.home>/lib/rt.jar(java/**,javax/**)
-keep public class org.simpleframework.** *; 
-keep class org.simpleframework.xml.** *; 
-keep class org.simpleframework.xml.core.** *; 
-keep class org.simpleframework.xml.util.** *; 
-keepattributes *Annotation*
-keepattributes Signature

我还需要添加以下内容,以便我可以读写我的类对象:

-keepclassmembers class com.package.app.ClassItem *; 

【讨论】:

就是这样。我很绝望,因为我找不到任何解决方案,而这正是我需要做的!非常感谢!!!!【参考方案2】:

这个proguard配置帮助我:Proguard obfuscation is breaking simplexml

# The following line may be different
-libraryjars <java.home>/lib/rt.jar(java/**,javax/**)

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
# (3)Not remove unused code
-dontshrink

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

-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
# (2)Simple XML
-keep public class org.simpleframework.** *;  
-keep class org.simpleframework.xml.** *;  
-keep class org.simpleframework.xml.core.** *;  
-keep class org.simpleframework.xml.util.** *; 
# (1)Annotations and signatures
-keepattributes *Annotation*
-keepattributes Signature

-keepclasseswithmembernames class * 
    native <methods>;


-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.app.Activity 
   public void *(android.view.View);


-keepclassmembers enum * 
    public static **[] values();
    public static ** valueOf(java.lang.String);


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

【讨论】:

【参考方案3】:

这些不是输出中的错误,而是您可以跳过的警告:

-dontwarn org.simpleframework.xml.stream.**

或者一般情况下:

-dontwarn

附:跳过后我得到了

值不是参考值 [proguard.evaluation.value.InstructionOffsetValue]

它已用adding the next line 修复

-optimizations !class/unboxing/enum

【讨论】:

以上是关于使用 Android proguard 忽略外部库的主要内容,如果未能解决你的问题,请参考以下文章

Android - proguard 忽略 -keep 的 Swig 方法

使用 Android 库项目设置 ProGuard

多库项目中的 Android Studio proguard 处理

android应用程序中的proguard

Android studio - Proguard 似乎忽略了规则,导致构建错误

Android Proguard 跳过外部 jar