1 /*
2  * Copyright 2017 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 
17 @file:JvmName("AndroidXConfig")
18 
19 package androidx.build
20 
21 import androidx.build.gradle.extraPropertyOrNull
22 import java.io.File
23 import org.gradle.api.Project
24 import org.gradle.api.file.FileCollection
25 
26 /** AndroidX configuration backed by Gradle properties. */
27 abstract class AndroidConfigImpl(private val project: Project) : AndroidConfig {
28     override val buildToolsVersion: String = "35.0.0"
29 
<lambda>null30     override val compileSdk: Int by lazy {
31         val sdkString = project.extraPropertyOrNull(COMPILE_SDK)?.toString()
32         check(sdkString != null) { "$COMPILE_SDK is unset" }
33         sdkString.toInt()
34     }
35 
<lambda>null36     override val latestStableCompileSdk: Int by lazy {
37         val sdkString = project.extraPropertyOrNull(LATEST_STABLE_COMPILE_SDK)?.toString()
38         check(sdkString != null) { "$LATEST_STABLE_COMPILE_SDK is unset" }
39         sdkString.toInt()
40     }
41 
42     override val minSdk: Int = 21
43 
<lambda>null44     override val targetSdk: Int by lazy {
45         project.providers.gradleProperty(TARGET_SDK_VERSION).get().toInt()
46     }
47 
48     companion object {
49         private const val COMPILE_SDK = "androidx.compileSdk"
50         private const val LATEST_STABLE_COMPILE_SDK = "androidx.latestStableCompileSdk"
51         private const val TARGET_SDK_VERSION = "androidx.targetSdkVersion"
52 
53         /**
54          * Implementation detail. This should only be used by AndroidXGradleProperties for property
55          * validation.
56          */
57         val GRADLE_PROPERTIES =
58             listOf(
59                 COMPILE_SDK,
60                 LATEST_STABLE_COMPILE_SDK,
61                 TARGET_SDK_VERSION,
62             )
63     }
64 }
65 
66 /**
67  * Configuration values for various aspects of the AndroidX plugin, including default values for
68  * [com.android.build.api.dsl.CommonExtension].
69  */
70 interface AndroidConfig {
71     /** Build tools version used for AndroidX projects. */
72     val buildToolsVersion: String
73 
74     /**
75      * Default compile SDK version used for AndroidX projects.
76      *
77      * This may be specified in `gradle.properties` using `androidx.compileSdk`.
78      */
79     val compileSdk: Int
80 
81     /**
82      * The latest stable compile SDK version that is available to use for AndroidX projects.
83      *
84      * This may be specified in `gradle.properties` using `androidx.latestStableCompileSdk`.
85      */
86     val latestStableCompileSdk: Int
87 
88     /** Default minimum SDK version used for AndroidX projects. */
89     val minSdk: Int
90 
91     /**
92      * Default target SDK version used for AndroidX projects.
93      *
94      * This may be specified in `gradle.properties` using `androidx.targetSdkVersion`.
95      */
96     val targetSdk: Int
97 }
98 
99 /** Default configuration values for Android Gradle Plugin. */
100 val Project.defaultAndroidConfig: AndroidConfig
101     get() =
102         extensions.findByType(AndroidConfigImpl::class.java)
103             ?: extensions.create("androidx.build.AndroidConfigImpl", AndroidConfigImpl::class.java)
104 
Projectnull105 fun Project.getExternalProjectPath(): File {
106     return File(rootProject.projectDir, "../../external").canonicalFile
107 }
108 
Projectnull109 fun Project.getKeystore(): File {
110     return File(project.getSupportRootFolder(), "development/keystore/debug.keystore")
111 }
112 
Projectnull113 fun Project.getPrebuiltsRoot(): File {
114     return File(project.extraPropertyOrNull("prebuiltsRoot").toString())
115 }
116 
117 /** @return the project's Android SDK stub JAR as a File. */
Projectnull118 fun Project.getAndroidJar(sdkNum: Int = project.defaultAndroidConfig.compileSdk): FileCollection {
119     val compileSdk = "android-${sdkNum}"
120     return files(
121         arrayOf(
122             File(getSdkPath(), "platforms/$compileSdk/android.jar"),
123             // Allow using optional android.car APIs
124             File(getSdkPath(), "platforms/$compileSdk/optional/android.car.jar"),
125             // Allow using optional android.test APIs
126             File(getSdkPath(), "platforms/$compileSdk/optional/android.test.base.jar"),
127             File(getSdkPath(), "platforms/$compileSdk/optional/android.test.mock.jar"),
128             File(getSdkPath(), "platforms/$compileSdk/optional/android.test.runner.jar")
129         )
130     )
131 }
132