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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16215106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:15:38  来源:igfitidea点击:

Configure proguard to exclude library jar which extends android.content.Context

javaandroidproguard

提问by Vinay S Shenoy

I'm integrating a library into a device for a project which is going to be embedded into the device OS. For this, we're integrating a library jar which overrides the Android framework Context.

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

Because of this, trying to enable Proguard for the app gives me the following error - "there were 220 instances of library classes depending on program classes".

因此,尝试为应用程序启用 Proguard 会给我以下错误 - “有 220 个库类实例取决于程序类”。

All of these files are Widgets and Views. How can I fix this?

所有这些文件都是小部件和视图。我怎样才能解决这个问题?

This is the content of my proguard-project.txt

这是我的 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

This is the content of my proguard-android.txt

这是我的 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.**

采纳答案by Vinay S Shenoy

Even if I'd marked my library as a libraryjar, I also had to add the lines

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

-keep public class android.content.** { *; }to the project proguard config file to fix the issues.

-keep public class android.content.** { *; }到项目 proguard 配置文件以解决问题。

回答by Eric Lafortune

The Android Ant/Eclipse builds automatically specify all necessary -injars/-outjars/-libraryjars options for you. You should only specify these options if you have a custom build process. Otherwise, you'll get lots of warnings about duplicate classes and possibly other problems.

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

If you do need to specify -injars/-outjars/-libraryjars options, classes specified with -injars can depend on classes specified with -libraryjars, but not the other way around. Cfr. ProGuard manual > Troubleshooting > Warning: library class ... depends on program class ...

如果您确实需要指定 -injars/-outjars/-libraryjars 选项,则使用 -injars 指定的类可以依赖于使用 -libraryjars 指定的类,但反过来不行。参考文献 ProGuard 手册 > 故障排除 >警告:库类...取决于程序类...