我希望在android apklib文件中混淆非公共方法等,以便我可以与其他人分发apklib文件,而不会因为apklib包含源代码而共享我的所有源代码。
我采取的方法是通过在pom.xml中启用proguard.cfg来混淆库项目中的非公共方法
以下是我的proguard.cfg的样子
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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
-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 *;
}
以下是我在Maven安装中遇到的错误
[INFO] --- android-maven-plugin:3.3.2:generate-sources (default-generate-sources)@ libraryprojects-lib1 --- [INFO] ANDROID-904-002:找到aidl文件:Count = 0 [INFO] ANDROID-904-002: 找到aidl文件:Count = 0 [INFO] C:\ GSDevelopment \ tools \ android-sdk \ platform-tools \ aapt.exe [包, -m,-J,C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ generated-sources \ r \ n, -M,C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ AndroidManifest.xml, -S,C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ res, --auto-add-overlay,-A,C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ assets, -I,C:\ GSDevelopment \ tools \ android-sdk \ platforms \ android-14 \ android.jar] [INFO] [INFO] --- maven-resources-plugin:2.6:资源 (default-resources)@ libraryprojects-lib1 --- [INFO]使用'UTF-8' 编码以复制过滤的资源。 [INFO]复制1资源[INFO] 跳过不存在的resourceDirectory C:\ GSDevelopment \ expworkspace \样品\ libraryprojects \ libraryprojects-LIB1 \目标\产生来源\萃取依赖性\ SRC \主\资源 [INFO] [INFO] --- maven-compiler-plugin:2.5.1:编译 (default-compile)@ libraryprojects-lib1 --- [INFO]编译2个源 文件到 C:\ GSDevelopment \ expworkspace \样本\ libraryprojects \ libraryprojects-LIB1 \目标\班 [INFO] [INFO] --- android-maven-plugin:3.3.2:proguard (default-proguard)@ libraryprojects-lib1 --- [INFO] C:\ GSDevelopment \ tools \ java \ jdk1.6.0_33 \ jre \ bin \ java [-Xmx512M,-jar, C:\ GSDevelopment \ TOOLS \ Android的SDK \工具\ proguard的\ LIB \ proguard.jar, @ proguard.cfg,-injars, 'C:\ GSDevelopment \ expworkspace \样品\ libraryprojects \ libraryprojects-LIB1 \目标\类', -libraryjars,'C:\ GSDevelopment \ tools \ java \ jdk1.6.0_33 \ jre \ lib \ rt.jar', -libraryjars,'C:\ GSDevelopment \ tools \ java \ jdk1.6.0_33 \ jre \ lib \ jsse.jar', -libraryjars,'C:\ GSDevelopment \ tools \ java \ jdk1.6.0_33 \ jre \ lib \ jce.jar', -libraryjars,c:.m2GSD \ repository \ com \ google \ android \ android \ 4.0.1.2 \ android-4.0.1.2.jar(!org / xml / ,! org / w3c / ,! java / ,! javax / ), - libjarjars,'c:.m2GSD \ repository \ commons-logging \ commons-logging \ 1.1.1 \ commons-logging-1.1.1.jar', -libraryjars,'c:.m2GSD \ repository \ org \ apache \ httpcomponents \ httpclient \ 4.0.1 \ httpclient-4.0.1.jar', -libraryjars,'c:.m2GSD \ repository \ org \ apache \ httpcomponents \ httpcore \ 4.0.1 \ httpcore-4.0.1.jar', -libraryjars,'c:.m2GSD \ repository \ commons-codec \ commons-codec \ 1.3 \ commons-codec-1.3.jar', -libraryjars,'c:.m2GSD \ repository \ org \ khronos \ opengl-api \ gl1.1-android-2.1_r1 \ opengl-api-gl1.1-android-2.1_r1.jar', -libraryjars,'c:.m2GSD \ repository \ xerces \ xmlParserAPIs \ 2.6.2 \ xmlParserAPIs-2.6.2.jar', -libraryjars,'c:.m2GSD \ repository \ xpp3 \ xpp3 \ 1.1.4c \ xpp3-1.1.4c.jar', - libraryjars,'c:.m2GSD \ repository \ org \ json \ json \ 20080701 \ json-20080701 。罐', -outjars,'C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ proguard-obfuscated.jar', -dump,'C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ proguard \ dump.txt', -printseeds,'C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ proguard \ seeds.txt', -printusage,'C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ proguard \ usage.txt', -printmapping,'C:\ GSDevelopment \ expworkspace \ Samples \ libraryprojects \ libraryprojects-lib1 \ target \ proguard \ mapping.txt'] [INFO]注意:有185个重复的类定义。 [信息] java.io.IOException:输出jar为空。你有没说明 适当的'-keep'选项? [INFO] at proguard.shrink.Shrinker.execute(Shrinker.java:159)[INFO] at proguard.ProGuard.shrink(ProGuard.java:275)[INFO] at proguard.ProGuard.execute(ProGuard.java:101)[INFO] at proguard.ProGuard.main(ProGuard.java:492)
我有两个带有两个私有方法的java文件。谁能指出我可能出错的地方?提前谢谢。
答案 0 :(得分:2)
不确定你是否还需要答案,但无论如何我都会给予答案,因为我遇到了类似的问题并偶然发现了你的问题......
关键在于,引用Proguard手册:“为了确定哪些代码必须保留以及哪些代码可以被丢弃或混淆,您必须为代码指定一个或多个入口点。这些入口点通常是具有主要方法,小程序,midlet,活动等的类。“
因此,您的代码中似乎没有任何公开条目。必须有公共条目,否则如何使用您的库或OS(如Android)如何调用您的代码。
您需要确定库中的入口点,然后确保正确使用“-keep”命令。例如,从Proguard库的示例来看,这就是他们保持公共类的方式:
-keep public class * {
public protected *;
}