利用android proguard混淆代码

网上虽然有很多相关博客,不过貌似都不是最新版的。。于是百度+谷歌+github上的开源demo,终于成功的配置了android proguard。

最新版的android sdk的默认配置已经可以满足我们的大多数要求了,我们只需要按照sdk的提示就可以配置大部分的必须配置,然后再加上一些基本的

自定义配置就行了。

  第一步,取消project.properties里面关于proguard的注释。这一步可以开启proguard。根据网上的博客,

最好加上sdk的路径。

1 2 sdk.dir=D:/Java/IDE/adt-bundle-windows-x86_64- 20130219 /sdk proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

  

这样一来默认的配置文件也会被包括进来。

第二步,保存被AndroidManifest.xml调用或者webview的类。这是因为proguard会移除没有被调用的代码。

1 2 3 4 5 6 7 8 9 10 -keepclassmembers class fqcn.of.javascript. interface . for .webview { public *; } -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

  

第三步,第三方类库。

1 2 3 4 5 #第三方类库 -keep class android.** {*;} -keep class com.jeremyfeinstein.slidingmenu.lib.** {*;} -keep class com.actionbarsherlock.** {*;} -keep class com.lidroid.xutils.** {*;}

  

第四步,其实这一步可以代替上面所有的keep语句,彻底将误删的概率降到最低。

那就是保留所有具有公有访问权限的类和其公共成员。

1 2 3 -keepclasseswithmembers class *{ public *; }

  

第五步,在github上的开源proguarddemo上学来的,删除Log代码

1 2 3 4 5 6 7 -assumenosideeffects class android.util.Log { public static *** e(...); public static *** w(...); public static *** wtf(...); public static *** d(...); public static *** v(...); }

  然后跑了一下,成功混淆并且可以运行。。。

[Android Pro] android 混淆文件project.properties和proguard-project.txt

参考文档:http://blog.csdn.net/xueyepiaoling/article/details/8202359转载自:http://glblong.blog.51cto.com/3058613/1263969

在新版本的ADT创建项目时,混码的文件不再是proguard.cfg,而是project.properties和proguard-project.txt。

如果需要对项目进行全局混码,只需要进行一步操作:

将project.properties的中

“# proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt”的“#”去掉就可以了。

如 果有一些代码不能被混淆,比如需要加入了so文件,需要调用里面的方法,那么调用JNI访问so文件的方法就不能被混码。在导出的时候,可能不会报错。但 是在手机上运行的时候,需要调用so文件的时候,就会报某某方法无法找到。这个时候就需要用到proguard-project.txt。

在老版本中,创建项目的时候,会给出proguard.cfg,但是在的新版中创建项目则不会有任何提示。这个时候需要只要将proguard.cfg的内容加入到proguard-project.txt中,再根据自己的需要进行编辑即可。

==================proguard.cfg防反编译方法==================================

To enable ProGuard so that it runs as part of an Ant or Eclipse build, set theproguard.configproperty in the<project_root>/project.propertiesfile. The path can be an absolute path or a path relative to the project's root.

If you left theproguard.cfgfile in its default location (the project's root directory), you can specify its location like this:

proguard.config=proguard.cfg

You can also move the the file to anywhere you want, and specify the absolute path to it:

proguard.config=/path/to/proguard.cfg

When you build your application in release mode, either by runningant releaseor by using theExport Wizardin Eclipse, the build system automatically checks to see if theproguard.configproperty is set. If it is, ProGuard automatically processes the application's bytecode before packaging everything into an.apkfile. Building in debug mode does not invoke ProGuard, because it makes debugging more cumbersome.

===========【转】混淆文件proguard.cfg详解======================================

-injars androidtest.jar【jar包所在地址】

-outjars out【输出地址】

-libraryjars 'D:\android-sdk-windows\platforms\android-9\android.jar' 【引用的库的jar,用于解析injars所指定的jar类】

-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

-keep public class com.android.vending.licensing.ILicensingService

-keep public abstract interface com.asqw.android.Listener{

public protected <methods>; 【所有方法不进行混淆】

}

-keep public class com.asqw.android{

public void Start(java.lang.String); 【对该方法不进行混淆】

}

-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 *;

}

=====================================常见异常===================================

参考:http://blog.csdn.net/vrix/article/details/7100841

加入第三方jar包之后常出现的几个异常:

proguard returned with error code 1.See console

情况1:

Proguard returned with error code 1. See console

Error: C:/Documents (系统找不到指定文件)

后来发现是因为将整个工程放到了桌面上,而桌面的目录是C:/Documents and Settings/Administrator/桌面,在这里面有空格,而proguard进行发编译的时候是不允许有空格的

如果换了正确路径还不好用的话,直接删除proguard就好了

注意:SDK和程序路径最好不要有空格符

情况2:

Proguard returned with error code 1. See console

异常:

java.lang.ArrayIndexOutOfBoundsException

解决办法:将proguard.cfg中的"-dontpreverify"改成“-dontoptimize”

参考文章:http://groups.google.com/group/android-developers/browse_thread/thread/eca3b0f5ce6ad00f

我把项目中生成的proguard文件夹(此时文件夹是空的)删掉,然后再重新运行项目,就OK 了。

情况3:

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] Proguard returned with error code 1. See console

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0] java.io.IOException: Can't read [proguard.ClassPathEntry@106082] (No such file or directory)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:230)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:200)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.readInput(InputReader.java:178)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.InputReader.execute(InputReader.java:100)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.readInput(ProGuard.java:195)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.execute(ProGuard.java:78)

[2011-10-21 13:22:32 - ZMKSMarket_Build_v1.0]

at proguard.ProGuard.main(ProGuard.java:499)

抛出这样的异常的原因是第三方jar的引用路径不对,没有找到这个需要忽略混淆的jar包。

更多相关文章

  1. [转]JS调用Android里面的方法,Android调用JS里面的方法
  2. Android中常用基本控件的使用方法和步骤(.txt)
  3. Android 文件系统移植
  4. android studio修改配置文件(.android .gradle .AndroidStudio)
  5. Android NDK系列(三)-AS编写C文件没有提示和不识别NULL
  6. Android开发之文件下载
  7. 解决android中使用shape文件画虚线不显示
  8. 3.6.3新版本AndroidStudio报Could not resolve all artifacts fo
  9. AndroidManifest.xml文件剖析 (二)

随机推荐

  1. 前端优化方案-JavaScript 优化方案
  2. HTML5练习之简陋版我画你猜(一)
  3. 有一个简单但有用的jquery.JsPlumb示例吗
  4. 根据AngularJS中的条件制作输入类型文件[
  5. JqueryUI Draggable - 仅自动垂直调整父
  6. iKcamp出品|全网最新|微信小程序|基于最新版
  7. 如何使用变量填充iframe源?
  8. 使信号器生成的代理集线器动态
  9. 只有在读取数据库后才选择(案例)(开关)
  10. 未捕获的IndexSizeError:未能在'HTMLTabl