Java代码编译成二进制class 文件,这个class 文件也可以反编译成源代码 ,除了注释外,原来的code 基本都可以看到。为了防止重要code 被泄露,我们往往需要混淆(Obfuscation code , 也就是把方法,字段,包和类这些java 元素的名称改成无意义的名称,这样代码结构没有变化,还可以运行,但是想弄懂代码的架构却很难。
一、项目混淆过程中注意事项:
我的新建android项目下只有proguard-project.txt和project.properties这两个文件夹,而网上一些所谓混淆的方法不少是proguard.config和project.properties,以下是一些混淆总结:
1、如果是proguard.config和project.properties这两个文件夹的则为:
proguard.config=proguard.cfg
如果是proguard-project.txt和project.properties这两个文件夹的则为:
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt(当然视您生成项目时候该文件具体生成情况所定)。
2、如果有第三方libs包的话,则混淆时需要注意了,以下是常用的一些libs包的混淆配置:
1)、友盟sdk:
-libraryjars libs/umeng_sdk.jar
-keepclassmembers class * {
public (org.json.JSONObject);
}
-keep public class com.smile.android.R$*{
public static final int ;
}
2)、gson
-ibraryjars libs/gson-2.2.2.jar
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.* { ; }
-keep class com.google.gson.examples.android.model.* { ; }
-keep class com.google.gson.* { *;}
3)、support-v4包
-libraryjars libs/android-support-v4.jar
-dontwarn android.support.v4.**
-keep class android.support.v4.** { ; }
4)、nineoldandroids动画lib包
-libraryjars libs/nineoldandroids-2.4.0.jar
-dontwarn com.nineoldandroids.*
-keep class com.nineoldandroids.** { *;}
注解:
-braryjars libs/nineoldandroids-2.4.0.jar----指明libs包的在工程中的路径
而-dontwarn com.xx.bbb.**和-keep class com.xx.bbb.** { ;}
这两个参数用来保持第三方库中的类而不乱,将-dontwarn和-keep 结合使用,意思是保持com.xx.bbb.*这个包里面的所有类和所有方法而不混淆,接着还叫ProGuard不要警告找不到com.xx.bbb.**这个包里面的类的相关引用。
project.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 edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-10
我的加混淆的project.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 edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-10
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 去掉前面的注解符#
proguard-project.txt源文件
-optimizationpasses 5->设置混淆的压缩比率 0 ~ 7
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses->如果应用程序引入的有jar包,并且想混淆jar包里面的class
-dontpreverify
-verbose->混淆后生产映射文件 map 类名->转化后类名的映射
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*->混淆采用的算法.
-keep public class * extends android.app.Activity->所有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>;-> 所有native的方法不能去混淆.
}
-keepclasseswithmembers class * {-->某些构造方法不能去混淆
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {-> 枚举类不能去混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {-> aidl文件不能去混淆.
public static final android.os.Parcelable$Creator *;
}
我的加混淆的
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 *;
#}
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-dontoptimize
-ignorewarnings
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keepattributes Signature
-dontskipnonpubliclibraryclassmembers
-dontskipnonpubliclibraryclasses
-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>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keep public class com.smile.android.R$*{
public static final *;
}
-libraryjars libs/android-support-v4.jar
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-libraryjars libs/alipaysdk.jar
-dontwarn com.alipay.**
-keep class com.alipay.** { *; }
-libraryjars libs/alipaysecsdk.jar
-dontwarn com.alipay.mobilesecuritysdk.**
-keep class com.alipay.mobilesecuritysdk.** { *; }
-dontwarn HttpUtils.**
-keep class HttpUtils.** { *; }
-libraryjars libs/alipayutdid.jar
-dontwarn com.**
-keep class com.** { *; }
-libraryjars libs/universal-image-loader-1.9.1-with-sources.jar
-dontwarn com.nostra13.universalimageloader.**
-keep class com.nostra13.universalimageloader.** { *; }
然后打包就ok了
这是简单的,凡事往往没想象的简单,具体问题遇到再说吧。