31

My team is developing an Android library, to be used in some example applications, also developed by us. The library is created as an AAR file. Here is its build.gradle

apply plugin: 'com.android.library'
apply from: 'deploy.gradle'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'LICENSE'
        exclude 'NOTICE'
    }
    defaultConfig {
        // Excluded for brevity
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
        // if true, check all issues, including those that are off by default
        checkAllWarnings true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v13:20.0.+'
    compile 'com.google.android.gms:play-services:5.+'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
}

Here is the dependencies part of the app's build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.mycompany.myapp:0.6.5-SNAPSHOT@aar') {
        transitive = true
        downloadJavadoc = true
        downloadSources = true
    }
}

I can use all the lib classes, but I can't see the Javadocs nor the sources. How can I package my AAR so it includes the sources and Javadocs?

jmm
  • 1,044
  • 2
  • 12
  • 38
  • `downloadJavadoc` and `downloadSources` aren't supported in a `compile` statement; not sure where that came from. See also http://stackoverflow.com/questions/17426628/how-to-make-android-studio-download-dependencies-sources-and-javadoc, though your question seems more about how to package those in the AAR and not how to reference them when they're included? – Scott Barta Oct 23 '14 at 16:49
  • That's correct Scott, I want the sources and javadoc to be packaged inside the AAR. Other libraries that we are using (like Retrofit) come with the sources and javadoc, but those libraries are packaged as JARs instead of AARs. – jmm Oct 23 '14 at 17:34

3 Answers3

11

Seems like it can't be done for now. However, adding these gradle tasks in the deploy.gradle, the -javadoc.jar and -sources.jar are created.

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
}

task androidJavadocsJar(type: Jar) {
    classifier = 'javadoc'
    baseName = artifact_id
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    baseName = artifact_id
    from android.sourceSets.main.java.srcDirs
}

artifacts {
//    archives packageReleaseJar
    archives androidSourcesJar
    archives androidJavadocsJar
}

After that, the sources and javadoc need to be manually imported. According to this thread and this open issue they can't be automatically imported.

jmm
  • 1,044
  • 2
  • 12
  • 38
1

This is not a solution but an important note regardless what solution you come up with. If you publish your library to Maven, you must make sure the name of the aar, javadocs file and sources all have the same name, otherwise Android Studio will not be able to retrieve the javadocs and let developers move their mouse over a class/function/property to get help. For example, if your library is called MyLib and and it includes a version number in the file name such as:

MyLib-1.0.0.aar

The javadocs and source files must be named:

MyLib-1.0.0-javadoc.jar
MyLib-1.0.0-sources.jar

This may seem obvious but by default Android builds libraries to include the variant name such as:

MyLib-1.0.0-release.aar

If the word "release" doesn't also appear in the filenames for the javadoc and sources, Gradle will fail to retrieve them. The standard naming convention when uploading to Maven is to strip off the variant name before uploading.

Johann
  • 27,536
  • 39
  • 165
  • 279
  • this doesn't seem to be working with local aar files. I have to choose the sources and open the source file again to see the sources. – Monster Brain Dec 28 '21 at 13:38
0

I was able to create the sources jar for a library in Kotlin using the below

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.kotlin.sourceFiles
}

task androidJavadocsJar(type: Jar) {
    archiveClassifier.set("javadoc")
    //baseName = artifact_id
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    archiveClassifier.set("sources")
    //baseName = artifact_id
    from android.sourceSets.main.kotlin.sourceFiles
}

artifacts {
//    archives packageReleaseJar
    archives androidSourcesJar
    archives androidJavadocsJar
}

I referred the U-crop library. classifier seems to be deprecated as well. using java.sourcefiles created empty src directories.

This code is added before the android{ } in app > build.gradle

Monster Brain
  • 1,950
  • 18
  • 28