配置proguard以排除扩展android.content.Context的库jar

时间:2013-04-25 12:43:15

标签: java android proguard

我正在将一个库集成到一个项目的设备中,该项目将嵌入到设备操作系统中。为此,我们正在集成一个库jar,它覆盖了Android框架Context。

因此,尝试为应用程序启用Proguard会出现以下错误 - “根据程序类有220个库类实例”。

所有这些文件都是小部件和视图。我该如何解决这个问题?

这是我的proguard-project.txt

的内容
-injars      bin/classes
-outjars     bin/classes-processed.jar
-libraryjars /Users/me/android-sdk-mac_86/platforms/android-15/android.jar
-libraryjars libs/library-that-overrides-context.jar

-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.content.Context

这是我的proguard-android.txt

的内容
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

2 个答案:

答案 0 :(得分:0)

Android Ant / Eclipse构建会自动为您指定所有必需的-injars / -outjars / -libraryjars选项。如果您有自定义构建过程,则应仅指定这些选项。否则,您会收到很多关于重复类和其他问题的警告。

如果确实需要指定-injars / -outjars / -libraryjars选项,则使用-injars指定的类可以依赖于使用-libraryjars指定的类,但不能反过来。 (CFR)。 ProGuard手册&gt;疑难解答&gt; Warning: library class ... depends on program class ...

答案 1 :(得分:0)

即使我将我的库标记为libraryjar,我也必须添加行

-keep public class android.content.** { *; }到项目proguard配置文件来修复问题。