Implementing Android Gradle plugin with IntelliJ

Source: Internet
Author: User
Tags maven central

Implementing Android Gradle plugin with IntelliJ

Android's Gradle plugin used a lot, such as the official app build plugin (com.android.application), LIB Build plug-ins (com.android.library), as well as third-party, such as auxiliary multidex Dexknifeplugin. But how to create a Gradle plug-in has not been understood, today try their own one of the Android Gradle plugin, after all, the practice of knowledge.

Basic knowledge

The first thing you need to know is what elements a Gradle plugin needs.
We use the Android application plugin as an example, often have the following code, what is the code exactly?

//root build.gradle    dependencies {        ‘com.android.tools.build:gradle:2.2.3‘    }//app build.gradle‘com.android.application‘
Properties value Notes
pluginID Com.android.application Corresponds to a plugin class
GroupId Com.android.tools.build
Artifactid Gradle
Version 2.2.3

The above is a required property for a Gradle plugin. Then since it is the Gradle plug-in must implement the Plugin<Project> interface. GroupId, Artifactid, version specify a plug-in project, each plug-in project can have a number of plug-ins (one pluginID per plugin)

Create a Gradle plugin

First choose the Science tool IntelliJ, which has been configured with groovy and IntelliJ in the groovy Getting Started Guide 1, so you can get it right.

    • 1. Open ij (IntelliJ),new->gradle-> Select Java and Groovy->next, as shown in

    • 2, select GroupID and Artifactid and version. These 3 are the 3 of the Gradle plug-in four properties that we're talking about, and they can determine classpath

Here we configure the following, note that the version number is 1.0

    • 3, hook on use Auto-import and create directories for empty:, this will help us to automatically guide the package and creating a good groovy and Java root directory

    • 4, just Next

    • 5, the first time to create Gradle project, he will go to download 2 jar,junit:4.11 and org.codehaus.groovy:groovy-all:2.3.11, the download process is very slow (well, actually is the company network comparison pit), patience.

    • 6. After the jar package is downloaded, the main and test folders will be created automatically (in fact, the creation of Gradle default project is also performing the Gradle task), the file structure is as follows, note that groovy and Java folders are blue

    • 7, in the Build.gradle dependencies inside to join
    gradleApi()    localGroovy()
    • 8. Create a package under the Groovy folder, and it will be called Com.fish, and the new groovy class will be called Firstplugin, and let Firstplugin implement plugin interface, This Plugin is org.gradle.api.Plugin. Then implement the Apply interface, add the code, the final code is as follows, we created a task called Showtiptask,task in the group of funny (we can group the task, we will understand later), Showtiptask just print out a line of text. Description is a description of this task, which is displayed when the user executes ./gradlew Tasks .
package Com.fishimport Org.gradle.api.Pluginimport org.gradle.api.project/** * Created by fish on 17/1/24. */  class  firstplugin  implements    Plugin  <project  > { void  Apply (Project project) {def showtiptask = project.tasks.create ( "showtips" ) <&L T {println ( "Hello I am the first custom Plguin ' )} showtiptask.group  =  "funny"  showtiptask.description =  "Hello Baby" }}  
    • 9, at this time plugin already have, is firstplugin, we need to put him to Maven central or jcenter, so we can use, here is just a demo, I do not upload, I put him on the local. How to configure:
      In Build.gradle Nega The following code, the code event is defined as a task called uploadarchives, the execution uploadarchives will be the Gradle plug-in package and then upload to local or remote.
‘maven‘uploadArchives {    repositories {        mavenDeployer {            repository(url: uri(‘repo‘))        }    }}
    • 10, at this time can try to build plug-ins, perform uploadarchives This task is OK, there are 2 ways. Method 1 is to click View-tool Windows-gradle, on the right to find uploadarchives, can not find the words may click the Blue Refresh button to refresh, as shown below


Method 2 is also possible in the terminal window of the IJ, it is ./gradlew uploadArchives recommended to use this method, you can see the clear log, especially when the log information is critical failure

10, unfortunately, failed, the log is as follows

awithan exception.

Can be used to ./gradlew uploadArchives --stacktrace see more detailed information, the information is as follows

in2.4.72.3.11        ...10 more

It seems that there are 2 Groovy-all, the original IJ relies on my default groovy is 2.4.7 version, and then the project here is dependent on the 2.3.11 version, so of course it will be wrong, the solution is to comment out this line of code in the Build.gradle compile ‘org.codehaus.groovy:groovy-all:2.3.11‘ .

11, we ./gradlew uploadArchives --stacktrace will also find the following problems, miss a file, think about what is missing, we did not specify pluginID

Fill it up, create a meta-inf/gradle-plugins folder under Main/resources, build a properties file in it, file name casually (package name format), For example, called Com.apple.propeties, the file configuration class name

implementation-class=com.fish.FirstPlugin

OK run it, at this time the plug-in is generated, the generated plug-in is mainly the jar package, configuration file, the structure is as follows

Using plugins

New an as project, call Useship.
Then in the root directory of the Build.gradle plus dependency classpath ‘com.netease:ship:1.0‘
, in the app's Build.gradle plus apply plugin: ‘com.apple‘
. Now that the plugin is in, we apply plugin: ‘com.apple‘ 're doing
Will call Firstplugin's apply method, within the Apply method we define a showtiptask, this task under the Funny group, we verify that, as shown below, sure enough to find this task

This can be performed under Terminal ./gradlew showTips and the correct result is displayed, "Hello I am the first custom Plguin"

192theforanewusingthehttps://docs.gradle.org/2.14.1anthefirsttime9.344secs
Other

We pluginid here deliberately with GroupID, Artifictid have nothing to do with, is to distinguish clearly, but in fact they are often associated.

THANKS

Http://tools.android.com/build/gradleplugin
http://blog.bugtags.com/2016/03/28/embrace-android-studio-gradle-plugin/
Https://docs.gradle.org/current/userguide/custom_plugins.html
Create a Standalone Gradle plugin for android-a step-by-step guide

Implementing Android Gradle plugin with IntelliJ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.