Android Gradle plugin Development guide for beginners

  • Android Gradle Plugin development guide (a), explains the complete process of developing Gradle Plugin
  • Android Gradle Plugin development guide (ii), for Android Gradle Plugin development practice
  • Android Gradle plugin development guide (3), how to publish the plugin to jCenter

In Tutorial 2 we developed a plugin to change the name of the APK output file, but this plugin can only be used locally and cannot be shared with other developers on the network. Next we used gradle-Bintray-plugin to upload our plugin to Bintray. Gradle-bintray-plugin documentation address: github.com/bintray/gra…

Introduction, Bintry & JCenter

Bintray is JFrog Bintray. JCenter is an official repository for Bintray, and it can be found in the build. Gradle build script in the root directory of the Android project.

buildscript {
    repositories {
        google()
        jcenter() / / I'm here. } } allprojects { repositories { google() jcenter()/ / I'm here}}Copy the code

A, preparation,bintrayThe account of

1. Register account information

If you already have a Bintray account, go straight to step 2

Go to the Bintray website:bintray.com/, click “Sign In” In the upper right corner to go to the login page, and then register an account. You have to be selective hereOpen account, free trial account is limited!

2. Create a warehouse

Log in to your Bintray account and create a warehouse

3. Locate the account information

Go to Your Bintray account and Edit Your Profile -> API Key to find Your Key information

Second, the application ofgradle-bintray-plugin

Gradle-bintray-plugin (apkrename-plugin/build.gradle

plugins {
    id "com.jfrog.bintray" version "1.8.5"
}
Copy the code

Three, configuration,gradle-bintry-plugin

1. Configure the account information

To tell Gradle-Bintray-Plugin which account we want to upload the package to, we need bintray’s user name and Api Key. Since the source code of our plug-in may be public or uploaded to Git, we cannot directly fill in our configuration information in the plug-in build script. There are two ways to handle this:

  • local.properties

The root directory of each Gradle project has a local. Properties file, which is used to store local properties. This file is not uploaded to git by default. Then read the plugin’s build script (apkrename-plugin/build.gradle)

// Read the bintay account information
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream()
properties.load(inputStream)
inputStream.close()

bintray {
    user = properties.getProperty('bintray.user')
    key = properties.getProperty('bintray.key')}Copy the code
  • System Environment Variables

If we accidentally upload the local.properties file to Git or send it to someone else, there is still a risk of account leakage. It is safer to save the Bintray account information in a system environment variable that can only be accessed locally. I use ZSH shell, ZSH environment variable configuration is in~/.zshrcAdd the following statement, other shell environment variables configuration you search, not to mention.Then read the plugin’s build script (apkrename-plugin/build.gradle)bintrayAccount information of

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_KEY')}Copy the code

2. Configure the package information

Add the package’s identity information, detailed instructions in the code comments, combined with the code should be more clear

bintray {
    // Bintray account information that tells Gradle-bintray-Plugin which account to upload the package to
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_KEY')
    
    // Configure the package information here
    pkg {
        // Mandatory, the name of the warehouse we created earlier
        repo = 'maven'

        // Mandatory, the name of the package we want to upload
        name = 'apk_rename'

        // Optional, user groups, paid accounts only available function
        userOrg = 'lenebf'

        // Original license, if it is open source package must have, otherwise optional, translation is not very accurate.
        Your package licenses (mandatory if the package doesn't exist
        // yet and must be created, and if the package is an OSS package; optional
        // otherwise)
        licenses = ['the Apache 2.0']

        // Version control system address (usually git address), if it is open source package must have, otherwise optional, translation is not very accurate.
        Your VCS URL (Mandatory if the package doesn't exist yet and
        // must be created, and if the package is an OSS package; optional otherwise)
        vcsUrl = 'https://github.com/lenebf/GradlePluginTutorial'

        // Package version information
        version {
            // Mandatory, version name
            name = '0.0.1'
            // Optional, version description
            desc = 'ApkRename Plugin 0.0.1'
            // Optional, version release date
            released  = new Date()
            // Select the tag name for the version control object
            vcsTag = 'tag_0. 0.1'
            // Optional. The attachment attribute information of this version can be filled in arbitrarily
            attributes = ['apk-rename': 'Hello, Word! ']}}}Copy the code

After the information is configured, we synchronizeUnder the project, let’s see if the gradle task list shows the task we want. BintrayUpload is our upload task, execute:After successful execution of our Bintray accountmavenSelect * from ‘apk_rename’ where there are no files in the package:Because our previous configuration just tells usbintray-gradle-pluginWe didn’t tell the plugin to include the files in the package to which bintray account we were uploading the package to. The plugin was a bit stupid, but luckily we weren’t, so we continued to teach it how to be human!

Release packages to Bintray

1. Configure the artifact group information

The files in a package are called artifacts. The Bintray-gradle-Plugin supports three ways to create groups of artifacts (there is more than one file in a package, so they are called artifact groups) :

  • Configurations
  • Publications
  • Copying specific files using filesSpec

Here we choose Publications, because we have used them in guide 1 and Guide 2, and I am not familiar with the other two 😝. Maven Publish the Plugin documentation: docs.gradle.org/current/use… First apply the Maven Publish Plugin and then configure the Publish information:

publishing {
    publications {
        apkRename(MavenPublication) {
            // Group ID of the plug-in. You are advised to set it to the package name of the plug-in
            groupId = 'com.lenebf.plugin'
            // The artifact ID is the name of the plug-in
            artifactId = 'apk-rename'
            version = '0.0.1'
            // Component type. Our plug-in is actually a Java component
            from components.java
        }
    }

    repositories {
        maven {
            // $rootProject indicates the root directory of your project
            url = "$rootDir/repo"}}}Copy the code

Then tell bintray-Gradle-Plugin to upload the apkRename artifact group:

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_KEY')
    // I want to upload the artifact group apkRename
    publications = ['apkRename']... }Copy the code

Re-execute the bintrayUpload task and our apk_rename plugin now has the file:At the same time, we see a prompt on the package description page:This prompt tells you that a new file has been uploaded successfully, but it is not public yet, only you can see, clickPublishJust post it. If you want to publish a new file every time it is uploaded, instead of clicking on it every time, you can add configuration notificationbintray-gradle-pluginDirect release:

bintray {
    ......
    pkg {
        ......
        // Publish each upload directly
        publish = true. }}Copy the code

2. Use Bintray to reference packages

At this point we have been able to reference our plugin package via Bintray. The first step is to find the repository address. My repository is maven, which I created earlier.Dl.bintray.com/lenebf/mave… Then put the previous project in the root directorybuild.gradleReplace the local warehouse address in the script with the Bintray warehouse address:

repositories {
    google()
    jcenter()
    // MavenLocal ()
    // mavenLocal()
    // A custom publishing address
    // maven { url('./repo') }
    // Replace with bintray warehouse address
    maven { url('https://dl.bintray.com/lenebf/maven')}}Copy the code

Synchronization project, our plug-in has been successfully applied!

Add package to Jcenter

Previously, by adding our warehouse address to the package reference list, we have been able to reference the plug-in package we uploaded on Bintray. However, the bintray account of each person is different, and the corresponding warehouse address is also different. If our project uses packages under 10 different Bintray accounts, we need to configure 10 warehouse addresses. This is too long. As mentioned earlier, Jcenter is an official repository for Bintray, which supports adding our packages. Let’s go through the documentation step by step. The document address: www.jfrog.com/confluence/… . Including your Package in JCenter is explained in detail

1. Specify package information

To add a package to jCenter, we need to meet several criteria:

  • Packages must be in a common Maven repository and must contain source code packages

The repository we created earlier is already a public Maven-type repository, but we didn’t upload the source package in the artifact, so we need to improve our compilation script to include the source package as well:

// Source package generation task
task sourcesJar(type: Jar, dependsOn: classes) {
    def classifier = getArchiveClassifier()
    classifier.set('source')
    classifier.convention('source')
    from sourceSets.main.allSource
}

publishing {
    publications {
        apkRename(MavenPublication) {
            ......
            // Add source artifacts
            artifact sourcesJar
        }
    }
}
Copy the code

Since we have already uploaded the 0.0.1 package, we need to change the version number and re-execute the bintrayUpload task. Then we can see the sources.jar file

  • The file path must comply with Maven standards

The main requirement is that the Group ID and Artifact ID combination must be unique, otherwise our package conflicts with someone else’s package. For more information about Maven standards, see the corresponding Maven documentation

  • Each version of the package must contain POM files

Packages generated by the Maven Publish Plugin already contain POM files by default

2. Try

Now that we have everything, let’s clickGo to the Add to Jcenter request page (in the lower right corner of the package home page), click the Send button, Send a message to Bintray, and wait for the email to be approved. You will then receive a private letter, as I did, telling you that the package does not meet the specification 😂 :Because our previous versions of the package do not contain sources.jar, the solution is to delete these versions of the package, as shown in the following steps:

Then resubmit the Add to Jcenter request and wait for the approval message. Once approved, your Bintray account should receive a private message telling you that the request has been approved:We can then reference our plug-in package directly through the JCenter repository.

Reference the plug-in package through the Jcenter repository

Remove references to our own repository from the build script in the project root directory:

buildscript {
    ......
    repositories {
        google()
        jcenter()
        // It has been published to jCenter and does not need its own repository
        // maven { url('https://dl.bintray.com/lenebf/maven')}}}Copy the code

Then the synchronizationThe ApkRename plugin is Easy to use.

Seven, extension

  • Actually in the Android project in addition to the common warehouse jcenter, and maven repositories, bintray also supports to add package maven warehouse, but some more requirements, specific requirements see the document: central.sonatype.org/pages/requi… . When your package is successfully added to the JCenter repository and meets the requirements to be added to the Maven repository, you can further synchronize the package to the Maven repository

  • An even more goofy-goody package management service is www.jitpack.io/, which requires almost no development effort to include your packages in the JitPack repository and then rely on the Jitpack repository to reference them

Eight, code,

The code address in the article: github.com/lenebf/Grad…