如何创建混淆jar文件?截至目前,我可以轻松地将我的Android lib项目导出到jar并使用它。如何模糊jar文件?
我的最终目标是允许其他人在他们的项目中使用我的lib.jar。我只是想尽可能多地保护代码...... :)
答案 0 :(得分:15)
事实证明这是可能的,并不是那么难。经过3个小时的尝试,我终于明白了!
Android提供了一个方便的proguard GUI:
Android的SDK \工具\ proguard的
使用GUI,您只需选择in.jar和out.jar。您必须选择android.jar并删除所选的默认rt.jar。
完成后,您必须仔细选择收缩和混淆选项。
我希望以某种方式帮助你!
答案 1 :(得分:1)
proguard.cfg示例
# optimization passes will allow multiple tries to strip dead code
# if it doesn't find anything on the 2nd pass, it won't do a 3rd.
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
# it would complain about my missing R stuff, and it does the right thing with this line
-dontwarn com.mycompany.myproject.R*
# the location of the pre-obfuscated .jar built by your project
-injars bin/myproject.jar
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
# this will fake "dead-strip" calls to Log.v() & Log.d()
-assumenosideeffects class android.util.Log {
public static int v(...);
public static int d(...);
}
# this will fake "dead-strip" calls to a more complex logging function i created
-assumenosideeffects class com.mycompany.myproject.FirstPackageWithLogging {
private void LogR(...);
}
# this will fake "dead-strip" a function in which i check to see if the
# USB-cable is connected and perform waitForDebugger(), since i don't want
# those making use of my obfuscated library to have to wait for the debgger
# when they won't be debugging my library
-assumenosideeffects class com.mycompany.myproject.MyProject {
private void setupDebugging();
}
# this will fake "dead-strip" a toast i use to measure focus
-assumenosideeffects class com.mycompany.myproject.UXObserver {
private void toastFocus();
}
# i don't want to obfuscate 3rd-party stuff, in case someone else
# is using the same classes, and wants to just get mine via pass-through
-keep class org.thirdparty.package.**
-keep class com.licensedpackage.**
# i want my API to be public, so i expose it
-keep public class com.mycompany.myproject.MyAPI
# almost all the rest are the suggestion of the proguard examples page
-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
-keepclassmembers public class com.mycompany.myproject.MyAPI {
public static <fields>;
}
-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 *;
}
...
$ java -jar $ANDROID_SDK/tools/proguard/lib/proguard.jar
-libraryjars $ANDROID_SDK/platforms/android-18/android.jar @proguard.cfg
-outjars ../MyReleaseSDK/libs/myproject.jar
就是这样。输出将包含您的混淆.jar文件。
从那时起,我将项目中的一些项目(.project文件减去NDK的外部工具构建器; res文件夹,我的jni代码的libs文件夹,以及project.properties和AndroidManifest.xml)复制到MyReleaseSDK中,然后从中创建一个.zip。那个.zip然后可以被其他人用作Eclipse“import”项目;它将创建一个空的src文件夹,正确地生成资源,并且任何想要的人现在都可以将它用作常规的Android库项目,尽管它被混淆了。
答案 2 :(得分:0)
如果它是一个外部jar,那么如果你把它放在“libs”文件夹中并用Android 2.2+ SDK编译,它会自动使用proguard来混淆apk。如果你想把这个jar作为一个库,并在你的apk中使用它之前对它进行模糊处理,那么它可能是不可能的。如果你正在谈论apk混淆而不是Android,我认为(使用proguard)来自2.2,你不必担心它。
答案 3 :(得分:0)
您为项目启用proguard - 非常简单。 http://developer.android.com/guide/developing/tools/proguard.html
可能发生的一个常见错误是使用反射时找不到方法。这是因为混淆删除了方法名称,有时甚至删除了整个类。如果您有自定义视图,则可能发生同样的事情。
使用proguard.cfg中的“-keep”标志配置要保留的类。
这是一些常见的,这是来自maven构建 - 但概念是相同的
-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.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*(...);
-keepclasseswithmembers class * {
public <init> (android.content.Context, android.util.AttributeSet); }
-keepclasseswithmembers class * {
public <init> (android.content.Context, android.util.AttributeSet, int); }
-keepclassmembers class * implements android.os.Parcelable {
static android.os.Parcelable$Creator *; }
-keepclassmembers class **.R$* { public static <fields>; }
-keepclasseswithmembernames class * { native <methods>; }
-keepclassmembers class * extends java.lang.Enum {
public static **[] values();
public static ** valueOf(java.lang.String); }
-keepclassmembers class * extends android.app.Activity{
public void *(android.view.View); }
相当大的区域,但希望它有所帮助...
答案 4 :(得分:0)
要创建模糊的jar文件,您可以通过向其添加build.xml文件并在proguard-project.txt文件中进行更改并从命令行运行ant release来实现,您将在proguard下找到obfuscated.jar项目的bin文件夹中的文件夹
步骤
在linux中的项目中添加build.xml:
打开终端并转到android / tools / ant文件夹并运行
./android update project -p /path_of_your_android_library_project
您将在项目根目录中找到build.xml。
在您的项目中启用模糊处理:
在编辑器中打开project.properties文件并从
中删除评论proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
将您的更改添加到proguard-project.txt文件中并在终端中转到您的项目并运行
ant release