preface

Recently, I have been learning the compilation of Gradle Plugin and publishing Gradle Plugin, which is a very direct requirement. I write this article to record how to learn and how to draw infertile examples.

demand

There are many reasons why your company wants you to write gradle Plugin. After writing Gradle Plugin, your goal must be to promote gradle plugin to your team members. This article will help you solve this problem. Usually you have two options:

Create a gradle plugin in your project and use it locally. Upload gradle plugin to remote repository and use it remotely

The sample

To use Butter Knife in a library, add the plugin To your buildscript:

buildscript { repositories { mavenCentral() google() } dependencies { classpath 'com. Jakewharton: butterknife - gradle - plugin: 10.2.3'}}Copy the code

and then apply it in your module:

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
Copy the code

Is it so easy? Usually this is standard. Not so fast, but let’s do a quick analysis:

Classpath ‘com. Jakewharton: butterknife – gradle – plugin: 10.2.3’

What does that mean? If you have been writing gradle Plugin, you can easily tell that this is a Gradle plugin and that it has been published in a remote repository. It means that the plugin GroupId is com.jakeWharton, ArtifactId is Butterknife-gradle-plugin, and Version 10.2.3 is published on the remote repository. Then look at the second sentence:

apply plugin: ‘com.jakewharton.butterknife’

Import the plug-in in the Module whose entry configuration file iscom.jakewharton.butterknife.properties, then we verify from the source engineering structure:

The lines

We can see that simply publishing the Gradle Plugin will suffice. And then the question is, you just need to know howRelease gradle plugintoRemote warehouseCan. And the way I think about it is, like the last articleBuild your private repository, publish your libraries, and speed up project buildsRelease the aarWhen I confidently clean->assemble ->publish -> artifactoryPublish, it comes to me, as shown below.

But when I use it in the app project, the app root directory is build.gradle

plugins {
    id 'com.android.application'
    id 'com.donews.gradle.plugin'
}
Copy the code

Prompt an error

Build file 'C:\dev\android\gradle-tools\app\build.gradle' line: 3

Plugin [id: 'com.donews.gradle.plugin'] was not found in any of the following sources:
Copy the code

There is a problem with my uploading to the remote repository, that is, publishing the AAR is different from publishing the Gradle Plugin.

Search for information

Gradle plugin publish gradle plugin publish gradle plugin publish gradle plugin publish

1. Register the publishKey and publishSecret account. 3. Publish the plugins that you want to publish

The code is as follows:

Plugins {id 'java-gradle-plugin' id 'maven-publish' id 'com.gradle.plugin-publish' version '0.12.0'} pluginBundle {plugins {id 'java-gradle-plugin' id 'maven-publish' id 'com.gradle.plugin-publish' version '0.12.0'} pluginBundle { website = 'http://www.okwyx.com' vcsUrl = 'http://www.okwyx.com' tags = ['publish'] } //////////////////////////////////////////////////////////////// def globalGroupId = "com.donews.tools.gradle" def GlobalArtifactId = "commonTools" def globalVersion = "0.0.5" def globalContextUrl = "http://localhost:8081/artifactory" def globalRepoKey = "android_gradle_plugins_local" def globalUserName = "admin" def globalPassword = "password" //////////////////////////////////////////////////////////////// group = globalGroupId version = globalVersion gradlePlugin { plugins { greetingsPlugin { id = globalGroupId displayName = 'Check code Common tools' description = = 'com.donews.gradle.Com' Check androidStudio project tools' implementationClass monTools'}}} / / to the end of the official releaseCopy the code

Execute the release code:

gradlew publishPlugins

Published successfully, indicating waiting for approval

I heard on the Internet that it takes three or four days, which is… There’s no test, and OBVIOUSLY I don’t want to wait three or four days. Then how about using the Artifactory private warehouse I built by myself? It’s theoretically feasible. The next step is to search for how to publish to Gradle Plugin and Artifactory, but there is basically no information on the example case on the official website (or I didn’t see it).

One more example

The only way to do this is to continuebutterknifeBecause it must also have such a requirement, look closely, it has a line of such code

Isn’t that the variation I posted to native code

/ / groovy local repository, debugging use uploadArchives {repositories. MavenDeployer {repository (url: uri ('.. Pro.artifactid = globalArtifactId// Pro.artifactid = repositoresrepositories ') // Repositoresrepositories'. Version = globalVersion// Version number}}Copy the code

All I need to do is change the URL to the local private repository path and add the login account and password. Let’s experiment

uploadArchives { repositories.mavenDeployer { repository(url: "http://localhost:8081/artifactory/android_gradle_plugins_local/"){ authentication(userName: "$Artifactory ", password: "$Artifactory password ")} pom.groupId = globalGroupId Version = globalVersion// Version number}}Copy the code

Execute the release code:

gradlew clean

gradlew gradle_plugin:assemble

gradlew gradle_plugin:uploadArchives

View the local repository code:

The release was successful, and the App project introduced the plug-in

plugins {
    id 'com.android.application'
    id 'com.donews.gradle.plugin'
}
Copy the code

Publish gradle plugin in Artifactory private server repository without waiting for approval, introduce your private library to your friends, use it.