从Android proguard中排除Java SE库

时间:2013-01-15 17:39:15

标签: android proguard

在Android中,我使用以下设置进行预测。

-dontpreverify

# Hold onto the mapping.text file, it can be used to unobfuscate stack traces in the developer console using the retrace tool
-printmapping mapping.txt

# Keep line numbers so they appear in the stack trace of the develeper console
-keepattributes SourceFile,LineNumberTable 

# The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle.
-optimizations !code/simplification/arithmetic 

# Activities, services and broadcast receivers are specified in the manifest file so they won't be automatically included
-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 

# Custom view components might be accessed from your layout files
-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*(...);
}

# event handlers can be specified in the layout files e.g. android:onClick="nextButton_onClick", I borrowed this method name notation from .NET
-keepclassmembers class * extends android.app.Activity {
    public void *_*(android.view.View);
}

# Parcelable implementations are accessed by introspection
-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

# You might want to keep your annotations
-keepattributes *Annotation*

# I use Google Guava in my app
# see http://code.google.com/p/guava-libraries/wiki/UsingProGuardWithGuava
-libraryjars libs/google/jsr305-1.3.9.jar;libs/pinyin4j/pinyin4j-2.5.0.jar
-dontwarn sun.misc.Unsafe

-keepclasseswithmembers class com.google.common.base.internal.Finalizer{
    <methods>;
}

我的一些库直接从Java SE导入(例如包含JApplet)

我怎样才能将他们从proguard中排除?请注意,pinyin4j-2.5.0.jar中有-libraryjars个库。我认为这是告诉proguard的方式,&#34;嘿,这是一个图书馆。不要做任何事情,好吗?&#34;但是,似乎proguard仍在尝试处理pinyin4j-2.5.0.jar

我收到以下错误。

Note: there were 125 duplicate class definitions.
Warning: demo.Pinyin4jAppletDemo: can't find superclass or interface javax.swing.JApplet
Warning: demo.Pinyin4jAppletDemo$1: can't find superclass or interface java.awt.event.WindowAdapter
Warning: demo.Pinyin4jAppletDemo$2: can't find superclass or interface java.awt.event.ActionListener
Warning: demo.Pinyin4jAppletDemo$3: can't find superclass or interface java.awt.event.ActionListener
Warning: org.jasypt.encryption.pbe.PBEBigDecimalCleanablePasswordEncryptor: can't find superclass or interface org.jasypt.encryption.pbe.PBEBigDecimalEncryptor
Warning: org.jasypt.encryption.pbe.PBEBigIntegerCleanablePasswordEncryptor: can't find superclass or interface org.jasypt.encryption.pbe.PBEBigIntegerEncryptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditorManager
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.IntrospectionException
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
...
...
Warning: org.jasypt.normalization.Normalizer: can't find referenced class com.ibm.icu.text.Normalizer$Mode
      You should check if you need to specify additional program jars.
Warning: there were 333 unresolved references to classes or interfaces.
         You may need to specify additional library jars (using '-libraryjars').
Warning: there were 6 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile them and try again.
         Alternatively, you may have to specify the option 
         '-dontskipnonpubliclibraryclassmembers'.
Error: Please correct the above warnings first.

有些人可能会评论说,对于包含Java SE的库jar,只有方法(如Applet,Swing,...)不能在Android中使用。的不。实际上,只要您使用非Java SE-only方法,它们就可以完美运行。

可以从此处下载完整的错误日志:https://www.dropbox.com/s/dns62f7gp6unusg/error-log.txt

3 个答案:

答案 0 :(得分:6)

如果您确定未使用这些Java SE类,您确实可以忽略警告(正如您在自己的答案中找到的那样)。一种更简单的方法来指定:

-dontwarn java.beans.**
-dontwarn java.awt.**
-dontwarn javax.swing.**

请参阅ProGuard manual > Troubleshooting > Warning: can't find referenced class

类似的问题总是有相同的答案:

答案 1 :(得分:1)

保持java运行时(rt.jar)

的示例
<libraryjar file="${java.home}/lib/rt.jar" />

看来你的行

-libraryjars libs/google/jsr305-1.3.9.jar;libs/pinyin4j/pinyin4j-2.5.0.jar

不完整。 的更新 但由于android上不存在所需的类,因此必须忽略这些警告。 但是一般不要忽略混淆警告,我们有一个严重的错误(在文件中使用obfuscation.map),因为我们忽略了所有警告。

答案 2 :(得分:0)

我只是通过

来避免错误
-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn javax.swing.**
-dontwarn java.awt.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn java.beans.**
-dontwarn org.joda.time.**
-dontwarn com.google.android.gms.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn com.ibm.icu.text.**
-dontwarn demo.**

这是完整的proguard配置

-optimizationpasses 1
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#-dontobfuscate

-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn javax.swing.**
-dontwarn java.awt.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn java.beans.**
-dontwarn org.joda.time.**
-dontwarn com.google.android.gms.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn com.ibm.icu.text.**
-dontwarn demo.**

# Hold onto the mapping.text file, it can be used to unobfuscate stack traces in the developer console using the retrace tool
-printmapping mapping.txt

# Keep line numbers so they appear in the stack trace of the develeper console 
-keepattributes *Annotation*,SourceFile,LineNumberTable

-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

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

# https://sourceforge.net/p/proguard/discussion/182456/thread/e4d73acf
-keep class org.codehaus.** { *; }

-assumenosideeffects class android.util.Log {
  public static int d(...);
  public static int i(...);
  public static int e(...);
  public static int v(...);  
}

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

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
}

# I use Google Guava in my app
# see http://code.google.com/p/guava-libraries/wiki/UsingProGuardWithGuava
-libraryjars libs/google/jsr305-1.3.9.jar

-keepclasseswithmembers class com.google.common.base.internal.Finalizer{
    <methods>;
}

它完全没有开箱即用,因为我仍然在“在proguard生成后”APK中遇到崩溃。

我很难找到崩溃的原因,因为代码是混淆的。

如果我具体dontobfuscate,我会在生成过程中遇到另一组问题。弹出“转换为Dalvik格式失败,错误1”消息,但没有附加信息。但是,这是另一套不同的问题。