summaryrefslogtreecommitdiff
path: root/extensions/build.gradle
blob: 4d22ab1325915afbd731793fa281ec1465e80d0a (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import com.android.build.gradle.LibraryExtension

class VersionFileWriterTask extends DefaultTask {
    static final String RESOURCE_DIRECTORY = "generatedResources"
    static final VERSION_FILE_PATH = RESOURCE_DIRECTORY + "/META-INF/%s_%s.version"
    @Input
    String version
    @OutputFile
    File outputFile

    @TaskAction
    void write() {
        outputFile.write(version)
    }

    /**
     * Sets up Android Library project to have a task that generates a version file.
     * It must be called after [LibraryExtension] has been resolved.
     *
     * @param project an Android Library project.
     */
    static void setUpAndroidLibrary(Project project, LibraryExtension library) {
        def group = project.properties["group"] as String
        def artifactId = project.properties["name"] as String
        def version = project.properties["version"] as String

        // Add a java resource file to the library jar for version tracking purposes.
        def artifactName = new File(
                project.buildDir,
                String.format(VERSION_FILE_PATH, group, artifactId))

        def writeVersionFile = project.tasks.create("writeVersionFile",
                VersionFileWriterTask.class)
        writeVersionFile.version = version
        writeVersionFile.outputFile = artifactName

        if (library != null) {
            library.libraryVariants.all {
                it.processJavaResources.dependsOn(writeVersionFile)
            }

            library.sourceSets.getByName("main").resources.srcDir(
                new File(project.buildDir, RESOURCE_DIRECTORY)
            )
        }
    }
}

buildscript {
    Properties databindingProperties = new Properties()
    databindingProperties.load(new FileInputStream("$projectDir/../databinding.properties"))
    Properties buildToolsProperties = new Properties()
    buildToolsProperties.load(new FileInputStream("$projectDir/../../buildSrc/base/version.properties"))
    Properties dependencyProperties = new Properties()
    dependencyProperties.load(new FileInputStream("$projectDir/../../buildSrc/base/dependencies.properties"))

    def runningInIde = project.hasProperty('android.injected.invoked.from.ide')
    // this is done by bazel but if we are in IDE it also configures so we need to distinguish
    def autoConfigured = !gradle.startParameter.getInitScripts().isEmpty() && !runningInIde
    ext.autoConfigured = autoConfigured

    def TOOLS_VERSION =
            System.getenv("VERSION") ?: buildToolsProperties.get("buildVersion").toString()
    def OUT_REPO = System.getenv("OUT_REPO") ?: System.getenv("BUILD_DIR")
    def MAVEN_REPO = System.getenv("MAVEN_REPO")
    def PREBUILTS_REPO = System.getenv("PREBUILTS_REPO")
    if (runningInIde) {
        Properties devVersions = new Properties()
        devVersions.load(new FileInputStream("$projectDir/../../buildSrc/base/version.properties"))
        // invoked in the IDE, go w/ defaults.
        TOOLS_VERSION = devVersions.buildVersion
        if (TOOLS_VERSION .contains('-')) {
            TOOLS_VERSION = TOOLS_VERSION .substring(0, TOOLS_VERSION.indexOf('-')) + "-dev"
        }
        MAVEN_REPO = MAVEN_REPO ?: "${projectDir}/../../../out/repo"
        OUT_REPO = OUT_REPO ?: "${projectDir}/../../../out/repo"
        PREBUILTS_REPO = PREBUILTS_REPO ?: "${projectDir}/../../../prebuilts/tools/common/m2/repository"
    } else if (autoConfigured) {
        if (TOOLS_VERSION == null || OUT_REPO == null) {
            throw new GradleException("""must set the following env variables:
                    VERSION:$TOOLS_VERSION // tools build version
                    OUT_REPO:$OUT_REPO // maven repo to upload artifacts""")
        }
    } else if (TOOLS_VERSION == null
            || MAVEN_REPO == null
            || OUT_REPO == null
            || PREBUILTS_REPO == null) {
        throw new GradleException("""must set the following env variables:
                        VERSION:$TOOLS_VERSION // tools build version
                        MAVEN_REPO:$MAVEN_REPO // maven repo for ToT tools build
                        OUT_REPO:$OUT_REPO // maven repo to upload artifacts
                        PREBUILTS_REPO:$PREBUILTS_REPO // prebuilts repo to fix support library dependencies""")
    }

    databindingProperties.version = TOOLS_VERSION
    databindingProperties.androidPluginVersion = TOOLS_VERSION
    databindingProperties.compileSdkVersion = databindingProperties.compileSdkVersionStr.trim()
    databindingProperties.group = "androidx.databinding"
    databindingProperties.maven = [:]
    if (autoConfigured) {
        // bazel build
        databindingProperties.maven.out_repo = new File(OUT_REPO, "local_repo").getAbsolutePath()
        databindingProperties.maven.zip_out = new File(OUT_REPO, "local_repo_zip")
    } else {
        // gradle build
        databindingProperties.maven.out_repo = new File(OUT_REPO).getAbsolutePath()
        databindingProperties.maven.tools_repo = file(MAVEN_REPO).getAbsolutePath()
        databindingProperties.maven.prebuilts_repo = file(PREBUILTS_REPO).getAbsolutePath()
    }
    ext.dataBindingConfig = databindingProperties
    def supportLibVersion = "1.0.0"
    def lifecycleVersion = "2.4.0"
    ext.libs = [:]
    ext.libs.android_support_annotations = "androidx.annotation:annotation:$supportLibVersion"
    ext.libs.android_support_collection = "androidx.collection:collection:$supportLibVersion"
    ext.libs.android_support_cardview_v7 = "androidx.cardview:cardview:$supportLibVersion"
    ext.libs.android_support_appcompat_v7 = "androidx.appcompat:appcompat:$supportLibVersion"
    ext.libs.fragments = "androidx.fragment:fragment:1.2.0"
    // some of these lifecycle deps are unnecessary but we are keeping them to stay backwards
    // compatible with the lifecycle-extensions dependency
    ext.libs.lifecycle_livedata = "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion"
    ext.libs.lifecycle_process = "androidx.lifecycle:lifecycle-process:$lifecycleVersion"
    ext.libs.lifecycle_service = "androidx.lifecycle:lifecycle-service:$lifecycleVersion"
    ext.libs.lifecycle_viewmodel = "androidx.lifecycle:lifecycle-viewmodel:$lifecycleVersion"
    ext.libs.android_arch_lifecycle_runtime = "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion"
    ext.libs.android_arch_lifecycle_runtime_ktx = "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
    ext.libs.kotlin_gradle_plugin = dependencyProperties.kotlin_gradle_plugin
    ext.libs.kotlin_stdlib = dependencyProperties.kotlin_stdlib
    ext.libs.coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1"

    dependencies {
        classpath "com.android.tools.build:gradle:$TOOLS_VERSION"
        classpath rootProject.ext.libs.kotlin_gradle_plugin
    }

    if (!autoConfigured) {
        repositories {
            maven {
                url = dataBindingConfig.maven.tools_repo
            }
            maven {
                url = dataBindingConfig.maven.prebuilts_repo
            }
        }
    }
}

task createArchive(type: Zip) {
    description "Creates a maven repository that includes just the libraries compiled in this" +
            " project, without any history from prebuilts."
    from dataBindingConfig.maven.out_repo
    if (dataBindingConfig.maven.zip_out != null) {
        destinationDir dataBindingConfig.maven.zip_out
    }
    archiveBaseName.set("dataBindingRuntimeRepo")
}

subprojects {
    def BUILD_DIR = System.getenv("BUILD_DIR")
    if (BUILD_DIR != null) {
        buildDir = new File(BUILD_DIR, "$name-build")
    }
    if (project.path == ":doclava") {
        return
    }
    apply plugin: 'maven-publish'
    group = dataBindingConfig.group
    version = dataBindingConfig.version
    if (!autoConfigured) {
        repositories {
            maven {
                url = dataBindingConfig.maven.tools_repo
            }
            maven {
                url = dataBindingConfig.maven.prebuilts_repo
            }
        }
    }

    publishing {
        repositories {
            maven {
                url = "${dataBindingConfig.maven.out_repo}"
            }
        }
    }
    createArchive.dependsOn tasks.named('publish')
    configurations.all {
        resolutionStrategy {
            failOnVersionConflict()

            preferProjectModules()
            force 'androidx.lifecycle:lifecycle-viewmodel:2.4.0'
            force 'androidx.lifecycle:lifecycle-livedata-core:2.4.0'
            force 'androidx.fragment:fragment:1.0.0'
        }
    }
    project.afterEvaluate {
        VersionFileWriterTask.setUpAndroidLibrary(project,
                project.extensions.findByType(LibraryExtension.class))
    }
}

// creates the javadoc task for all of data binding runtime libraries
def getRootDocsTask(p) {
    def rootDocs = rootProject.tasks.findByName("generateDocs")
    if (rootDocs != null) {
        return rootDocs
    }
    rootDocs = p.rootProject.tasks.create(name : "generateDocs",type: Javadoc, dependsOn: p.configurations.doclava) {
        def docDir = "javadoc-offline"
        if (project.hasProperty("online")) {
            docDir = "javadoc-online"
            options.addStringOption("dac_libraryroot", "android/databinding")
            options.addStringOption("dac_dataname", "DATABINDING_DATA")
            options.addStringOption("toroot", "/")
            options.addStringOption("hdf", "dac")
            options.addBooleanOption("yamlV2", true)
            options.addBooleanOption("devsite", true)
        }
        title = null
        def sdkVersion = dataBindingConfig.compileSdkVersionInt
        destinationDir = new File(buildDir, docDir)
        // add baseLibrary sources, not pretty but easy solution.
        source = files('../baseLibrary/src/main/java')
        classpath = files("${rootDir}/../../../prebuilts/sdk/${sdkVersion}/public/android.jar")
        options.addStringOption "federate Android", "http://developer.android.com"
        options.addStringOption "federationapi Android",
                "${rootDir}/../../../prebuilts/sdk/${sdkVersion}/public/api/android.txt"
        options.encoding = "UTF-8"
        options.doclet = "com.google.doclava.Doclava"
        options.docletpath = p.configurations.getByName("doclava").resolve().toList()
        options.addStringOption("templatedir", "${rootDir}/../../../external/doclava/res/assets/templates-sdk/")
        exclude '**/BuildConfig.java'
        exclude '**/R.java'
    }
}

def enableDoclava(p) {
    p.configurations {
        doclava
    }

    p.dependencies {
        doclava project(':doclava')
    }

    def docsTask  = getRootDocsTask(p)
    p.afterEvaluate {
        p.android.libraryVariants.all { variant ->
            def name = variant.buildType.name

            if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
                return // Skip debug builds.
            }
            docsTask.classpath += p.files(variant.javaCompile.classpath) +
                    p.files(variant.javaCompile.destinationDir)
            docsTask.source += files(p.android.sourceSets.main.java.srcDirs)
            docsTask.dependsOn variant.javaCompile
        }
    }

}

if (findProject(":doclava") != null) {
    subprojects { p ->
        if (p.path != ":doclava") {
            project.plugins.whenPluginAdded { plugin ->
                def isAndroidLibrary = "com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)
                if (isAndroidLibrary) {
                    enableDoclava(p)
                }
            }
        }
    }
}