使用Android proguard忽略外部库

时间:2013-10-23 20:08:23

标签: android proguard

我想使用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的路径没问题。

in gradle:

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

感谢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 :(得分:2)

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

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

或一般情况下:

-dontwarn

P.S。跳过后我得到了

  

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

已将adding the next line修复为proguard

-optimizations !class/unboxing/enum