Archive addition Android artifacts with Gradle.

When building Android applications or libraries common practice is to save your artifacts to a local file storage or repo.

Beside your APK there are some additional artifacts you want/need to save, and you want gradle to do this. Most common ones are Javadoc, your source files and perhaps your proguard generated files like the mapping file.

I naturally wanted a Gradle task to handle this. So lets have a look as some tasks you may want to use.

Adding Javadoc archive tasks

The following will add tasks to generate Javadocs for each build type and assemble it into a jar archive .

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
31
32
33
34
35
36
android.applicationVariants.all { variant ->
    project.task("${variant.name.capitalize()}Javadoc", type: Javadoc) {

        destinationDir = new File("$project.buildDir/javadoc/$variant.name")

        source = variant.javaCompile.source

        ext.androidJar = "${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar"
        classpath = project.files(variant.javaCompile.classpath.files) + project.files(ext.androidJar)

        options {
            linksOffline("http://d.android.com/reference", "${project.android.sdkDirectory}/docs/reference")
            links("http://docs.oracle.com/javase/7/docs/api/");
            setMemberLevel(JavadocMemberLevel.PACKAGE)
            docEncoding = 'UTF-8'
            encoding = 'UTF-8'
            charSet = 'UTF-8'
        }

        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }

    project.task("generate${variant.name.capitalize()}JavadocJar", type: Jar, dependsOn: "${variant.name.capitalize()}Javadoc") {
        classifier 'javadoc'

        description = 'Assembles a jar archive containing the generated Javadoc API documentation of $variant.name.'

        destinationDir = new File("$project.buildDir/libs/")

        exclude '**/BuildConfig.class'
        exclude '**/R.class'

        from "$project.buildDir/javadoc/$variant.name"
    }
}

When added to your build.gradle file, generate your Javadoc jar archive from the command line with gradle generateReleaseJavadocJar.

Adding Source archive tasks

The following will add tasks to assemble a jar archive containing the Java sources.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
android.applicationVariants.all { variant ->
    project.task("generate${variant.name.capitalize()}SourcesJar", type: Jar) {
        classifier = 'sources'

        description = 'Assembles a jar archive containing the main sources of $variant.name..'

        destinationDir = new File("$project.buildDir/libs/")

        // exclude generated files
        exclude '**/BuildConfig.java'
        exclude '**/R.java'

        from variant.javaCompile.source
    }
}

When added to your build.gradle file, generate your Sources jar archive from the command line with gradle generateReleaseSourcesJar.

Adding Proguard archive tasks

The following will add tasks to assemble a zip archive containing the generated proguard files.

1
2
3
4
5
6
7
8
9
10
android.applicationVariants.all { variant ->
    project.task("generate${variant.name.capitalize()}ProguardFilesJar", type: Zip) {
        classifier 'proguard'
       
        description = 'Assembles a jar archive containing the Proguard files of $variant.name..'
       
        destinationDir = new File("$project.buildDir/libs/")
        from "$project.buildDir/outputs/mapping"
    }    
}

When added to your build.gradle file, generate your Proguard zip archive from the command line with gradle generateReleaseProguardFilesJar.