1 /*
<lambda>null2 * Copyright (C) 2023 The Dagger Authors.
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 dagger.hilt.android.plugin.util
18
19 import com.android.build.api.variant.AndroidComponentsExtension
20 import com.android.build.api.variant.Component
21 import com.android.build.api.variant.HasAndroidTest
22 import com.android.build.api.variant.HasUnitTest
23 import com.android.build.gradle.AppExtension
24 import com.android.build.gradle.BaseExtension
25 import com.android.build.gradle.LibraryExtension
26 import com.android.build.gradle.TestExtension
27
28 /**
29 * Invokes the [block] function for each Android variant, including android instrumentation tests
30 * and host unit tests.
31 */
32 internal fun AndroidComponentsExtension<*, *, *>.onAllVariants(block: (Component) -> Unit) {
33 this.onVariants { variant ->
34 block(variant)
35 (variant as? HasUnitTest)?.unitTest?.let { block(it) }
36 (variant as? HasAndroidTest)?.androidTest?.let { block(it) }
37 }
38 }
39
40 /**
41 * Invokes the [block] function for each Android variant that is considered a Hilt root, where
42 * dependencies are aggregated and components are generated.
43 */
forEachRootVariantnull44 internal fun BaseExtension.forEachRootVariant(
45 @Suppress("DEPRECATION") block: (variant: com.android.build.gradle.api.BaseVariant) -> Unit
46 ) {
47 when (this) {
48 is AppExtension -> {
49 // For an app project we configure the app variant and both androidTest and unitTest
50 // variants, Hilt components are generated in all of them.
51 applicationVariants.all { block(it) }
52 testVariants.all { block(it) }
53 unitTestVariants.all { block(it) }
54 }
55 is LibraryExtension -> {
56 // For a library project, only the androidTest and unitTest variant are configured since
57 // Hilt components are not generated in a library.
58 testVariants.all { block(it) }
59 unitTestVariants.all { block(it) }
60 }
61 is TestExtension -> {
62 applicationVariants.all { block(it) }
63 }
64 else -> error("Hilt plugin does not know how to configure '$this'")
65 }
66 }
67