在上一篇【Gradle系列(中篇)】中,我们已经完成了在自定义插件中对class文件代码的注入功能,只不过是在主项目app中使用自定义插件的,那么能不能在library中使用自定义插件呢?当然可以,这不是废话么,OK,那我们就来试一下,看看会不会踩坑~
首先,我们先来在项目中新建一个Android Library,名称暂且叫做“mylibrary”,如下:
这里我新建了一个类叫做“ToastUtils”,包含了一个空方法showToasShort,后面我们就在showToasShort方法中注入一行代码,来弹出一个Toast提示。
Android Library已经创建好了,然后我们在app中去引用这个module,代码如下:
dependencies {
···
implementation project(':mylibrary')
···
}
之前我们依赖插件和传值是在app中进行的,也就是下面这段代码,现在我们从app的build.gradle中移除,然后放到mylibrary的build.gradle中(传值要对应修改)。
apply plugin: 'custom-gradle-plugin'
InjectCodeToClass {
className = "ToastUtils"//类名
packageName = "com.zhuyong.mylibrary"//类所在包名
methodName = "showToasShort"//方法名
injectCode = "android.widget.Toast.makeText(context,\"这是我在Library中插入的代码\",android.widget.Toast.LENGTH_SHORT).show();"//要注入的代码
}
接下来Sync一下,Build过程直接报错了,报错信息如下:
我们重点看这几句:
Caused by: org.gradle.api.UnknownDomainObjectException: Extension of type 'AppExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, NamedDomainObjectContainer
, LibraryExtension]
其中 Extension of type 'AppExtension' does not exist.这句就是AppExtension类型不存在,后面的意思是当前可注册的extension types有哪些。没错,我们这是在library中使用插件而不是app中,所以不可以使用AppExtension,从报错信息的最后我们看到有一个Extension类型是LibraryExtension,就是它没错了,现修改MyPlugin类如下:
def android = project.extensions.getByType(AppExtension.class)
修改为如下(import同时修改):
def android = project.extensions.getByType(LibraryExtension.class)
重新执行uploadArchives生成新的maven包(如果报错则要先注释掉apply的引用和InjectCodeToClass的传值,uploadArchives执行完成后再放开)。此时,我们再执行clean会发现又报错了,报错信息如下:
* What went wrong:
A problem occurred configuring project ':mylibrary'.
> Transforms with scopes '[SUB_PROJECTS, EXTERNAL_LIBRARIES]' cannot be applied to library projects.
也就是说在Transforms中[SUB_PROJECTS, EXTERNAL_LIBRARIES]这两个作用范围的类型不能应用在library中,我们把这两个类型去掉试试看,在作如下修改
static {
SCOPES.add(QualifiedContent.Scope.PROJECT);
// SCOPES.add(QualifiedContent.Scope.SUB_PROJECTS);
// SCOPES.add(QualifiedContent.Scope.EXTERNAL_LIBRARIES);
}
重复刚才的动作,重新执行uploadArchives生成新的maven包。clean之后发现已经没有报错了。OK,那接下来我们来执行make project看一下代码能不能注入进去。
Executing tasks: [:myplugin:assemble, :myplugin:testClasses, :app:assembleDebug] in project /Users/zhuyong/Desktop/Android Demo/JetPackDemo/GradleDemo
> Configure project :mylibrary
=================================
======这是我的自定义Gradle插件======
=================================
> Task :myplugin:compileJava NO-SOURCE
> Task :myplugin:compileGroovy
> Task :myplugin:processResources
> Task :myplugin:classes
> Task :myplugin:jar
> Task :myplugin:assemble
> Task :myplugin:compileTestJava NO-SOURCE
> Task :myplugin:compileTestGroovy NO-SOURCE
> Task :myplugin:processTestResources NO-SOURCE
> Task :myplugin:testClasses UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :mylibrary:preBuild UP-TO-DATE
> Task :mylibrary:preDebugBuild UP-TO-DATE
> Task :mylibrary:packageDebugRenderscript NO-SOURCE
> Task :app:generateDebugBuildConfig
> Task :mylibrary:generateDebugBuildConfig
> Task :mylibrary:generateDebugResValues
> Task :mylibrary:compileDebugAidl NO-SOURCE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :mylibrary:compileDebugRenderscript NO-SOURCE
> Task :mylibrary:generateDebugResources
> Task :mylibrary:packageDebugResources
> Task :mylibrary:processDebugManifest
> Task :mylibrary:parseDebugLocalResources
> Task :mylibrary:javaPreCompileDebug
> Task :app:mainApkListPersistenceDebug
> Task :app:generateDebugResValues
> Task :app:generateDebugResources
> Task :mylibrary:generateDebugRFile
> Task :mylibrary:compileDebugJavaWithJavac
> Task :app:mergeDebugResources
> Task :mylibrary:bundleLibCompileDebug
> Task :app:createDebugCompatibleScreenManifests
> Task :app:extractDeepLinksDebug
> Task :mylibrary:extractDeepLinksDebug
> Task :app:processDebugManifest
> Task :app:javaPreCompileDebug
> Task :mylibrary:compileDebugLibraryResources
> Task :app:processDebugResources
> Task :app:compileDebugJavaWithJavac
> Task :app:compileDebugSources
> Task :app:mergeDebugShaders
> Task :app:compileDebugShaders
> Task :app:generateDebugAssets
> Task :mylibrary:mergeDebugShaders
> Task :mylibrary:compileDebugShaders
> Task :mylibrary:generateDebugAssets
> Task :mylibrary:packageDebugAssets
> Task :app:mergeDebugAssets
> Task :app:processDebugJavaRes NO-SOURCE
> Task :mylibrary:processDebugJavaRes NO-SOURCE
> Task :mylibrary:transformClassesWith__MyTransformEditClasses__ForDebug
filePath: /Users/zhuyong/Desktop/Android Demo/JetPackDemo/GradleDemo/mylibrary/build/intermediates/javac/debug/classes
正在操作的路径 = /Users/zhuyong/Desktop/Android Demo/JetPackDemo/GradleDemo/mylibrary/build/intermediates/javac/debug/classes/com/zhuyong/mylibrary/ToastUtils.class
要插入的代码 = android.widget.Toast.makeText(context,"这是我在Library中插入的代码",android.widget.Toast.LENGTH_SHORT).show();
> Task :mylibrary:bundleLibResDebug
> Task :mylibrary:bundleLibRuntimeDebug
> Task :app:dexBuilderDebug
> Task :app:checkDebugDuplicateClasses
> Task :app:mergeDebugJavaResource
> Task :app:mergeDebugJniLibFolders
> Task :mylibrary:mergeDebugJniLibFolders
> Task :mylibrary:mergeDebugNativeLibs
> Task :mylibrary:stripDebugDebugSymbols
> Task :mylibrary:copyDebugJniLibsProjectOnly
> Task :app:mergeDebugNativeLibs
> Task :app:stripDebugDebugSymbols
> Task :app:validateSigningDebug
> Task :app:mergeExtDexDebug
> Task :app:mergeDexDebug
> Task :app:packageDebug
> Task :app:assembleDebug
BUILD SUCCESSFUL in 8s
47 actionable tasks: 47 executed
在整个build过程中,我们看到Task:transformClassesWith__MyTransformEditClasses__ForDebug已经执行了注入代码的工作,接下来看一下ToastUtils.class文件的变化:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.zhuyong.mylibrary;
import android.content.Context;
import android.widget.Toast;
public class ToastUtils {
public ToastUtils() {
}
public static void showToasShort(Context context) {
Toast.makeText(context, "这是我在Library中插入的代码", 0).show();
}
}
到此为止,我们已经在library中完成了代码的注入,最后我们再在MainActivity中使用此类,看看能不能正常弹出Toast提示。
package com.zhuyong.gradledemo;
import android.os.Bundle;
import com.zhuyong.mylibrary.ToastUtils;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showToast();
}
/**
* 弹出一个Toast
*/
public void showToast() {
ToastUtils.showToasShort(this);
}
}
直接运行项目,在手机上运行效果如下:
至此,Gradle系列上中下三篇已经完结了,如果大家还有什么疑问的话随时可以跟我讨论,谢谢。
全部代码已上传至: Github