1. 程式人生 > >Android混淆打包

Android混淆打包

一、理論知識   ProGuard是一款免費的Java類檔案壓縮器、優化器和混淆器。它能發現並刪除無用類、欄位(field)、方法和屬性值(attribute)。它也能優化位元組碼並刪除無用的指令。最後,它使用簡單無意義的名字來重新命名你的類名、欄位名和方法名。經過以上操作的jar檔案會變得更小,並很難進行逆向工程。 二、基本使用   在Android應用程式也可以使用 ProGuard來進行混洗打包,大大的優化Apk包的大小。但是注意 ProGuard對檔案路徑的名名很有講究,不支援括號,也不支援空格 在混淆過後,可以在工程目錄的
proguard中的 mapping.txt 看到混淆後的類名,方法名,變數名和 混淆前的類名,方法名,變數名。    在使用Eclipse或Ant打包應用程式時,都是使用Android工程目錄的 project.properties 檔案來指定配置。關於Android中如何使用ant打包請參考《 Android中使用Ant編譯打包 在使用Eclipse新建一個工程,都會在工程目錄下生產配置project.properties和proguard-project.tx 檔案如下所示:
例1
 
   

 

# 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   # Project target.
target=android-10

 

project.properties用於配置Android工程的一些屬性,#號的話表示當前行是註釋,這裡的 proguard.config 就用於指定 ProGuard的 混淆配置檔案,並對使用 release 方式打包應用程式時開啟 程式碼混淆 功能。對於是否是 使用release方式打包,和 AndroidManifest.xml 中application的android:debuggable屬性有很多關係。如果該值為 android:debuggable=" true " ,那麼最終就是 debug方式打包。最明智的方式就是在 AndroidManifest.xml並不顯示的指定它,而是是打包工具在打包時來決定它最終的值。對於ant就是 ant  release ant  debug。 而對於直接在Eclipse中使用 run  debgu 來打包的話就是debug,使用export的話就是 release.
 
   

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

這裡的話指定了混淆的基本配置檔案 proguard-android.txt,和 混淆的個性化配置檔案 proguard-project.txt。這裡 proguard-project.txt檔案用於對前面的 基本的混淆配置檔案 proguard-android.txt的配置進行override和新增。 混淆的基本配置檔案 proguard-android.txt 如下: 檔案1
 
   

# This is a configuration file for ProGuard.# http://proguard.sourceforge.net/index.html#manual/usage.html-dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -verbose # Optimization is turned off by default. Dex does not like code run# through the ProGuard optimize and preverify steps (and performs some# of these optimizations on its own).-dontoptimize -dontpreverify # Note that if you want to enable optimization, you cannot just# include optimization flags in your own project configuration file;# instead you will need to point to the# "proguard-android-optimize.txt" file instead of this one from your# project.properties file.-keepattributes *Annotation*-keep publicclass com.google.vending.licensing.ILicensingService-keep publicclass com.android.vending.licensing.ILicensingService# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native-keepclasseswithmembernames class*{native<methods>;}# keep setters in Views so that animations can still work.# see http://proguard.sourceforge.net/manual/examples.html#beans-keepclassmembers publicclass*extends android.view.View{voidset*(***);***get*();}# We want to keep methods in Activity that could be used in the XML attribute onClick-keepclassmembers class*extends android.app.Activity{publicvoid*(android.view.View);}# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations-keepclassmembers enum*{publicstatic**[] values();publicstatic** valueOf(java.lang.String);}-keep class*implements android.os.Parcelable{publicstaticfinal android.os.Parcelable$Creator*;}-keepclassmembers class**.R$*{publicstatic<fields>;}# The support library contains references to newer platform versions.# Don't warn about those in case this app is linking against an older# platform version. We know about them, and they are safe.-dontwarn android.support.**

以下則個是我們專案 混淆的個性化配置檔案 proguard-project.txt
 
   

# To enable ProGuard in your project, edit project.properties# to define the proguard.config property as described in that file.## Add project specific ProGuard rules here.# By default, the flags in this file are appended to flags specified# in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the ProGuard# include property in project.properties.## For more details, see# http://developer.android.com/guide/developing/tools/proguard.html# Add any project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {# public *;#}-dontwarn android.**-dontwarn edu.edut.lsf.payment.link.**-libraryjars ..\Download_Install\lib\classes.jar -keep class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil-keep class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil$Node-keep class org.jboss.netty.util.internal.LinkedTransferQueue$Node-keep class edu.edut.robin.activities.LeWebJsActivity$AppStoreInterface-keepclasseswithmembers class*{publicstaticvoid main(java.lang.String[]);}-keepclasseswithmembers class org.jboss.netty.util.internal.AtomicFieldUpdaterUtil$Node{*;}-keepclasseswithmembers class edu.edut.robin.activities.LeWebActionActivity$AppstoreWebInterface{*;}-keepclasseswithmembers class edu.edut.robin.utils.SilentInstallAssistant$*{*;}-keepclasseswithmembers class edu.edut.robin.silentinstaller.utils.SilentInstallAssistant$*{*;}-keepclasseswithmembers class edu.edut.robin.utils.Pm$*{*;}-keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue{volatiletransient org.jboss.netty.util.internal.LinkedTransferQueue$Node head;volatiletransient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail;volatiletransientint sweepVotes;}-keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue$Node{*;}-keepclasseswithmembers class edu.edut.robin.activities.LeWebJsActivity$AppStoreInterface{*;}-keepclasseswithmembers class*extends edu.edut.lsf.payment.WebSubmitInterface{*;}-keepclasseswithmembers class edu.edut.lsf.payment.WebSubmitInterface{*;}-keep publicclass com.unionpay.**{*;}-keep publicclass edu.edut.lsf.**{*;}

:由於牽扯到保密的問題,一些關於專案的東西換成了edu.edut或edu.edut.robin 三、混淆配置詳解 另外以下是關於混淆配置檔案的一些說明: - injars  androidtest.jar【jar包所在地址】  - outjars  out【輸出地址】 - libraryjars    'D:\android-sdk-windows\platforms\android-9\android.jar' 【引用的庫的jar,用於解析injars所指定的jar類】   - optimizationpasses 5 - dontusemixedcaseclassnames 【混淆時不會產生形形色色的類名 】 puzzle - dontskipnonpubliclibraryclasses 【指定不去忽略非公共的庫類。 】 puzzle - dontpreverify 【不預校驗】 - verbose - optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 【優化】 puzzle - keep public class * extends android.app.Activity  【不進行混淆類名的類,保持其原類名和包名】   - keep public abstract interface com.asqw.android.Listener{ public protected <methods>;  【所有public protected的方法名不進行混淆】 } - keep public class com.asqw.android{ public void Start(java.lang.String); 【對該方法不進行混淆】 } - keepclasseswithmembernames class * { 【對所有類的native方法名不進行混淆】 native <methods>; } - keepclasseswithmembers class * { 【 對所有類的指定方法的方法名不進行混淆】 public <init>(android.content.Context, android.util.AttributeSet); } - keepclassmembers class * extends android.app.Activity {【 對所有類的指定方法的方法名不進行混淆】 public void *(android.view.View); } - keepclassmembers enum * { 對列舉型別enum的所有類的以下指定方法的方法名不進行混淆 public static **[] values(); public static ** valueOf(java.lang.String); } - keep class * implements android.os.Parcelable {【對實現了 Parcelable介面的所有類的類名不進行混淆,對其成員變數為 Parcelable$Creator型別的成員變數的變數名不進行混淆 public static final android.os.Parcelable$Creator *; } -keepclasseswithmembers class org.jboss.netty.util.internal.LinkedTransferQueue { 對指定類的指定變數的變數名不進行混淆 volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node head; volatile transient org.jboss.netty.util.internal.LinkedTransferQueue$Node tail; volatile transient int sweepVotes;} -keep public class com.unionpay.** {*; }【對 com.unionpay 包下所有的類都不進行混淆,即不混淆類名,也不混淆方法名和變數名】 結束!  

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://www.cnblogs.com/captainbed