【问题标题】:Exclude Java SE library from Android proguard从 Android proguard 中排除 Java SE 库
【发布时间】:2013-01-15 17:39:14
【问题描述】:

在 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 中排除?注意,我在-libraryjars 中有pinyin4j-2.5.0.jar 库。我想这就是告诉 proguard 的方式,“嘿,这是一个图书馆。不要在上面做任何事情,好吗?”但是,似乎 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

【问题讨论】:

  • Android 没有这些类,因此您无论如何都不能在您的 Android 应用中使用它们。
  • 我假设您将其作为库包含在内,因此您需要将其附加到 -libraryjars,但我真的不明白您要在这里做什么。这些类不适用于 android。
  • 都是图书馆。事实上,在我介绍 proguard 之前,它们(例如 pinyin4j-2.5.0.jar -> demo.Pinyin4jAppletDemo)可以完美运行。它们起作用的原因是,我只使用库中的方法,这不涉及仅限 Java SE 的方法。现在的问题是,ProGuard 似乎想要处理我所有的 lib jar。
  • @njzk2 有错误的类都是库。因此,我通过告诉 proguards 想,“嘿,这些是图书馆!”。事情会更好。事实上,它没有帮助。

标签: android proguard


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    保持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),因为我们忽略了所有警告。

    【讨论】:

    • 我已经在-libraryjars中包含libs/pinyin4j/pinyin4j-2.5.0.jar,但是demo.Pinyin4jAppletDemo还是有错误(在pinyin4j-2.5.0.jar中找到)
    • 你也必须包含运行时。安卓.jar?我不太了解android。
    • 其实看了他们的文档,我也不是很明白-libraryjars有什么用。我们告诉 proguard “他们是图书馆”。好的,如果它们是库,proguard 将如何处理这些库?
    • 它不会混淆它们!他们留在外部。当使用 injar 命令时,所有的库都被混淆并合并到一个 outjar 中。但是 libraryjars 中的罐子保持不变。通常是系统运行时库(rt.jar)和所有 LGPL 的 jars,它们都被 nitvallowed 混淆。
    • 谢谢信息。你知道为什么,即使我在 -libraryjars 中指定了 pinyin4j 库,我仍然在 pinyin4j 上收到 proguard 警告?似乎 proguard 正试图“触摸”它。
    【解决方案3】:

    我只是避免错误

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-19
      • 2015-07-04
      • 2013-09-03
      • 1970-01-01
      • 2011-05-27
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多