简体   繁体   中英

How to add a javadoc task to Android Gradle subprojects from the root build.gradle file?

I have a workspace with multiple android projects as follow :

  • root
    • SDK
      • Android lib 1
      • Android lib 2
      • ...
    • Samples
      • Android app 1
      • Android app 2

All projects compile with gradle.

I'm able to add directives to generate javadoc on Android apps and libs with this code :

applicationVariants.all {...}
or
libraryVariants.all { variant ->
    println variant
    task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
        description "Generates Javadoc for $variant.name."
        source = variant.javaCompile.source
        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
        options.links("http://docs.oracle.com/javase/7/docs/api/");
        options.linksOffline("http://d.android.com/reference", "${android.sdkDirectory}/docs/reference");
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

What I want is to add this directive to all subprojects from the build.gradle file in root project.

I believe this would use the subprojects {} directive, but I don't find out how run code in this directive after configuration process, and I don't know how to detect whether my application is a library or not.

Indead, I succeeded in using subprojects {} directive.

The key is to use afterEvaluate {} , so I can use android property added by android gradle plugin.

Here is the code I ended with :

subprojects{
    afterEvaluate { project ->
        if(project.hasProperty('android')){
            if(project.android.hasProperty('libraryVariants')) {
                project.android.libraryVariants.all { variant ->
                    println variant
                    task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
                        description "Generates Javadoc for $variant.name."
                        source = variant.javaCompile.source
                        ext.androidJar = "${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar"
                        classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
                        options.links("http://docs.oracle.com/javase/7/docs/api/");
                        options.linksOffline("http://d.android.com/reference", "${project.android.sdkDirectory}/docs/reference");
                        exclude '** /BuildConfig.java'
                        exclude '** /R.java'
                    }
                }
            }
            else if (project.android.hasProperty('applicationVariants')){
                project.android.applicationVariants.all { variant ->
                println variant
                task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
                    description "Generates Javadoc for $variant.name."
                    source = variant.javaCompile.source
                    ext.androidJar = "${project.android.sdkDirectory}/platforms/${project.android.compileSdkVersion}/android.jar"
                    classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
                    options.links("http://docs.oracle.com/javase/7/docs/api/");
                    options.linksOffline("http://d.android.com/reference", "${project.android.sdkDirectory}/docs/reference");
                    exclude '** /BuildConfig.java'
                    exclude '** /R.java'
                    }
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM