2013-01-15 49 views
0

在Android中,我使用了以下設置的proguard。 (包含JApplet的舉例)從Android proguard中排除Java SE庫

我怎樣才能從ProGuard的排除它們

-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進口的?請注意,我有pinyin4j-2.5.0.jar-libraryjars。我認爲這是告訴程序員的方式,「嘿,這是一個圖書館,不要做任何事情,好嗎?」但似乎像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唯一方法(如小程序,鞦韆,...)不能在Android中使用的jar包。 沒有。事實上,只要您使用非Java SE-only方法,它們運行得非常好。

完整的錯誤日誌可以從這裏下載:https://www.dropbox.com/s/dns62f7gp6unusg/error-log.txt

+0

Android不具備這些類,因此,你不能在你的Android應用反正使用它們。 – CommonsWare

+0

我假設你將它作爲一個庫包含在內,因此你需要將它附加到'-libraryjars'中,但是我真的不知道你在這裏試圖做什麼。這些類在android上不起作用。 – njzk2

+0

所有這些都是圖書館。事實上,他們(pinyin4j-2.5.0.jar - > demo.Pinyin4jAppletDemo例如)在我引入proguard之前完美地工作。他們工作的原因是,我只使用來自庫的方法,它不涉及Java SE only方法。現在的問題是,ProGuard似乎想要照顧我所有的lib罐子。 –

回答

5

如果您確定不使用這些Java SE類,那麼您確實可以忽略警告(如您在自己的答案中找到的那樣)。一個簡單的方法來指定這個:

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

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

總是具有相同的答案類似的問題:

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),因爲我們忽略了所有的警告。

+0

我已經在-libraryjars中包含'libs/pinyin4j/pinyin4j-2.5.0.jar',但仍然存在demo.Pinyin4jAppletDemo錯誤(它位於pinyin4j-2.5.0.jar) –

+0

您必須包含運行時也是如此。的android.jar?我不太瞭解android。 – AlexWien

+0

實際上,在閱讀他們的文檔後,我不太瞭解-libraryjars的用法。我們告訴proguard「他們是圖書館」。好吧,如果他們是圖書館,那麼這些圖書館會採用什麼proguard? –

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」,但沒有附加信息。但是,這是另一個不同的問題。