2011-05-23 53 views
8

我该如何去创建一个混淆jar文件?截至目前,我可以轻松地将我的Android lib项目导出到jar并使用它。如何混淆jar文件?如何创建一个混淆的jar文件?

我的最终目标是让别人在他们的项目中使用我的lib.jar。我只是想尽可能保护代码... :)

回答

15

原来,这是可能的,并没有那么难。经过3小时的努力,我终于明白了!

Android提供下方便proguard的GUI:

Android的SDK \工具\ proguard的

使用GUI,你只需选择in.jar和out.jar。比你必须选择android.jar并删除所选的默认rt.jar。

一旦完成,您必须仔细选择缩小和混淆选项。

我希望以某种方式帮助您!

+3

用android.jar替换rt.jar绝对值得一提。 +1。 – Dave 2013-05-30 22:20:37

+0

感谢您发布这个答案 - 你可能会添加你如何保持图书馆类,让他们无障碍吗?导入后(libs文件夹,添加到构建路径)混淆的库与类如A.class,没有被识别,我坚持什么要保持,而仍然保护代码 – brandall 2013-11-20 16:25:16

0

如果它是外部jar,那么如果你在“libs”文件夹中并且使用Android 2.2+ SDK进行编译,它会自动使用proguard混淆APK。如果你想将这个jar作为一个库,并且在你的apk中使用它之前将其混淆,那么它可能是不可能的。如果你正在谈论apk的混淆而不是Android,我认为这是从2.2开始的(带有proguard),你不必担心它。

+0

感谢您的评论。其实我正在研究混淆实际的lib.jar文件。 Flurry使用他们的SDK lib来做这件事。 – Jona 2011-05-23 21:05:39

+0

混淆jar可能就像在其上运行proguard一样简单,在Android apk中使用该混淆jar可能很困难。 – omermuhammed 2011-05-23 21:08:56

0

您可以为您的项目启用proguard - 非常简单。 http://developer.android.com/guide/developing/tools/proguard.html

可能发生的一个常见错误是使用反射时找不到方法。这是因为混淆会删除方法名称,有时甚至会删除整个类。如果您有自定义视图,则可能会发生同样的情况。

您配置使用“-keep”标志proguard.cfg

下面是一些常见的,这是从一个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); } 

相当大的一个领域,但希望它可以帮助...

+0

那么这是一个完整的Android项目,我已经没有问题地工作了。我只想用我的代码混淆一个jar文件。该罐子与最终项目完全分开。该jar包含Android代码。这个jar打算分发给其他开发者。 – Jona 2011-05-24 12:51:54

0

要创建一个模糊的jar文件,您可以通过添加build.xml文件并在proguard-project.txt文件中进行更改并从命令行运行ant版本来完成该操作,并且您会发现obfuscated.jar位于

打开一个终端进入到Android /工具/蚂蚁倍:在项目

步骤

  • 要在您的项目在linux中添加的build.xml您的bin文件夹中的ProGuard文件夹呃并且运行

    ./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 
    
1

figurin克该出来是不容易的,但一旦完成,重复步骤并不难:

  • 创建和构建你的Android库项目,你通常会
  • 创建proguard.cfg文件(以下是配方,但包含一些重要的部分。

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 *; 
} 
  • 在命令行上(这是面向达尔文,但应主要在Windows

一样...

$ java -jar $ANDROID_SDK/tools/proguard/lib/proguard.jar 
-libraryjars $ANDROID_SDK/platforms/android-18/android.jar @proguard.cfg 
-outjars ../MyReleaseSDK/libs/myproject.jar 

就是这样,输出将包含你的obfu scated .jar文件。

从这一点上,我复制了我的项目中的几个项目(.project文件减去NDK的外部工具构建器; res文件夹,我的jni代码的libs文件夹以及project.properties和AndroidManifest.xml)到MyReleaseSDK中,然后创建一个.zip。 .zip可以被其他人用作Eclipse的“导入”项目;它会创建一个空的src文件夹,正确地创建资源,以及任何希望现在可以将其用作常规Android库项目(尽管它被混淆)的人。