Mastering Gradle for Android: From Ant to Maven and Custom Plugins
This article walks through the evolution from Ant to Maven to Gradle for Android projects, explains Gradle's project structure, key concepts like repositories, source sets, properties, tasks, shows how to publish artifacts, create and use plugins, and offers tips to speed up Gradle builds.
0. A Story
0.1 Ant – I Thought It Was an Insect
My first close encounter with programming was in 2012 when my senior suggested we replace a time‑consuming manual build with an Ant script.
Back then I didn't even know what a HashMap was, but after struggling with targets I eventually understood the basics.
0.2 Maven – Can You Read This Word?
Maven /meɪvən/
I discovered Maven while reading Chen Xionghua's "Spring in Action"; the source code was built with Maven, so I started using it.
Like Ant, Maven builds Java projects with XML configuration, but Maven also manages dependencies, allowing a single line such as:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>Early on the central repository was unstable, but later Alibaba’s mirror (maven.oschina.net) restored access.
0.3 Gradle – Is Google Your Father?
In 2013 Google recommended Gradle, which uses code‑based configuration instead of XML, making it far more flexible. Gradle can also consume Maven repositories, so you often feel like you are still using Maven.
task helloTAS << {
ant.echo(message: 'Hello TAS.')
}1. Building with Gradle
1.1 Project Structure
This is a typical Android Gradle project.
In settings.gradle we include sub‑modules, e.g.: include ':app' The root build.gradle holds common configuration for all modules.
gradle.properties defines project‑wide properties, for example: hello=Hello Tas! Creating a simple task:
task hello << {
println hello
println project.getProperties().get("hello")
}Running the task prints:
14:28:11: Executing external task 'hello'...
:app:hello
Hello Tas!
Hello Tas!
BUILD SUCCESSFULGradle also provides access to the Android SDK/NDK paths via internal classes:
private void findLocation() {
// ... omitted for brevity ...
}and helper methods:
File getSdkDirectory() { return sdk.sdkDirectory }
File getNdkDirectory() { return sdk.ndkDirectory }Example usage:
task hello << {
println android.getSdkDirectory()
} /Users/benny/Library/Android/sdkGradle supports both hierarchical and flat project layouts (see images below).
1.2 Important Concepts
1.2.1 Repository and Dependency
Repositories solve the dependency problem by letting you declare an artifact's groupId, artifactId, and version without worrying about its physical location.
Maven Central is the original repository; other repositories proxy it or add custom artifacts.
Proxy repository – caches artifacts after the first download.
Private repository – internal to a company.
Local repository – stored on the developer's machine.
1.2.2 SourceSets
SourceSets define where source code lives, e.g., src/main/java.
1.2.3 Properties
Gradle properties are a map accessible via project.getProperties(). They can be defined in gradle.properties or set programmatically: setProperty('hello', 'Hello Tas again!') 1.2.4 Project and Task
A Gradle Project is analogous to Ant's project element, and a Task corresponds to Ant's target. Tasks are created with: task hello << { ... } or with a closure:
task('hello2', {
println hello
})2. Publishing Artifacts
2.1 UI Publishing
If you have permission, the UI provides an "Upload Artifact" tab where you can select the artifact and upload it.
2.2 Using the Maven Plugin
Apply the Maven plugin in build.gradle and configure signing:
gradle.properties
-----------------
sonatypeUsername=yourUsername
sonatypePassword=yourPassword
signing.keyId=yourKeyId
signing.password=yourKeyPass
signing.secretKeyRingFile=/Users/yourUser/.gnupg/secring.gpg
projectName=YourArtifactName
group=your.group.id
artifactId=your-artifact-id
version=0.0.1-SNAPSHOT build.gradle
-----------
apply plugin: 'com.android.library'
apply plugin: 'maven'
apply plugin: 'signing'
android { /* compileSdkVersion, defaultConfig, etc. */ }
signing { /* sign configurations.archives */ }
uploadArchives { /* mavenDeployer configuration */ }Running gradle uploadArchives publishes the AAR to the Maven repository.
2.3 Using Maven Command Line
Deploy a file directly with Maven:
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
-DrepositoryId=some.id \
-Dfile=your-artifact-1.0.jar \
-DgroupId=org.some.group \
-DartifactId=your-artifact \
-Dversion=1.0 \
-Dpackaging=jarAuthentication details are added to settings.xml:
<servers>
<server>
<id>helloworld</id>
<username>yourName</username>
<password>yourPassword</password>
</server>
</servers>3. Plugins
3.1 What Is a Plugin?
A plugin bundles configuration and tasks to simplify common build scenarios.
3.2 Common Plugins
java – builds Java projects
war – builds web archives
groovy – builds Groovy projects
com.android.application – builds Android apps
com.android.library – builds Android libraries (AAR)
sign – signs artifacts
maven – publishes to Maven repositories
org.jetbrains.intellij – builds IntelliJ plugins
3.3 Write Your Own Plugin
Create a Groovy project with src/main/groovy and implement:
package com.example.wecar.plugin
import org.gradle.api.Plugin
import org.gradle.api.internal.project.ProjectInternal
class GreetingPlugin implements Plugin<ProjectInternal> {
void apply(ProjectInternal project) {
project.task('hello') << {
println 'hello'
}
}
}Add META-INF/gradle-plugins/greetings.properties containing:
implementation-class=com.example.wecar.plugin.GreetingPluginPublish the plugin with uploadArchives and then apply it in another build:
buildscript {
repositories { mavenLocal() }
dependencies { classpath 'com.example.wecar.plugin:deployplugin:1.1-SNAPSHOT' }
}
apply plugin: 'greetings'4. Gradle Is Slow? Tips to Speed It Up
Avoid the central Maven repository; use a faster mirror such as JCenter.
Upgrade to the latest Gradle version.
Enable parallel execution and the daemon in gradle.properties:
org.gradle.parallel=true
org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx1024mPlace these settings in ~/.gradle/gradle.properties to share them with teammates.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent TDS Service
TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
