Android程式碼混淆

yangxi_001發表於2013-11-15

這是在工程中的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 *;
#}

 
從指令碼中可以看到,混淆中保留了繼承自Activity、Service、Application、BroadcastReceiver、ContentProvider等基本元件以及com.android.vending.licensing.ILicensingService,
並保留了所有的Native變數名及類名,所有類中部分以設定了固定引數格式的建構函式,列舉等等。(詳細資訊請參考<proguard_path>/examples中的例子及註釋。)
讓proguard.cfg起作用的做法很簡單,就是在eclipse自動生成的default.properties檔案中加上一句“proguard.config=proguard.cfg”就可以了
完整的default.properties檔案應該如下:
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-15
proguard.config=proguard.cfg


Android程式碼混淆,如何過濾掉反射的R檔案及第三方包?
解決方案:在Proguard.cfg方件中新增以下設定:

  • 過濾R檔案的混淆:

-keep class **.R$* {   *;  }

  • 過濾第三方包的混淆:

-keep class packagename.** {*;}(其中packagename為第三方包的包名)

Android匯入第三方jar包,proguard混淆指令碼(遮蔽警告,不混淆第三方包)
最近1個專案中 需要匯入移動MM的第三方計費包,混淆時用到了如下指令碼,可遮蔽警告,不混淆第三方包指定內容。
非常有效

proguard.cfg 檔案

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-ignorewarnings //這1句是遮蔽警告,指令碼中把這行註釋去掉
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
//這1句是匯入第三方的類庫,防止混淆時候讀取包內容出錯,指令碼中把這行註釋去掉
-libraryjars libs/mmbilling.jar 

-dontwarn  //dontwarn去掉警告
-dontskipnonpubliclibraryclassmembers

-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
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

//這4句是不混淆第三方包中的指定內容,指令碼中把這行註釋去掉 -keep class com.ccit.** {*; }   
-keep class ccit.** { *; }
-keep class com.aspire.**
-keep class mm.vending.**

相關文章