Android code obfuscation and third-party jar packages are not obfuscated

Source: Internet
Author: User
Tags xml attribute

To prevent code decompilation, android introduces the concept of obfuscation code.

1. Set Obfuscation
Find the project. properties file under the project.

Add proguard. config =$ {sdk. dir}/tools/proguard/proguard-android.txt in the file: proguard-project.txt this is the system

You can also use your own obfuscation file (so that you can configure some of your own items) to copy the proguard-android.txt file to the local project under sdk. dir}/tools/proguard /.

Then set it to proguard.configpolicproguard-android.txt.


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

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

# Project target.
Target = android-17

2.configure the corresponding profile proguard-android.txt

To prevent third-party packages from being obfuscated, the third-party packages will be suspended during running after being obfuscated. My error is 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 (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 (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
In the end, I found through the debugging method of adding LOG that it was caused by the obfuscation of Third-Party jar packages.

3. solution:
At the end of the proguard-android.txt file, the-keep class org. jsoup. ** code is added to ensure that this class is not obfuscated.

Attached proguard-android.txt source file

 

[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
# "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 cocould 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
# "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 cocould 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 .**

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.