The process of compiling and packaging based on Gradle in Android is as follows: how to develop the Gradle plug-in:
First, a new module named buildSrc is created in the project. Please see why it is called Official documents . Then delete the Java folder and res folder, and create a new groovy folder and resource folder:
Create a new package directory in the groovy folder, which is com/lin/plugin in my order, then create two folders meta-inf / gradle plugins in the resource folder, and add the package name. properties file in the folder. The internal code is as follows:
implementation-class=com.lin.plugin.TestPlugin
The code of TestPlugin.groovy is as follows:
class TestPlugin implements Plugin<Project> { @Override void apply(Project project) { println('--------------------------') println('this is a buildSrc plugin...') println('--------------------------') } }
Modify the build.gradle file of buildSrc:
//Add groovy and maven plug-ins apply plugin: 'groovy' apply plugin: 'maven' //Local publishing plug-in uploadArchives { repositories { mavenDeployer {//Used to publish local maven pom.groupId = "com.lin.plugin" pom.artifactId = "test" pom.version = "0.0.1" repository(url: uri('../repo')) } } } task sourcesJar(type: Jar) { from project.sourceSets.main.groovy.srcDirs classifier = 'sources' } task javadocJar(type: Jar, dependsOn: groovydoc) { classifier = 'javadoc' from groovydoc.destinationDir } artifacts { archives javadocJar archives sourcesJar } //Plug in dependency dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile gradleApi() compile localGroovy() }
Then add the local maven library to build.gradle in the root directory of the project, in order to use the plug-ins we created in the main project:
buildscript { repositories { google() jcenter() mavenCentral() maven{ url uri('./repo')//Local maven for publishing test plug-ins } } dependencies { classpath 'com.android.tools.build:gradle:3.3.0' classpath 'com.lin.plugin:test:0.0.1'//Local maven, assembled according to the name in uploadArchives above // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
After compiling, you can see the following commands in the gradle command of the plug-in:
Double click the command to build the plug-in library. After the construction, you can see the generated maven file in the root directory of the project:
Then import the plug-in into our main project build.gradle
apply plugin: 'com.lin.plugin'
Run the assemble command to build the project. You can see the execution of TestPlugin in the run window:
A Plugin plug-in has been compiled in this way. If the project needs to do some processing for apk's compilation and packaging process, you can use the plug-in mentioned above for hook operation. For more usage methods of gradle, move on Official website Just read.