参考博文:http://www.jianshu.com/p/0a3ce6e9ab85
开展项目合作时,基于模块化思想,对方要用到你的程序,而你又不想将源代码给对方,通常会将程序进行打包生成jar,并作混淆处理。
1、创建项目
【File】——【New Module】——【Android Library】,命名,然后编辑代码。
二、编辑library路径下的build.gradle
AS中进行代码混淆需要在build.gradle文件和proguard-rules.pro文件中进行设置(可以通过jd-gui工具对比混淆前后效果):
1、配置build.gradle文件
你的library库下打开build.gradle文件,在末尾加上下述代码。
task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {说明:(1)AS会自动对library进行打包,即build/intermediates/bundles/release/classes.jar,只是未进行混淆工作而已;
//删除已有的jar包
delete 'build/outputs/ips.jar'
// 未混淆的jar路径
injars 'build/intermediates/bundles/release/classes.jar'
// 混淆后的jar输出路径
outjars 'build/outputs/ips.jar'
// 混淆协议
configuration 'proguard-rules.pro'
}
(2)在delete和injars中的ips.jar是你要打包的jar包名字。
2、配置proguard-rules.pro文件
(1)把AS自带的协议配置进来
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer
# versions are distributed with the plugin and unpacked at build time. Files in this directory are
# no longer maintained.
#表示混淆时不使用大小写混合类名
-dontusemixedcaseclassnames
#表示不跳过library中的非public的类
-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
##表示不进行校验,这个校验作用 在java平台上的
-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);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator 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.**
# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}
参考http://www.jianshu.com/p/0a3ce6e9ab85。
(2)引入依赖包路径
#引入依赖包rt.jar(jdk路径)(3)加入自己不想混淆的配置
-libraryjars 'D:\Android_Studio\Android_Studio_Install\jre\jre\lib\rt.jar'
#引入依赖包android.jar(android SDK路径)
-libraryjars 'D:\Android_Studio\Android_SDK\platforms\android-25\android.jar'
#如果用到其他包,需要引入
#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
#保护泛型
-keepattributes Signature
这个根据实际情况选择性配置
#自己不想混淆的配置,保证com下的类名不被混淆看下添加(3)和不添加(3)的效果
-keep class ips.casm.com.**{public *;}
不混淆 混淆
3、执行打包命令
在Terminal 窗口输入下面代码:
gradlew makeJar提示BUILD SUCCESSFUL表示打包成功!
说明:上面的命令是在windows下,而在mac下则需要输入
./gradlew makeJar
4、jar包路径
ipslibrary(你的library)/build/outputs/ips.jar(你的jar包名称)
三、用jd_gui或导入到AS查看jar打包效果