1 /* <lambda>null2 * Copyright 2023 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 package androidx.baselineprofile.gradle.utils 18 19 import com.android.build.api.AndroidPluginVersion 20 import com.android.build.api.variant.AndroidComponentsExtension 21 import com.android.build.api.variant.TestVariant 22 import org.gradle.api.GradleException 23 import org.gradle.api.Project 24 import org.gradle.api.provider.Provider 25 26 internal fun Project.agpVersion(): AndroidPluginVersion { 27 return project.extensions.findByType(AndroidComponentsExtension::class.java)?.pluginVersion 28 ?: throw GradleException( 29 // This can happen only if the plugin is not applied to an android module. 30 """ 31 The module $name does not have a registered `AndroidComponentsExtension`. This can only 32 happen if this is not an Android module. Please review your build.gradle to ensure this 33 plugin is applied to the correct module. 34 """ 35 .trimIndent() 36 ) 37 } 38 39 internal enum class AgpFeature(internal val version: AndroidPluginVersion) { 40 41 APPLICATION_VARIANT_HAS_UNIT_TEST_BUILDER(AndroidPluginVersion(8, 1, 0)), 42 TEST_MODULE_SUPPORTS_MULTIPLE_BUILD_TYPES(AndroidPluginVersion(8, 1, 0).alpha(7)), 43 TEST_VARIANT_SUPPORTS_INSTRUMENTATION_RUNNER_ARGUMENTS(AndroidPluginVersion(8, 2, 0).alpha(3)), 44 LIBRARY_MODULE_SUPPORTS_BASELINE_PROFILE_SOURCE_SETS(AndroidPluginVersion(8, 3, 0).alpha(15)), 45 TEST_VARIANT_TESTED_APKS(AndroidPluginVersion(8, 3, 0)), 46 CONFIGURATION_CACHE_FIX_B348136774(AndroidPluginVersion(8, 4, 0)), 47 } 48 49 /** 50 * This class should be referenced only in AGP 8.2, as the utilized api doesn't exist in previous 51 * versions. Keeping it as a separate class instead of accessing the api directly allows previous 52 * version of AGP to be compatible with this code base. 53 */ 54 internal object InstrumentationTestRunnerArgumentsAgp82 { setnull55 fun set(variant: TestVariant, arguments: List<Pair<String, String>>) { 56 arguments.forEach { (k, v) -> set(variant, k, v) } 57 } 58 setnull59 fun set(variant: TestVariant, key: String, value: String) { 60 variant.instrumentationRunnerArguments.put(key, value) 61 } 62 setnull63 fun set(variant: TestVariant, key: String, value: Provider<String>) { 64 variant.instrumentationRunnerArguments.put(key, value) 65 } 66 } 67 68 /** 69 * This class should be referenced only in AGP 8.3, as the utilized api doesn't exist in previous 70 * versions. Keeping it as a separate class instead of accessing the api directly allows previous 71 * version of AGP to be compatible with this code base. 72 */ 73 @Suppress("UnstableApiUsage") 74 internal object TestedApksAgp83 { getTargetAppApplicationIdnull75 fun getTargetAppApplicationId(variant: TestVariant): Provider<String> { 76 // Note that retrieving the BuildArtifactsLoader from within the lambda causes an issue 77 // with serialization (see b/325886853#comment13). 78 val buildArtifactLoader = variant.artifacts.getBuiltArtifactsLoader() 79 return variant.testedApks.map { buildArtifactLoader.load(it)?.applicationId ?: "" } 80 } 81 } 82