Recently I have been digging into the learning of Android Gradle, and found that in fact the so-called Android Gradle is just a Gradle plug-in, so if we can learn the development of Gradle, we can use Gradle to do a lot of things for us, such as intervening Android packaging, Type the APK or JAR we want, or multi-channel setup, etc.


Build plug-in project

If you want to develop Gradle, you must need a project to develop Gradle. You can use Android Studio to build Gradle projects. There are two main ways to build Gradle projects on the Internet

buildSrc

Create a buildSrc folder directly in AndroidStudio and add the SRC folder and build.gradle file, as shown below:

Groovy Basics
build.gradle

apply plugin: 'groovy'
dependencies {
    compile gradleApi()
    compile localGroovy()
}
Copy the code

This way we can add the corresponding code to the Groovy folder for Gradle development.

java Library

The second approach can take the form of creating a New Java Library:

Note that the Java Library is not the Android Library

We temporarily named glib, as shown in the picture below:

apply plugin: 'groovy'
dependencies {
    compile gradleApi()
    compile localGroovy()
}
Copy the code

Then delete the Java folder, create the Groovy folder, and create the Resources folder as shown below:

The difference between

BuildSrc can be used directly in any module in this Project. If you use a new Java Library, you will need to upload Gradle to the local code base, which will be discussed later, so we will start with buildSrc

Plug-in test

Groovy create a new file called testplugin. groovy (note that the suffix must be groovy) :


public class TestPlugin implements Plugin<Project> {

    @Override
    void apply(Project project) {
        println("aaaaaa")}}Copy the code

Gradle plugins need to apply in build.gradle, for example: apply plugin: ‘com.android.application’, so it needs to have an external name. This requires files under Resources to work. Create meta-INF /gradle-plugins in the Resources folder:

properties
hhh.properties

implementation-class=TestPlugin
Copy the code

Then we use Android Studio’s built-in App Module to do a test:

Upload to local repository

If you use the Java Library, you need to upload it to the local repository to reference it. You need to modify the build.gradle file.

apply plugin: 'groovy'
apply plugin: "maven"
dependencies {
    compile gradleApi()
    compile localGroovy()
}

uploadArchives{
    repositories{
        mavenDeployer{
            repository(url:uri('.. /repo'))
            pom.groupId = 'com.test.plugin'// Group name pom.artifactid ='test'// Plug-in name pom.version ='2.0.0'// Version number}}}Copy the code

Then execute the uploadArchives Task and you will find that the project has a new folder:

If the App Module needs to be used, add dependencies:

apply plugin: 'com.android.application'
apply plugin: 'hhh'
buildscript{
    repositories{
        maven{
            url uri('.. /repo')
        }
    }
    dependencies{
        classpath 'com. Test. The plugin: test: 2.0.0'}} // apply plugin:'fff'
Copy the code

conclusion

I will write here for the moment, and I will gradually add some knowledge about Gradle

Also welcome to pay attention to my public number, after will recommend more easy to use component library.