最近公司项目需要AS混淆打jar,在百度上搜索“AS打jar包并混淆”,一下出来好多面,试了很多网上的方法几乎都出问题,最后根据其中一种自己做了修改,结果打jar并混淆成功,现在将成果分享给大家,希望能帮助大家。直接上代码
1.在你的build.gradle文件中加入代码
<span style="font-size: 16px;">import com.android.build.gradle.AppPlugin
import proguard.gradle.ProGuardTask
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.test.myapp"
minSdkVersion 14
targetSdkVersion 18
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/android-core-3.1.1.jar')
compile files('libs/core-3.1.1.jar')
}
//dependsOn 可根据实际需要增加或更改
task buildJar(dependsOn: ['compileReleaseJavaWithJavac'], type: Jar) {
// appendix = "demo"
baseName = "MDCCode"
version = "2.2.0"
// classifier = "release"
//后缀名
extension = "jar"
//最终的 Jar 包名,如果没设置,默认为 [baseName]-[appendix]-[version]-[classifier].[extension]
// archiveName = "MDCCode.jar"
//需打包的资源所在的路径集
def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
//初始化资源路径集
from srcClassDir
//去除路径集下部分的资源
exclude "com/topcode/mdccodesdk/MainActivity.class"
exclude "com/topcode/mdccodesdk/MainActivity\$*.class"
exclude "com/topcode/mdccodesdk/BuildConfig.class"
exclude "com/topcode/mdccodesdk/BuildConfig\$*.class"
exclude "**/R.class"
exclude "**/R\$*.class"
//只导入资源路径集下的部分资源
include "com/**/*.class"
//注: exclude include 支持可变长参数
}
task proguardJar(dependsOn: ['buildJar'], type: ProGuardTask) {
//Android 默认的 proguard 文件
configuration android.getDefaultProguardFile('proguard-android.txt')
//会根据该文件对 Jar 进行混淆,注意:需要在 manifest 注册的组件也要加入该文件中
configuration 'proguard-rules.pro'
String inJar = buildJar.archivePath.getAbsolutePath()
//输入 jar
injars inJar
//输出 jar
outjars inJar.substring(0, inJar.lastIndexOf(File.separator)) + "/P${buildJar.archiveName}"
//设置不删除未引用的资源(类,方法等)
dontshrink
AppPlugin appPlugin = getPlugins().findPlugin(AppPlugin)
if (appPlugin != null) {
List<String> runtimeJarList
if (appPlugin.getMetaClass().getMetaMethod("getRuntimeJarList")) {
runtimeJarList = appPlugin.getRuntimeJarList()
} else if (android.getMetaClass().getMetaMethod("getBootClasspath")) {
runtimeJarList = android.getBootClasspath()
} else {
runtimeJarList = appPlugin.getBootClasspath()
}
for (String runtimeJar : runtimeJarList) {
//给 proguard 添加 runtime
libraryjars(runtimeJar)
}
}
}
</span><span style="font-size:18px;">
</span>
<span style="font-size:18px;"></span><pre style="color: rgb(51, 51, 51); line-height: 27.2px; text-indent: 16px; font-family: Consolas; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">minifyEnabled一定要设计为true</span>
2.在proguard-rules.pro文件中加入代码
# Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in D:\Android\sdk\sdk-as\sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # 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 *; #} -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } -keep interface com.test.callback.DecodeCallback { *; } -keep public class com.test.mdccode.Mde { *; } -keep public class com.test.mdccode.scanner.MScanner { *; } -keep public class com.test.mdccode.scanner.decode.BitmapDecoder { *; } -libraryjars libs/core-3.1.1.jar -libraryjars libs/android-core-3.1.1.jar
在你的Terminal中执行
<pre name="code" class="html">gradlew<span style="font-family: Consolas; line-height: 27.2px;"> buildJar 这是打包不混淆的jar包</span>
<span style="font-family: Consolas; line-height: 27.2px;"></span><pre name="code" class="html" style="color: rgb(51, 51, 51); line-height: 27.2px; text-indent: 16px;">gradlew<span style="font-family: Consolas; line-height: 27.2px;"> </span><span style="font-family: Consolas; line-height: 27.2px; background-color: rgb(255, 255, 255);">proguardJar 打包代混淆的jar包</span>
以上是我的所有代码,希望可以帮助大家,如果有什么问题,可以留言给我,我会全力回答大家的问题,谢谢