我在我的应用中包含了一个使用 gradle 的项目:
compile group: 'org.bytedeco', name: 'javacv', version: '0.11'
构建良好。但是每当我在启用混淆器的情况下运行应用程序时,它显然会从随后包含的 jar 中删除 @Platform
注释。
我尝试使用以下基于 http://proguard.sourceforge.net/manual/examples.html#annotations 的内容
-keepattributes *Annotation*
-keep @org.bytedeco.javacpp.annotation interface * {
*;
}
我还根据 http://proguard.sourceforge.net/manual/troubleshooting.html#notkept 尝试了以下方法
-keep @interface *
但这也行不通。我还能尝试什么来阻止混淆器删除这些注释?我正在考虑使用 -injars
或 -libraryjars
但我相信 gradle 会为您处理。
解决方案:
所以解决方法如下:
我在混淆器规则中包含了以下内容:
# JavaCV
-keep @org.bytedeco.javacpp.annotation interface * {
*;
}
-keep @org.bytedeco.javacpp.annotation.Platform public class *
-keepclasseswithmembernames class * {
@org.bytedeco.* <fields>;
}
-keepclasseswithmembernames class * {
@org.bytedeco.* <methods>;
}
-keepattributes EnclosingMethod
-keep @interface org.bytedeco.javacpp.annotation.*,javax.inject.*
-keepattributes *Annotation*, Exceptions, Signature, Deprecated, SourceFile, SourceDir, LineNumberTable, LocalVariableTable, LocalVariableTypeTable, Synthetic, EnclosingMethod, RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, AnnotationDefault, InnerClasses
-keep class org.bytedeco.javacpp.** {*;}
-dontwarn java.awt.**
-dontwarn org.bytedeco.javacv.**
-dontwarn org.bytedeco.javacpp.**
# end javacv
我的 gradle 中的以下行(这些是日期为 7/5/2015 (dd/mm/yyyy) 的最新版本):
compile group: 'org.bytedeco', name: 'javacv', version: '0.11'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-x86'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-x86'
我很确定有些混淆规则有点矫枉过正,但我还没有测试过哪些是多余的。如果遇到此问题,您可能需要自己解决。
最佳答案
我也在使用 javacv,这是我的 proguard 文件的样子:
## JavaCV
-keepattributes *Annotation*, Exceptions, Signature, Deprecated, SourceFile, SourceDir, LineNumberTable, LocalVariableTable, LocalVariableTypeTable, Synthetic, EnclosingMethod, RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, AnnotationDefault, InnerClasses
-keep class org.bytedeco.javacpp.** {*;}
-dontwarn java.awt.**
-dontwarn org.bytedeco.javacv.**
-dontwarn org.bytedeco.javacpp.**
它可能有点过分,但它最终使它对我有用。希望对你有帮助。
如果您将以下行添加到您的 gradle 文件中,您也不需要添加任何额外的 jar 文件:
compile group: 'org.bytedeco.javacpp-presets', name: <module>, version: <module-version>, classifier: <your-platform>
要获取可用模块,请在 jcenter 中搜索 javacpp你会看到它们为 org.bytedeco.javacv-presets:<module>
.
单击其中任何一个将使您能够获得与您的 javacv 版本相匹配的版本。因此,如果您使用的是 javacv 0.11 并想添加 opencv模块,您需要使用 2.4.11-0.11 版本的 javacpp-preset。
最后,只需添加您选择的平台android-arm
或 android-x86
或者两者兼而有之,你应该可以开始了。
最后,作为示例,这里是 arm 平台的 opencv 和 ffmpeg 的 javacv 导入:
compile group: 'org.bytedeco', name: 'javacv', version: '0.11'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm'
关于java - Proguard 删除 Android 应用程序中的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078380/