一墨辛。在as中新建module如下
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
//gradle sdk
compile gradleApi()
//groovy sdk
compile localGroovy()
compile "com.android.tools.build:gradle:$localBuildToolsVersion"
compile 'com.android.tools.build:transform-api:1.5.0'
compile 'org.aspectj:aspectjrt:1.8.10'
}
repositories {
mavenCentral()
}
//以上都為固定寫法
//group和version
group='com.col.decypt_fixer'
version='1.0.0'
//打包到本地或者遠(yuǎn)程Maven庫
uploadArchives {
repositories {
mavenDeployer {
//提交到遠(yuǎn)程服務(wù)器:
// repository(url: "http://www.xxx.com/repos") {
// authentication(userName: "admin", password: "admin")
// }
//本地的Maven地址設(shè)置為E:/Maven
repository(url: "file:../aop-tech/repositories")
}
}
}
com.col.decypt_fixer.properties里面的
implementation-class=com.col.decypt_fixer.LibChanger能跳轉(zhuǎn)才說明結(jié)構(gòu)正確。
插件作用悔据,在transformNativeLibsWithStripDebugSymbolForApiTestRelease之后智蝠,packageApiTestRelease之前睛榄,替換.so庫馅闽。
Task :app:transformNativeLibsWithStripDebugSymbolForApiTestRelease
Task :app:validateSigningApiTestRelease
Task :app:packageApiTestRelease
class LibChanger implements Plugin<Project> {
@Override
void apply(Project project) {
println("Plugin Start_________________________________________________________________")
if (project.plugins.hasPlugin(AppPlugin)) {
AppExtension android = project.extensions.getByType(AppExtension)
// android.registerTransform(new LibChangerTransform(project))
//創(chuàng)建一個(gè)Extension丧慈,名字叫做testCreatJavaConfig 里面可配置的屬性參照MyPlguinTestClass
project.extensions.create("libChanger", MyPluginTestClass)
android.applicationVariants.all { variant -> //設(shè)置task依賴于生成BuildConfig的task,然后在生成BuildConfig后生成我們的類
//獲取到scope,作用域
def variantData = variant.variantData
def scope = variantData.scope
//拿到build.gradle中創(chuàng)建的Extension的值
def config = project.extensions.getByName("libChanger")
println("Plugin Start______${variantData}______________${scope}_____________________________________________")
if (!variantData.toString().contains("ApiTestRelease")) return
// println("Plugin Start______${variant.getVariantData().getTaskName("transform", "release")}______________________________________________________")
//創(chuàng)建一個(gè)task
def createTaskName = scope.getTaskName("col", "LibChangerPlugin")
def createTask = project.task(createTaskName)
File debugSoFile = null
String outPath = null
def oriTask = project.tasks.getByName("transformNativeLibsWithStripDebugSymbolForApiTestRelease")
println("Plugin Start______${oriTask}______________________________________________________")
// println("Plugin Start______${oriTask.inputs}_________________________${oriTask.outputs}_____________________________")
oriTask.inputs.each { input ->
println("Plugin Start______input: ${input.properties}______________________________________________________")
}
oriTask.inputs.files.each { file ->
println("Plugin Start______input: ${file.absolutePath}______________________________________________________")
if (!file.absolutePath.endsWith("armeabi-v7a" + File.separator + "libjni-lib.so")) return
debugSoFile = file
}
oriTask.outputs.files.each { file ->
println("Plugin Start______output: ${file.absolutePath}______________________________________________________")
outPath = file.absolutePath
}
//設(shè)置task要執(zhí)行的任務(wù)
createTask.doLast {
//生成java類
println("設(shè)置task要執(zhí)行的任務(wù)_____________________________________________")
def lastIndexOf = outPath.indexOf("reader_sh_as")
println("lastIndexOf---------------------------------------- " + lastIndexOf)
if (lastIndexOf == -1) return
def rootPath = outPath.substring(0, lastIndexOf + "project_name".length() + 1)
println("rootPath---------------------------------------- " + rootPath)
debugSoFile = new File(rootPath + File.separator
+ "jni_lib" + File.separator
+ "libjni-lib_debug.so"
)
if (outPath == null || outPath.length() == 0) return
if (debugSoFile == null) return
def releaseSoFile = new File(outPath + File.separator
+ "0" + File.separator
+ "lib" + File.separator
+ "armeabi-v7a" + File.separator
+ "libjni-lib.so")
println("directoryInput---------------------------------------- " + debugSoFile.exists() + " " + releaseSoFile.exists())
if (!debugSoFile.exists() || !releaseSoFile.exists()) return
println(debugSoFile.absolutePath + " -------------------\n--------------------- " + releaseSoFile.absolutePath)
println(" ---------------------------------------- " + debugSoFile.exists() + " " + releaseSoFile.exists())
println(" ---------------------------------------- " + FileUtils.checksumCRC32(debugSoFile) + " " + FileUtils.checksumCRC32(releaseSoFile))
FileUtils.copyFile(debugSoFile, releaseSoFile)
println(" ---------------------------------------- " + FileUtils.checksumCRC32(debugSoFile) + " " + FileUtils.checksumCRC32(releaseSoFile))
}
if (oriTask) {
createTask.dependsOn oriTask
oriTask.finalizedBy createTask
}
}
}
}
}
class MyPluginTestClass {
def str = "默認(rèn)值";
}
二兴泥。運(yùn)行uploadArchives的gradle任務(wù)工育,會(huì)在配置的目錄生成
三。引用
在project的根目錄build.gradle引入
buildscript {
dependencies {
classpath 'com.col.decypt_fixer:decypt_fixer:1.0.0'
在需要調(diào)用插件的地方
apply plugin: 'com.android.application'
apply plugin: 'com.col.decypt_fixer'
四搓彻。編譯時(shí)查看日志如绸。
如果plugin的代碼有問題嘱朽,編譯生成plugin時(shí)不會(huì)報(bào)錯(cuò),宿主運(yùn)行時(shí)會(huì)報(bào)錯(cuò)怔接。
要先注釋掉apply plugin: 'com.col.decypt_fixer'再修改運(yùn)行uploadArchives
五搪泳。Transform插件
Transform API 是在1.5.0-beta1版開始使用,利用Transform API扼脐,第三方的插件可以在.class文件轉(zhuǎn)為dex文件之前岸军,對一些.class 文件進(jìn)行處理。Transform API 簡化了這個(gè)處理過程瓦侮,而且使用起來很靈活艰赞。
注意getScopes和getReferencedScopes的區(qū)別
參考:
https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx
http://www.reibang.com/p/811b0d0975ef