从Android proguard中排除Java SE库

从Android proguard中排除Java SE库,android,proguard,Android,Proguard,在Android中,我有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

在Android中,我有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>;
}
有些人可能会评论说,对于只包含JavaSE方法的库JAR,如Applet、Swing、。。。无法在Android中使用。不。事实上,只要您使用它们的非javase-only方法,它们就可以运行得非常好


完整的错误日志可以从这里下载:

示例保存java运行时rt.jar

<libraryjar file="${java.home}/lib/rt.jar" />
这是不完整的。 使现代化 但由于android上不存在所需的类,因此必须忽略这些警告。
但通常不要忽略模糊警告,我们在文件中使用了一个obfuscation.map有一个严重的错误,因为我们忽略了所有警告。

我只是通过

-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,没有其他信息。但是,这是另一组不同的问题。

如果您确定没有使用这些Java SE类,那么您确实可以忽略您在自己的答案中发现的警告。指定此项的更简单方法:

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

答案始终相同的类似问题:

...
Android没有这些类,因此您无法在Android应用程序中使用它们。我假设您将其作为库包含,因此您需要将其附加到-libraryjars,但我不知道您在这里尝试做什么。这些类在android上不起作用。它们都是库。事实上,在我介绍proguard之前,例如pinyin4j-2.5.0.jar->demo.Pinyin4jAppletDemo就可以很好地工作。它们工作的原因是,我只使用库中的方法,而不涉及只使用JavaSE的方法。现在的问题是,ProGuard似乎想处理我所有的lib jar。@njzk2出错的类都是库。因此,我想通过告诉proguards,嘿,这些是库!。事情会好起来的。事实上,它没有帮助。我已经在-libraryjars中包含libs/pinyin4j/pinyin4j-2.5.0.jar,但是demo.Pinyin4jAppletDemo仍然存在错误。它在pinyin4j-2.5.0.jar中找到。您还必须包含运行时。android.jar?我对android不太了解。事实上,在阅读了libraryjars的文档后,我真的不明白它的用途。我们告诉proguard他们是图书馆。好的,如果它们是库,proguard将如何处理这些库?它不会混淆它们!他们呆在外面。当使用injar命令时,所有lib都会被混淆并合并到一个outjar中。但是图书馆里的罐子没有动过。通常,系统运行时lib rt.jar和所有LGPL jar(这些jar被nitvallowed设置为obfuscated.thx)以获取信息。你知道为什么,即使我在图书馆里使用了特定的pinyin4j库,我仍然会收到关于pinyin4j的proguard警告吗?好像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>;
}
-dontwarn java.beans.**
-dontwarn java.awt.**
-dontwarn javax.swing.**