0. 配置标准
# 保持 Main 类内的 public 属性相同
-keep class xxx.Main {
public *;
}
# private void sensorRegisterListener(android.content.Context, android.hardware.SensorManager, android.hardware.Sensor);
# 对应 native
-keepclasseswithmembernames class xxx.Main {
native <methods>;
}
# 保持对应的 -keepclassmembernames
-keepclassmembers class xxx.Main$* {
public *;
}
-keep public class * extends android.app.Service
-keep public class xxx.BuildConfig
#引入依赖包rt.jar(jdk路径) ,, 需要配置
-libraryjars 'D:\ni\studio\jre\jre\lib\rt.jar'
#引入依赖包android.jar(android SDK 路径) ,, 在 gradle 中就已经配置了,这里不需要再次设置
#-libraryjars 'D:\sdk\platforms\android-28\android.jar'
#忽略警告 中间会后找不到 class 的警告,然后抛出错误,需添加这个
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
1. 输出混淆了的 jar 包
1. build.gradle 脚本
- 生成 jar 包
def SDK_BASENAME = "raw_du";
def SDK_VERSION = "";
def sdkDestinationPath = "build";//生成jar存放目录
//打包release目录下的classes.jar(开启混淆后这个jar就是混淆的源码)----注意由于as版本原因有的在default目录
def releaseJar = file('build/intermediates/intermediate-jars/release/classes.jar')
//打包jar前先删除原先的jar
task deleteBuild(type: Delete) {
delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
}
task makeJar(type: Jar) {
from zipTree(releaseJar)//导出混淆的jar
//from zipTree(debugJar)//导出未混淆的jar
//from zipTree(thirdPartyJar)将第三方jar包打入jar包
//from fileTree(dir: 'src/main', includes: ['assets/**'])//将assets目录打入jar包
exclude('**/BuildConfig.class')//排除不必要的class文件
baseName = SDK_BASENAME + SDK_VERSION
destinationDir = file(sdkDestinationPath)
}
makeJar.dependsOn(deleteBuild, build)
上面这个就是生成了 jar 文件,对应 build/raw_du.jar 目录
- 生成混淆了的 jar 包
task makeProguardJar(type: proguard.gradle.ProGuardTask, dependsOn: ['makeJar']) {
// 未混淆的jar路径
injars 'build/raw_du.jar'
// 混淆后的jar输出路径
outjars 'build/proGuard_du.jar'
// 混淆协议
configuration 'proguard-rules.pro'
}
直接生成了 build/proGuard_du.jar 混淆后的文件
- 生成混淆了的 jar 包
task makePPPJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
//删除已有的jar包
delete 'build/proGuard_du.jar'
// 未混淆的jar路径
injars 'build/intermediates/intermediate-jars/release/classes.jar'
// 混淆后的jar输出路径
outjars 'build/proGuard_du.jar'
// 混淆协议
configuration 'proguard-rules.pro'
}
这个 gradle 命令,对应的混淆配置是 proguard-rules.pro
模版如下
#表示混淆时不使用大小写混合类名
-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>(...);
}
# 保持 Main 类内的 public 属性相同
-keep class ***.Main {
public *;
}
# 对应 native
-keepclasseswithmembernames class ***.Main {
native <methods>;
}
-keep public class * extends android.app.Service
# 如果有这个类,就保持
-keep class com.kok.http.BuildConfig{
public *;
}
#引入依赖包rt.jar(jdk路径) ,, 需要配置
-libraryjars 'D:\ni\studio\jre\jre\lib\rt.jar'
#引入依赖包android.jar(android SDK路径) ,, 在 gradle 中就已经配置了,这里不需要再次设置
#-libraryjars 'D:\sdk\platforms\android-28\android.jar'
#忽略警告 中间会后找不到 class 的警告,然后抛出错误,需添加这个
-ignorewarnings
#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
-dontshrink
2. 操作
双击执行 gradle 栏目的 core/Tasks/other/makeProguardJar 条目
网友评论