您的位置:首页 > 移动开发 > Android开发

Android java代码混淆

2013-10-16 10:20 363 查看
网上搜了很多资料,说在工程的根目录下有default.properties文件,在该文件下加上“proguard.config=proguard.cfg”,同时增加proguard.cfg文件,但我的工程下没找到这两个文件,不过找到一个proguard-project.txt的文件,进去看了看,说的很明白:To enable
ProGuard in your project, edit project.properties,好,进入project.properties,又提到:

To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):

#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

就是把上面的#去掉,然后把sdk.dir指向你的AndroidSDK的目录,我想那个proguard-android.txt应该是个模板,是个公用的模板,每个需要混淆的工程都会用到该文件。同时,proguard-project.txt又提到Add any project specific keep options
here,即具体到某个工程需要而编写的一些特定的混淆配置,需要写在proguard-project.txt文件中,比如暴露在AndroidManifest.xml中的类,service,activity等等,加入生成APK后,运行过程中,找不到某个类,如果不是jar库没有有效编译就去,那就是混淆器把这类给混淆了,运行时无法找到这个类,需要把这个类keep,保持原样。根据经验,一般只需要keep
类即可,对变量和函数一般不需要做处理

如何输出混淆的APK,右键工程--->Android Tools--->Export Unsigned Application Package。

但是用上述方法输出的apk无法安装,提示“应用程序未安装”,后来通过右键工程--->Android Tools--->Export Signed Application Package,然后开始自己新建一个key_store,设置好用

了!!!!

问题:

I am trying to use javamail in my Android app, but proguard complains about a whole load of stuff that it cannot find. I have tried various remedies that I have found while searching the internet, but nothing seems to work. Has
anyone got this thing working? Thanks.

I have added some -dontwarn commands to the defauly proguard.cfg, and succeeded in eliminating all of the warning messages except this one:
Warning: org.apache.harmony.awt.datatransfer.DataProxy:


解决:

I am using the 2.1 SDK, which might make a difference. I also have a fairly complex email. But I have sorted it now, by upgrading to proguard 4.6 and adding the following lines to the head my proguard.cfg:

-dontwarn java.awt.**

-dontwarn java.beans.Beans

-dontwarn javax.security.**

-keep class javamail.** {*;}

-keep class javax.mail.** {*;}

-keep class javax.activation.** {*;}

-keep class com.sun.mail.dsn.** {*;}

-keep class com.sun.mail.handlers.** {*;}

-keep class com.sun.mail.smtp.** {*;}

-keep class com.sun.mail.util.** {*;}

-keep class mailcap.** {*;}

-keep class mimetypes.** {*;}

-keep class myjava.awt.datatransfer.** {*;}

-keep class org.apache.harmony.awt.** {*;}

-keep class org.apache.harmony.misc.** {*;}

The last group of lines is non-trivial. I obtained it by running tar tf commands against each of the jars in the javamail package.

同时如果使用pop3接收邮件,应该加上-keep class com.sun.mail.pop3.** {*;},不然会爆出“javax.mail.nosuchproviderexception:pop3”异常,原因可能是proguard把类com.sun.mail.pop3也给混淆了,导致程序中找不到该类。

Update for SDK 17

Android SDK 17 introduces some changes in the way that jars are loaded. If you have a project that uses external jars, upgrading to SDK 17 or beyond will probably break it. To fix this, select Project > Properties > Java Build Path
> Order and Export from the menu, and check the boxes to left of the three jars used by javamail. This ensures that the jars get exported to the target build. Without this fix, the project will still build, but javamail will no longer work and proguard will
also fail. This is not a proguard issue at all. It is an Android SDK issue. No changes to proguard.cfg are required.

Another consequence of upgrading the SDK is that it is no longer necessary to upgrade proguard manually.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: