android_tasks.gradle 3.39 KB
Newer Older
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

import java.nio.file.Files
import java.nio.file.Paths
import java.io.FileOutputStream
import java.util.zip.ZipFile

// Android tasks for Javadoc and sources.jar generation

afterEvaluate { project ->
    if (POM_PACKAGING == 'aar') {
        task androidJavadoc(type: Javadoc, dependsOn: assembleDebug) {
            source += files(android.sourceSets.main.java.srcDirs)
            failOnError false
            // This task will try to compile *everything* it finds in the above directory and
            // will choke on text files it doesn't understand.
            exclude '**/BUCK'
            exclude '**/*.md'
        }

        task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
            classifier = 'javadoc'
            from androidJavadoc.destinationDir
        }

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

        android.libraryVariants.all { variant ->
            def name = variant.name.capitalize()
            task "jar${name}"(type: Jar, dependsOn: variant.javaCompileProvider) {
                from variant.javaCompileProvider.get().destinationDir
            }

            androidJavadoc.doFirst {
                classpath += files(android.bootClasspath)
                classpath += files(variant.javaCompileProvider.get().classpath.files)
                // This is generated by `assembleDebug` and holds the JARs generated by the APT.
                classpath += fileTree(dir: "$buildDir/intermediates/bundles/debug/", include: '**/*.jar')

                // Process AAR dependencies
                def aarDependencies = classpath.filter { it.name.endsWith('.aar') }
                classpath -= aarDependencies
                aarDependencies.each { aar ->
                    // Extract classes.jar from the AAR dependency, and add it to the javadoc classpath
                    def outputPath = "$buildDir/tmp/aarJar/${aar.name.replace('.aar', '.jar')}"
                    classpath += files(outputPath)

                    // Use a task so the actual extraction only happens before the javadoc task is run
                    dependsOn task(name: "extract ${aar.name}").doLast {
                        extractEntry(aar, 'classes.jar', outputPath)
                    }
                }
            }
        }

        artifacts.add('archives', androidJavadocJar)
        artifacts.add('archives', androidSourcesJar)
    }

    if (POM_PACKAGING == 'jar') {
        task javadocJar(type: Jar, dependsOn: javadoc) {
            classifier = 'javadoc'
            from javadoc.destinationDir
        }

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

        artifacts.add('archives', javadocJar)
        artifacts.add('archives', sourcesJar)
    }
}

// Utility method to extract only one entry in a zip file
private def extractEntry(archive, entryPath, outputPath) {
    if (!archive.exists()) {
        throw new GradleException("archive $archive not found")
    }

    def zip = new ZipFile(archive)
    zip.entries().each {
        if (it.name == entryPath) {
            def path = Paths.get(outputPath)
            if (!Files.exists(path)) {
                Files.createDirectories(path.getParent())
                Files.copy(zip.getInputStream(it), path)
            }
        }
    }
    zip.close()
}