|
| 1 | + |
| 2 | +import java.nio.file.Files |
| 3 | +import java.nio.file.Paths |
| 4 | +import java.io.FileOutputStream |
| 5 | +import java.util.zip.ZipFile |
| 6 | + |
| 7 | +// Android tasks for Javadoc and sources.jar generation |
| 8 | + |
| 9 | +afterEvaluate { project -> |
| 10 | + if (POM_PACKAGING == 'aar') { |
| 11 | + task androidJavadoc(type: Javadoc, dependsOn: assembleDebug) { |
| 12 | + source += files(android.sourceSets.main.java.srcDirs) |
| 13 | + failOnError false |
| 14 | + // This task will try to compile *everything* it finds in the above directory and |
| 15 | + // will choke on text files it doesn't understand. |
| 16 | + exclude '**/BUCK' |
| 17 | + exclude '**/*.md' |
| 18 | + } |
| 19 | + |
| 20 | + task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) { |
| 21 | + classifier = 'javadoc' |
| 22 | + from androidJavadoc.destinationDir |
| 23 | + } |
| 24 | + |
| 25 | + task androidSourcesJar(type: Jar) { |
| 26 | + classifier = 'sources' |
| 27 | + from android.sourceSets.main.java.srcDirs |
| 28 | + } |
| 29 | + |
| 30 | + android.libraryVariants.all { variant -> |
| 31 | + def name = variant.name.capitalize() |
| 32 | + task "jar${name}"(type: Jar, dependsOn: variant.javaCompileProvider) { |
| 33 | + from variant.javaCompileProvider.get().destinationDir |
| 34 | + } |
| 35 | + |
| 36 | + androidJavadoc.doFirst { |
| 37 | + classpath += files(android.bootClasspath) |
| 38 | + classpath += files(variant.javaCompileProvider.get().classpath.files) |
| 39 | + // This is generated by `assembleDebug` and holds the JARs generated by the APT. |
| 40 | + classpath += fileTree(dir: "$buildDir/intermediates/bundles/debug/", include: '**/*.jar') |
| 41 | + |
| 42 | + // Process AAR dependencies |
| 43 | + def aarDependencies = classpath.filter { it.name.endsWith('.aar') } |
| 44 | + classpath -= aarDependencies |
| 45 | + aarDependencies.each { aar -> |
| 46 | + // Extract classes.jar from the AAR dependency, and add it to the javadoc classpath |
| 47 | + def outputPath = "$buildDir/tmp/aarJar/${aar.name.replace('.aar', '.jar')}" |
| 48 | + classpath += files(outputPath) |
| 49 | + |
| 50 | + // Use a task so the actual extraction only happens before the javadoc task is run |
| 51 | + dependsOn task(name: "extract ${aar.name}").doLast { |
| 52 | + extractEntry(aar, 'classes.jar', outputPath) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + artifacts.add('archives', androidJavadocJar) |
| 59 | + artifacts.add('archives', androidSourcesJar) |
| 60 | + } |
| 61 | + |
| 62 | + if (POM_PACKAGING == 'jar') { |
| 63 | + task javadocJar(type: Jar, dependsOn: javadoc) { |
| 64 | + classifier = 'javadoc' |
| 65 | + from javadoc.destinationDir |
| 66 | + } |
| 67 | + |
| 68 | + task sourcesJar(type: Jar, dependsOn: classes) { |
| 69 | + classifier = 'sources' |
| 70 | + from sourceSets.main.allSource |
| 71 | + } |
| 72 | + |
| 73 | + artifacts.add('archives', javadocJar) |
| 74 | + artifacts.add('archives', sourcesJar) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Utility method to extract only one entry in a zip file |
| 79 | +private def extractEntry(archive, entryPath, outputPath) { |
| 80 | + if (!archive.exists()) { |
| 81 | + throw new GradleException("archive $archive not found") |
| 82 | + } |
| 83 | + |
| 84 | + def zip = new ZipFile(archive) |
| 85 | + zip.entries().each { |
| 86 | + if (it.name == entryPath) { |
| 87 | + def path = Paths.get(outputPath) |
| 88 | + if (!Files.exists(path)) { |
| 89 | + Files.createDirectories(path.getParent()) |
| 90 | + Files.copy(zip.getInputStream(it), path) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + zip.close() |
| 95 | +} |
0 commit comments