zoukankan      html  css  js  c++  java
  • 利用android proguard混淆代码

     

    利用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 the proguard.config property in the<project_root>/project.properties file. The path can be an absolute path or a path relative to the project's root.

    If you left the proguard.cfg file 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 running ant release or by using the Export Wizardin Eclipse, the build system automatically checks to see if the proguard.config property is set. If it is, ProGuard automatically processes the application's bytecode before packaging everything into an .apk file. 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-windowsplatformsandroid-9android.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包。

    请注明出处,此文档来自“善思善学”。
  • 相关阅读:
    linux 添加环境变量(php为例)
    composer install Your requirements could not be resolved to an installable set of packages
    pytesseract 验证码识别
    mac crontab时间断内随机时间执行定时任务
    Mac使用crontab来实现定时任务
    安居客滑动验证码识别
    jQuery图片点击预览遮罩层,再点击关闭效果
    linux系统必学-部分链接
    JavaScript概念总结:作用域、闭包、对象与原型链
    Web前端性能优化全攻略
  • 原文地址:https://www.cnblogs.com/gtgl/p/4089017.html
Copyright © 2011-2022 走看看