Android studio jar 生成与混淆

一、前言

最近在这家公司,又要搞被安排搞Android 开发,许久没有碰过Android ,又要重新拾起来,这次需要调试底层,并且封装sdk 供给客户进行二次开发,并且老板要求不能让客户知道具体细节,因此,只能对封装好的jar 进行混淆。因此,写下这边文章,进行记录。

二、开发环境

2.1 开发工具 Android studio 4.1.x

 2.3 JDK 编译环境

 

3. 在app的 模块的build.gradle 在末尾加上下述代码

用来生成没有混淆的 jar

 

task makeJar(type: Jar) {
//指定生成的jar名
    baseName 'RFlib'
    //从哪里打包class文件,根据你的AS版本会所有不同
    //但是一定要能在此路径下可以找得到自己写的类
    //如果你封装的jar包用起来有问题,很可能是此处出错
    from('build/intermediates/javac/release/classes/')
    from fileTree(dir: 'src/main', includes: ['jniLibs/**'])
    //去掉不需要打包的目录和文件
    exclude('test/','BuildConfig.class','R.class')
    //去掉R$开头的文件
    exclude{ it.name.startsWith('R$') }
}

makeJar.dependsOn(build)

鼠标点击 红色圈中,生成的jar位于左边去圈中的位置

 

4. 生成混淆 jar

4.1 在build.gradle 同一个目录下,创建 proguard-rules.pro 文件

复制下面,文本

###########################以下是AndroidStudio自带的混淆配置协议###############################
# 表示混淆时不使用大小写混合类名
-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
#指定不混淆所有的JNI方法
#-keepclasseswithmembernames class * { native ; }

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
#所有View的子类及其子类的get、set方法都不进行混淆
-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
# 不混淆Activity中参数类型为View的所有方法
#-keepclassmembers class * extends android.app.Activity { public void *(android.view.View); }

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
# 不混淆Enum类型的指定方法
-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }

# 不混淆Parcelable和它的子类,还有Creator成员变量
-keepclassmembers class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator CREATOR; }

# 不混淆R类里及其所有内部static类中的所有static变量字段
#-keepclassmembers class **.R$* { public static; }

# 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>(...);
}


#################################以下是自己添加的混淆协议###################################
#下面代码中的路径配置,你要修改成与你相对应的路径
#引入依赖包rt.jar(jdk路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉) -libraryjars &
#39;C:\Android_Develop_Tools\Java\jdk1.8.0_101\jre\lib\rt.jar&#39;

#引入依赖包android.jar(android SDK路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)
#-libraryjars &#39;C:\Android_Develop_Tools\sdk\platforms\android-23\android.jar&#39;

#如果用到Appcompat包,需要引入(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)
#-libraryjars &#39;D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.4.0\jars\classes.jar&#39;
#-libraryjars &#39;D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\support-v4\23.4.0\jars\classes.jar&#39;
#忽略警告 -ignorewarnings #保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
#-dontshrink #保护泛型 -keepattributes Signature  -keep class com.lcw.mylibrary.LogUtil{ public *; }

#引入依赖包rt.jar(jdk路径)
-libraryjars 'C:\Program Files\Java\jdk1.8.0_331\jre\lib\rt.jar'
#引入依赖包android.jar(android SDK路径)
-libraryjars 'D:\AndroidSDK\platforms\android-32\android.jar'
#如果用到其他包,需要引入

#忽略警告
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
#保护泛型
-keepattributes Signature

#自己不想混淆的配置,保证com下的类名不被混淆
#-keep class android.serialport.**{*;}

#简单讲讲,这个其实就是告诉编译器com.csdn.info包下的所以文件不要混淆,还可以这样写

#这个是告诉编译器不要混淆com.csdn.info包下的所以文件的public变量和函数。
-keep class com.octp.senselessborrowrflibrary.OctpRFInterface{
    public <fields>;
    public <methods>;
}

-keep class com.octp.senselessborrowrflibrary.callBack.MessageCallBack{
    public <fields>;
    public <methods>;
}

修改需要的部分。

在build.gradle 文件输入这个

task proguard(type: proguard.gradle.ProGuardTask, dependsOn: makeJar) {
//  输入路径
    injars "build/libs/RFlib.jar"

//  输出路径
    outjars 'libs/RFlib.jar'

//  添加配置信息
    configuration 'proguard-rules.pro'
}
task proguard(type: proguard.gradle.ProGuardTask, dependsOn: makeJar) {
//  输入路径
    injars "build/libs/RFlib.jar"

//  输出路径
    outjars 'libs/RFlib.jar'

//  添加配置信息
    configuration 'proguard-rules.pro'
}

 输出的混淆文件

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值