Android obfuscation code details and examples

Source: Internet
Author: User

Android obfuscation code details and examples

Android obfuscation code details and examples

To prevent your work results from being stolen by others, obfuscation code can effectively prevent decompilation. The following describes the steps for obfuscation of code:

1. You may also see a new project with a proguard-project.txt file under the project. This is very important for obfuscation code. If you accidentally delete it, it doesn't matter. Copy one from other places.

2. The most important statement is to add a obfuscation statement in proguard-project.txt:

A. Declare all your jar packages. For example:

-Libraryjars libs/apns_1.0.6.jar

-Libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so

-Libraryjars libs/armeabi/liblocSDK4.so

-Libraryjars libs/baidumapapi_v2_3_1.jar

-Libraryjars libs/core. jar

-Libraryjars libs/gesture-imageview.jar

-Libraryjars libs/gson-2.0.jar

-Libraryjars libs/infogracesound. jar

-Libraryjars libs/locSDK_4.0.jar

-Libraryjars libs/ormlite-android-4.48.jar

-Libraryjars libs/ormlite-core-4.48.jar

-Libraryjars libs/universal-image-loader-1.9.0.jar

B. Declare the parts you do not need to be confused, because some classes will cause program compilation to fail, as shown below:

-Keep public class * extends android. app. Fragment

-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 * extends android. support. v4 .**

-Keep public class com. android. vending. licensing. ILicensingService

-- All of the above are classes in the API. It is best to avoid confusion.

For some special items, such as Baidu map, you need to add the following statement:

-Keep class com. baidu .**{*;}

-Keep class vi.com. gdi. bgl. android .**{*;}

Based on my experience, it is generally best to avoid confusion between models (model is irrelevant and does not matter much if it is not obfuscated), for example:

-Keep class com. bank. pingan. model .**{*;}

The following is a statement about Umeng sharing statistics to avoid confusion.

-Dontwarn android. support. v4 .**

-Dontwarn org.apache.commons.net .**

-Dontwarn com. tencent .**

-Keepclasseswithmembernames class *{

Native ;

}

-Keepclasseswithmembernames class *{

Public (Android. content. Context, android. util. AttributeSet );

}

-Keepclasseswithmembernames class *{

Public (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 *;

}

-Keepclasseswithmembers class *{

Public (Android. content. Context );

}

-Dontshrink

-Dontoptimize

-Dontwarn com. google. android. maps .**

-Dontwarn android. webkit. WebView

-Dontwarn com. umeng .**

-Dontwarn com. tencent. weibo. sdk .**

-Dontwarn com. facebook .**

-Keep enum com. facebook .**

-Keepattributes Exceptions, InnerClasses, Signature

-Keepattributes * Annotation *

-Keepattributes SourceFile, LineNumberTable

-Keep public interface com. facebook .**

-Keep public interface com. tencent .**

-Keep public interface com. umeng. socialize .**

-Keep public interface com. umeng. socialize. sensor .**

-Keep public interface com. umeng. scrshot .**

-Keep public class com. umeng. socialize .*{*;}

-Keep public class javax .**

-Keep public class android. webkit .**

-Keep class com. facebook .**

-Keep class com. umeng. scrshot .**

-Keep public class com. tencent .**{*;}

-Keep class com. umeng. socialize. sensor .**

-Keep class com. tencent. mm. sdk. openapi. WXMediaMessage {*;}

-Keep class com. tencent. mm. sdk. openapi. ** implements com. tencent. mm. sdk. openapi. WXMediaMessage $ IMediaObject {*;}

-Keep class im. yixin. sdk. api. YXMessage {*;}

-Keep class im. yixin. sdk. api. ** implements im. yixin. sdk. api. YXMessage $ YXMessageData {*;}

-Keep public class [your_pkg]. R$ *{

Public static final int *;

}

3. After the above work is completed, the obfuscation work is completed by more than half. The last thing to do is to add your obfuscation file declaration in the project. properties file, as shown below:

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

4. OK, In the hosts file

------- The above is the whole process of obfuscation code.

Paste all the code of proguard-project.txt to the end:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

# 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 *;

#}

-Optimizationpasses 5

-Dontusemixedcaseclassnames

-Dontskipnonpubliclibraryclasses

-Dontpreverify

-Verbose

-Optimizations! Code/simplification/arithmetic ,! Field /*,! Class/merging /*

 

-Keepattributes * Annotation *

-Keepattributes Signature

 

-Libraryjars libs/apns_1.0.6.jar

-Libraryjars libs/armeabi/libBaiduMapSDK_v2_3_1.so

-Libraryjars libs/armeabi/liblocSDK4.so

-Libraryjars libs/baidumapapi_v2_3_1.jar

-Libraryjars libs/core. jar

-Libraryjars libs/gesture-imageview.jar

-Libraryjars libs/gson-2.0.jar

-Libraryjars libs/infogracesound. jar

-Libraryjars libs/locSDK_4.0.jar

-Libraryjars libs/ormlite-android-4.48.jar

-Libraryjars libs/ormlite-core-4.48.jar

-Libraryjars libs/universal-image-loader-1.9.0.jar

 

-Keep class com. baidu .**{*;}

-Keep class vi.com. gdi. bgl. android .**{*;}

 

-Keep public class * extends android. app. Fragment

-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 * extends android. support. v4 .**

-Keep public class com. android. vending. licensing. ILicensingService

 

-Keep class com. google. gson. stream .**{*;}

-Keep class com. google. gson. examples. android. model .**{*;}

-Keep class com. uuhelper. Application .**{*;}

-Keep class net. sourceforge. zbar .**{*;}

-Keep class com. google. android. gms .**{*;}

 

-Keep class com. bank. pingan. model .**{*;}

 

-Keep public class * extends com. j256.ormlite. android. apptools. OrmLiteSqliteOpenHelper

-Keep public class * extends com. j256.ormlite. android. apptools. OpenHelperManager

 

-Keep class com. android. vending. licensing. ILicensingService

-Keep class android. support. v4 .**{*;}

-Keep class org.apache.commons.net .**{*;}

-Keep class com. tencent .**{*;}

 

-Keep class com. umeng .**{*;}

-Keep class com. umeng. analytics .**{*;}

-Keep class com. umeng. common .**{*;}

-Keep class com. umeng. newxp .**{*;}

 

-Keep class com. j256.ormlite .**{*;}

-Keep class com. j256.ormlite. android .**{*;}

-Keep class com. j256.ormlite. field .**{*;}

-Keep class com. j256.ormlite. stmt .**{*;}

 

-Dontwarn android. support. v4 .**

-Dontwarn org.apache.commons.net .**

-Dontwarn com. tencent .**

 

-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 *;

}

 

-Keepclasseswithmembers class *{

Public <init> (android. content. Context );

}

 

-Dontshrink

-Dontoptimize

-Dontwarn com. google. android. maps .**

-Dontwarn android. webkit. WebView

-Dontwarn com. umeng .**

-Dontwarn com. tencent. weibo. sdk .**

-Dontwarn com. facebook .**

 

-Keep enum com. facebook .**

-Keepattributes Exceptions, InnerClasses, Signature

-Keepattributes * Annotation *

-Keepattributes SourceFile, LineNumberTable

 

-Keep public interface com. facebook .**

-Keep public interface com. tencent .**

-Keep public interface com. umeng. socialize .**

-Keep public interface com. umeng. socialize. sensor .**

-Keep public interface com. umeng. scrshot .**

 

-Keep public class com. umeng. socialize .*{*;}

-Keep public class javax .**

-Keep public class android. webkit .**

 

-Keep class com. facebook .**

-Keep class com. umeng. scrshot .**

-Keep public class com. tencent .**{*;}

-Keep class com. umeng. socialize. sensor .**

 

-Keep class com. tencent. mm. sdk. openapi. WXMediaMessage {*;}

 

-Keep class com. tencent. mm. sdk. openapi. ** implements com. tencent. mm. sdk. openapi. WXMediaMessage $ IMediaObject {*;}

 

-Keep class im. yixin. sdk. api. YXMessage {*;}

-Keep class im. yixin. sdk. api. ** implements im. yixin. sdk. api. YXMessage $ YXMessageData {*;}

 

-Keep public class [your_pkg]. R$ *{

Public static final int *;

}

Update

1. jar statement of the dependent project:

The following is an example of email sending:

-Libraryjars ../KlowerBase/libs/mail-activation.jar

-Libraryjars ../KlowerBase/libs/mail-additionnal.jar

-Libraryjars ../KlowerBase/libs/mail. jar

2. Fixed an exception when sending the email.

?

1

2

3

4

5

-Keep class javax. mail .**{*;}

-Keep class com. sun. mail .**{*;}

-Keep class javax. activation .**{*;}

-Keep class org. apache. harmony .**{*;}

-Keep class java. security. ** {*;} <br>

?

1

2

3

4

5

# Solution: can't find referenced class java. beans. Beans

-Dontwarn org. apache. commons. collections. BeanMap

-Dontwarn java. beans .**

# Solution: can't find referenced class javax. security. sasl. Sasl

-Dontwarn com. sun. mail. imap. protocol .**

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.