PermaLink Using Gradle to build Android Library Eclipse projects08/01/2013 02:51 AM
The Gradle build system was one of the most exciting things revealed at Google I/O this year if you're a developer.  The promise of getting rid of Ant/Maven XML config hell was a big juicy apple.  But then you find out that it only works well with Android Studio.  But Android Studio isn't stable or featured enough to use for development (no JUnit test support even).  So you go back to Eclipse and find the Gradle Eclipse plugin which mostly works except you have to invoke Gradle via a menu item.  But this doesn't work with Android Library or with Android App projects because you can't override Eclipse's build system (the main reason Google switched to IntelliJ/AndroidStudio is you can do this w/ IntelliJ).

But you still want to use Gradle and Groovy for your build scripts...

The Gradle script below will let you build an Android Library while still being able to develop your code in Eclipse. Unfortunately, you can only use this script for the final command line build process, but this it's an alternative to using Ant/Maven for the release process at least. I'll annotate the script in blue with my comments on how it works.

buildscript {
repositories {
mavenCentral()
}
dependencies {
the Android Gradle plugin changes frequently, so change this version# as needed
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
this is the reference to the Android Library plugin (apps use just the 'android' plugin)
apply plugin: 'android-library'

android {
this references the latest Android SDK installed on your system
compileSdkVersion 18
buildToolsVersion "18.0.0"

defaultConfig {
these versions match what's in your AndroidManifest.xml file
targetSdkVersion 18
minSdkVersion 10
versionCode 10
}

this remaps the standard Gradle layout (root at src/main) to the Eclipse Android project directory layout
sourceSets {
main {
manifest {
srcFile 'AndroidManifest.xml'
}
java {
srcDir 'src'
}
res {
srcDir 'res'
}
assets {
srcDir 'assets'
}
resources {
srcDir 'src'
}
}
}
}


/// add dependencies
repositories {
mavenCentral()
}

dependencies {
this pulls the support library artifact from your local ADK install's repos
compile group: 'com.android.support', name: 'support-v4', version: '13.0.+'
}

the jar() and javaDoc() functions are in the 'java' plugin and for some reason Android's plugin checks that the 'java' plugin is *not* installed before it'll work, so you have to rewrite any functionality for those using the executables
//jar method lives inside java plugin which we can't include
// because it conflict w/ Android plugin :-P
//jar {
// manifest {
// attributes 'Implementation-Title': 'Android DDP Client Library',
// 'Implementation-Version': version
// }
//}

this is my version of the javaDoc function
task makeJavadoc(type:Exec) {
commandLine 'javadoc', '-d', 'build/docs/javadoc', '-sourcepath', './src', '-subpackages', 'com'
standardOutput = new ByteArrayOutputStream()
errorOutput = new ByteArrayOutputStream()
//extension method makeJavadoc.output() can be used to obtain the output:
ext.output = {
return standardOutput.toString()
}
ext.error = {
return errorOutput.toString()
}
}

// can't ", dependsOn: javadoc" because that is in the java plugin which isn't compatible w/ Android plugin :-P
task javadocJar(type: Jar, dependsOn: makeJavadoc) {
classifier = 'javadoc'
from 'build/docs/javadoc'
}

task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.allSource
}

=======================
As mentioned above, the Android Gradle plugin uses Maven artifacts from the local ADK Manager repos (install these in the Extras section of the list of toolkits to install). It's important that you get the artifact names right or Gradle will try pulling them from Maven Central:
com.android.support:support-v4:13.0.0
com.android.support:support-v13:13.0.0
com.android.support:gridlayout-v7:13.0.0
com.google.android.gms:play-services:3.1.36

Comments :v
No comments.

Start Pages
RSS News Feed RSS Comments Feed CoComment Integrated
The BlogRoll
Calendar
June 2024
Su
Mo
Tu
We
Th
Fr
Sa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Search
Contact Me
About Ken
Full-stack developer (consultant) working with .Net, Java, Android, Javascript (jQuery, Meteor.js, AngularJS), Lotus Domino