• Android去掉/混淆Log,反编译都看不到

    发布:51Code 时间: 2018-10-11 10:47

  • 出发点: 当然是由于编程习惯太好,打了一堆中文log,其实只是想给测试看。然而如果包被反编译,看log基本都能理解流程了,有点尴尬。所以此文主要探究proguard配置,以去除log。...

  • 出发点:

    当然是由于编程习惯太好,打了一堆中文log,其实只是想给测试看。然而如果包被反编译,看log基本都能理解流程了,有点尴尬。所以此文主要探究proguard配置,以去除log。

    以下过程示例,来自于这段代码。

    public class MainActivity extends AppCompatActivity {

        private static final String TAG = "MainActivity";

     

        private String a = "a";

        private String b = "b";

     

        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

     

            Log.d(TAG, "This is" + a + b);

        }

    }

    啥都不配置的情况下,反编译的smali代码如下:

     

    # virtual methods

    .method public onCreate(Landroid/os/Bundle;)V

        .locals 2

     

        invoke-super {p0, p1}, Landroid/support/v7/app/c;->onCreate(Landroid/os/Bundle;)V

     

        const p1, 0x7f09001b

     

        invoke-virtual {p0, p1}, Lcom/rentee/logremove/MainActivity;->setContentView(I)V

     

        const-string p1, "MainActivity"

     

        new-instance v0, Ljava/lang/StringBuilder;

     

        const-string v1, "This is"

     

        invoke-direct {v0, v1}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

     

        iget-object v1, p0, Lcom/rentee/logremove/MainActivity;->m:Ljava/lang/String;

     

        invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

     

        iget-object v1, p0, Lcom/rentee/logremove/MainActivity;->n:Ljava/lang/String;

     

        invoke-virtual {v0, v1}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

     

        invoke-virtual {v0}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

     

        move-result-object v0

     

        invoke-static {p1, v0}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

     

        return-void

    .end method

    很明显,整条字符串拼接过程是由StringBuilder完成的。

    1.在build.gradle配置proguard-android-optimize.txt,因为默认的proguard-android.txt优化开关是关了的,而proguard的assumenosideeffects、

    assumenoexternalsideeffects配置是需要开启优化开关的。

     

        buildTypes {

            release {

                minifyEnabled true

                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            }

        }

    在proguard-rules.pro文件中配置assumenosideeffects。这是网上的通用大解。

     

    -assumenosideeffects class android.util.Log {

        public static boolean isLoggable(java.lang.String, int);

        public static int v(...);

        public static int i(...);

        public static int w(...);

        public static int d(...);

        public static int e(...);

    }

    不过也的确存在一个问题,就是去除不干净,反编译后,smail如下:

     

    # virtual methods

    .method public onCreate(Landroid/os/Bundle;)V

        .locals 1

     

        invoke-super {p0, p1}, Landroid/support/v7/app/c;->onCreate(Landroid/os/Bundle;)V

     

        const p1, 0x7f09001b

     

        invoke-virtual {p0, p1}, Lcom/rentee/logremove/MainActivity;->setContentView(I)V

     

        new-instance p1, Ljava/lang/StringBuilder;

     

        const-string v0, "This is"

     

        invoke-direct {p1, v0}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V

     

        iget-object v0, p0, Lcom/rentee/logremove/MainActivity;->m:Ljava/lang/String;

     

        invoke-virtual {p1, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

     

        iget-object v0, p0, Lcom/rentee/logremove/MainActivity;->n:Ljava/lang/String;

     

        invoke-virtual {p1, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

     

        return-void

    .end method

    Log的相关代码没了,但是还是有StringBuilder的拼接过程在,这个从根本上解决不了问题。

    2.进化版,proguard 6.0以上版本新增了assumenoexternalsideeffects和assumenoexternalreturnvalues,这两个属性6.0才引入,Android自带proguard-gradle插件并没有这么新,所以要在工程项目根目录配置如下strategy,强制将progurad指定到6.0以上版本。

    buildscript {

        configurations.all {

            resolutionStrategy {

                force "net.sf.proguard:proguard-gradle:6.0.3"

            }

        }

    }

    然后在progaurd-rules.txt文件中增加如下配置:

    -assumenoexternalsideeffects class java.lang.StringBuilder {

        public java.lang.StringBuilder();

        public java.lang.StringBuilder(int);

        public java.lang.StringBuilder(java.lang.String);

        public java.lang.StringBuilder append(java.lang.Object);

        public java.lang.StringBuilder append(java.lang.String);

        public java.lang.StringBuilder append(java.lang.StringBuffer);

        public java.lang.StringBuilder append(char[]);

        public java.lang.StringBuilder append(char[], int, int);

        public java.lang.StringBuilder append(boolean);

        public java.lang.StringBuilder append(char);

        public java.lang.StringBuilder append(int);

        public java.lang.StringBuilder append(long);

        public java.lang.StringBuilder append(float);

        public java.lang.StringBuilder append(double);

        public java.lang.String toString();

    }

     

    -assumenoexternalreturnvalues public final class java.lang.StringBuilder {

        public java.lang.StringBuilder append(java.lang.Object);

        public java.lang.StringBuilder append(java.lang.String);

        public java.lang.StringBuilder append(java.lang.StringBuffer);

        public java.lang.StringBuilder append(char[]);

        public java.lang.StringBuilder append(char[], int, int);

        public java.lang.StringBuilder append(boolean);

        public java.lang.StringBuilder append(char);

        public java.lang.StringBuilder append(int);

        public java.lang.StringBuilder append(long);

        public java.lang.StringBuilder append(float);

        public java.lang.StringBuilder append(double);

    }

    这段配置的作用是,去除返回值无用的StringBuilder的相关操作,由于在第2点配置后,Log相关代码被去除,所以Log中的字符串拼接也是无用的,会被去除。配置后smali代码是:

    # virtual methods

    .method public onCreate(Landroid/os/Bundle;)V

        .locals 0

     

        invoke-super {p0, p1}, Landroid/support/v7/app/c;->onCreate(Landroid/os/Bundle;)V

     

        const p1, 0x7f09001b

     

        invoke-virtual {p0, p1}, Lcom/rentee/logremove/MainActivity;->setContentView(I)V

     

        return-void

    .end method

    是的,一句都没有了,干干净净,爽!

    proguard官方英文原文地址:https://www.guardsquare.com/en/proguard/manual/examples#logging

    文章来源:网络
    上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8103),我们将立即处理。
  • 上一篇:月薪3000与月薪30000的程序员,差在了哪里?

    下一篇:这些开源框架你都听说过吗?

网站导航
Copyright(C)51Code软件开发网 2003-2018 , 沪ICP备16012939号-1