1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.build
18
19import androidx.build.SupportConfig.INSTRUMENTATION_RUNNER
20import androidx.build.license.CheckExternalDependencyLicensesTask
21import com.android.build.gradle.AppExtension
22import net.ltgt.gradle.errorprone.ErrorProneBasePlugin
23import net.ltgt.gradle.errorprone.ErrorProneToolChain
24import org.gradle.api.JavaVersion
25import org.gradle.api.Plugin
26import org.gradle.api.Project
27import org.gradle.api.tasks.compile.JavaCompile
28
29/**
30 * Support library specific com.android.application plugin that sets common configurations needed
31 * for support library test apps.
32 */
33class SupportAndroidTestAppPlugin : Plugin<Project> {
34    override fun apply(project: Project) {
35        val testAppExtension = project.extensions.create("supportTestApp",
36                SupportAndroidTestAppExtension::class.java, project)
37        CheckExternalDependencyLicensesTask.configure(project)
38        project.afterEvaluate {
39            val application = project.extensions.findByType(AppExtension::class.java)
40                    ?: throw Exception("Failed to find Android extension")
41            application.defaultConfig.minSdkVersion(testAppExtension.minSdkVersion)
42        }
43
44        project.apply(mapOf("plugin" to "com.android.application"))
45        project.apply(mapOf("plugin" to ErrorProneBasePlugin::class.java))
46
47        val application = project.extensions.findByType(AppExtension::class.java)
48                ?: throw Exception("Failed to find Android extension")
49
50        application.compileSdkVersion(SupportConfig.CURRENT_SDK_VERSION)
51        application.defaultConfig.targetSdkVersion(SupportConfig.CURRENT_SDK_VERSION)
52
53        application.buildToolsVersion = SupportConfig.BUILD_TOOLS_VERSION
54
55        application.defaultConfig.versionCode = 1
56        application.defaultConfig.versionName = "1.0"
57
58        application.compileOptions.setSourceCompatibility(JavaVersion.VERSION_1_8)
59        application.compileOptions.setTargetCompatibility(JavaVersion.VERSION_1_8)
60
61        application.defaultConfig.testInstrumentationRunner = INSTRUMENTATION_RUNNER
62        application.testOptions.unitTests.isReturnDefaultValues = true
63
64        // Use a local debug keystore to avoid build server issues.
65        application.signingConfigs.findByName("debug")?.storeFile =
66                SupportConfig.getKeystore(project)
67
68        application.lintOptions.isAbortOnError = true
69        val baseline = SupportConfig.getLintBaseline(project)
70        if (baseline.exists()) {
71            application.lintOptions.baseline(baseline)
72        }
73
74        val toolChain = ErrorProneToolChain.create(project)
75        project.dependencies.add("errorprone", ERROR_PRONE_VERSION)
76
77        project.afterEvaluate {
78            if (testAppExtension.enableErrorProne) {
79                project.tasks.forEach {
80                    (it as? JavaCompile)?.configureWithErrorProne(toolChain)
81                }
82            }
83        }
84    }
85}