Android 代碼混淆及第三方jar包不被混淆

來源:互聯網
上載者:User

為了保護代碼被反編譯,android引入了混淆代碼的概念

1.設定混淆
在工程下找到project.properties檔案

在檔案中加入proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt這個是系統的

也可以用自己的混淆檔案(這樣就可以配置一些自己的東西),去sdk.dir}/tools/proguard/ 下複製proguard-android.txt檔案到本地工程中

然後設定成proguard.config=proguard-android.txt


[java] #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 
proguard.config=proguard-android.txt 
 
# Project target. 
target=android-17 

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

# Project target.
target=android-17

2.配置自己的混淆檔案proguard-android.txt

為瞭解決第三方包不被混淆,第三方包在混淆後,啟動並執行時候會掛掉。我的錯誤是java.lang.ExceptionInInitializerError


[java]  E/AndroidRuntime( 9608): java.lang.ExceptionInInitializerError 
E/AndroidRuntime( 9608):        at a.a.b.f.<init>(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.b.e.<init>(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.c.dg.b(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.c.dg.a(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.c.b.a(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.c.ad.a(Unknown Source) 
E/AndroidRuntime( 9608):        at a.a.a.a(Unknown Source) 
E/AndroidRuntime( 9608):        at com.petsea.c.a(Unknown Source) 
E/AndroidRuntime( 9608):        at com.petsea.c.a(Unknown Source) 
E/AndroidRuntime( 9608):        at com.petsea.h.a(Unknown Source) 
E/AndroidRuntime( 9608):        at com.petsea.h.onPostExecute(Unknown Source) 
E/AndroidRuntime( 9608):        at android.os.AsyncTask.finish(AsyncTask.java:631) 
E/AndroidRuntime( 9608):        at android.os.AsyncTask.access$600(AsyncTask.java:177) 
E/AndroidRuntime( 9608):        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
E/AndroidRuntime( 9608):        at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 9608):        at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime( 9608):        at android.app.ActivityThread.main(ActivityThread.java:5041) 
E/AndroidRuntime( 9608):        at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 9608):        at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime( 9608):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
E/AndroidRuntime( 9608):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
E/AndroidRuntime( 9608):        at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime( 9608): Caused by: java.lang.ExceptionInInitializerError 
E/AndroidRuntime( 9608):        at a.a.b.l.<clinit>(Unknown Source) 
E/AndroidRuntime( 9608):        ... 22 more 

E/AndroidRuntime( 9608): java.lang.ExceptionInInitializerError
E/AndroidRuntime( 9608):        at a.a.b.f.<init>(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.b.e.<init>(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.c.dg.b(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.c.dg.a(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.c.b.a(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.c.ad.a(Unknown Source)
E/AndroidRuntime( 9608):        at a.a.a.a(Unknown Source)
E/AndroidRuntime( 9608):        at com.petsea.c.a(Unknown Source)
E/AndroidRuntime( 9608):        at com.petsea.c.a(Unknown Source)
E/AndroidRuntime( 9608):        at com.petsea.h.a(Unknown Source)
E/AndroidRuntime( 9608):        at com.petsea.h.onPostExecute(Unknown Source)
E/AndroidRuntime( 9608):        at android.os.AsyncTask.finish(AsyncTask.java:631)
E/AndroidRuntime( 9608):        at android.os.AsyncTask.access$600(AsyncTask.java:177)
E/AndroidRuntime( 9608):        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
E/AndroidRuntime( 9608):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 9608):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 9608):        at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime( 9608):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9608):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 9608):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime( 9608):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime( 9608):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 9608): Caused by: java.lang.ExceptionInInitializerError
E/AndroidRuntime( 9608):        at a.a.b.l.<clinit>(Unknown Source)
E/AndroidRuntime( 9608):        ... 22 more
最終我通過 加LOG的調試方法定位到是由於第三方jar包被混淆後的原因導致的。

3解決方案:
在proguard-android.txt檔案最後加入了-keep class org.jsoup.**這樣一句代碼,就是保持這個類不被混淆

附上proguard-android.txt源檔案

 

[html] # 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 public class com.google.vending.licensing.ILicensingService 
-keep public class 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 public class * extends android.view.View { 
   void set*(***); 
   *** get*(); 

 
# We want to keep methods in Activity that could be used in the XML attribute onClick 
-keepclassmembers class * extends android.app.Activity { 
   public void *(android.view.View); 

 
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations 
-keepclassmembers enum * { 
    public static **[] values(); 
    public static ** valueOf(java.lang.String); 

 
-keep class * implements android.os.Parcelable { 
  public static final android.os.Parcelable$Creator *; 

 
-keepclassmembers class **.R$* { 
    public static <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.** 
 
-keep class org.jsoup.** 

# 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 public class com.google.vending.licensing.ILicensingService
-keep public class 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 public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <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.**

-keep class org.jsoup.**

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.