如何创建混淆的jar文件?

问题描述

| 如何创建混淆的jar文件?到目前为止,我可以轻松地将我的Android lib项目导出到jar中并使用它。我如何混淆jar文件? 我的最终目标是允许其他人在他们的项目中使用我的lib.jar。我只想尽可能地保护代码... :)     

解决方法

事实证明这是可能的,而且并不难。经过3个小时的尝试,我终于明白了! Android在以下情况下提供了便捷的Proguard GUI:   android-sdk \\ tools \\ proguard 使用GUI,您只需选择in.jar和out.jar。比您必须选择android.jar并删除所选的默认rt.jar。 完成后,您必须仔细选择“收缩”和“混淆”选项。 我希望以某种方式有所帮助!     ,弄清楚这一点并不容易,但是一旦完成,重复这些步骤就不那么困难了: 像往常一样创建和构建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 *;
}
在命令行上(这是针对darwin的,但在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
而已。输出将包含混淆的.jar文件。 从那时起,我将项目中的几个项目(.project文件减去NDK的外部工具生成器; res文件夹,jni代码的libs文件夹以及project.properties和AndroidManifest.xml)复制到MyReleaseSDK中,然后创建一个。滑出它。然后,其他人可以将.zip用作Eclipse“导入”项目;它将创建一个空的src文件夹,正确生成资源,并且即使有人对其进行了混淆,现在任何想要使用它的人都可以将其用作常规Android库项目。     ,如果它是一个外部jar,那么如果您将其放在\“ libs \”文件夹中并使用Android 2.2+ SDK进行编译,它将自动使用proguard来混淆apk。如果您希望将此jar作为库,并且在将其用于apk之前也对其进行了混淆处理,则可能无法实现。如果您谈论的是apk混淆,那么我认为Android会从2.2开始使用proguard进行混淆(使用proguard),而您不必为此担心。     ,您可以为您的项目启用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,int);
                                public void set*(...);  
-keepclasseswithmembers class * {
                                public <init> (android.content.Context,android.util.AttributeSet); } 
-keepclasseswithmembers class * {
                                public <init> (android.content.Context,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); } 
很大的面积,但希望对您有帮助...     ,要创建混淆的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