混淆

Android Studio: 
只需在build.gradle(Module:app)中的buildTypes中增加release的编译选项即可,如下:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> buildTypes {
        release {
            minifyEnabled <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>
            proguardFiles getDefaultProguardFile(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'proguard-android.txt'</span>), <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'proguard-rules.pro'</span>
        }
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

这个proguard-android.txt是sdk中groguard默认的文件,具体地址在:/opt/sdk/tools/proguard/proguard-android.txt 
而proguard-rules.pro是AS中专用的proguard配置文件,其实只是后缀名不同,与Eclipse中的proguard-project.txt是一样的,配置规则相同,后面会详细提到。 
老版本开启混淆的命令是runProguard,现在统一用minifyEnabled命令了,将其设为true就好了。 
编译的时候可以使用命令:

<code class="hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">./gradlew assembleRelease</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

或者用上一篇生成签名apk的办法都可。

Eclipse: 
在project.properties文件中开启proguard配置(放开注释),如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">proguard<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.config</span>=${sdk<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dir</span>}/tools/proguard/proguard-android<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>:proguard-project<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

同样,生成签名时代码就会被混淆。

混淆语法 
请参考${sdk.dir}/tools/proguard/proguard-android.txt文件,需要注意的是文件中虽然有了不混淆Parcelable的语句,如下:

<code class="hljs php has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> * <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">android</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">os</span>.<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Parcelable</span> {</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> android.os.Parcelable<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$Creator</span> *;
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

但是还是要自己把继承自Parcelable的类写进来避免混淆,否则会出现BadParcelableException异常。

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.linc</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.datatype</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.XXInfo</span> {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

为微信分享而引入的jar包,我们不需要对其进行混淆,也需要在proguard-android.txt中注明,如下:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span>.** { *<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">; }</span>
-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WXMediaMessage</span> {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span>
-keep class <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span>.** implements <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tencent</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.mm</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.sdk</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.openapi</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WXMediaMessage</span>$IMediaObject {*<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;}</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

为了验证是否混淆成功,可以使用下面的反编译工具验证。


反编译

主要用到三个工具: 
dex2jar:将dex文件转为jar文件 
jd-gui:反编译jar文件 
AXMLPrinter2.jar:反编译xml文件

使用方法参见《反编译apk文件,得到其源代码的方法》

对于Ubuntu64位,运行jd-gui或许会报错: 
尝试解决如下:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">sudo</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">apt-get</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">install</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libgtk2</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libnss3</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libcurl3-gnutls</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libidn11</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpango1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpangox-1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libpangoxft-1</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">librtmp0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libxft2</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span>
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

又报错:

<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ /opt/sdk/tools/jd-gui: error <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> loading <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">shared</span> libraries: libXxf86vm.so<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.1</span>: cannot <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">open</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">shared</span> object <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>: No such <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> directory</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

解决办法如下:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">$ <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">sudo</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">apt-get</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">install</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libgtk2</span><span class="hljs-class" style="box-sizing: border-box; color: rgb(155, 112, 63);">.0-0</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libxxf86vm1</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">libsm6</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:i386</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">lib32stdc</span>++6

</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

参考: 

本文同步分享在 博客“xiangzhihong8”(CSDN)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

04-17 06:09