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

为了保护代码被反编译,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.**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值