Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | |
Ember Rose | c662e1a | 2022-09-16 14:37:50 -0400 | [diff] [blame^] | 17 | package androidx.build.transform |
Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 18 | |
Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 19 | import com.android.build.api.attributes.BuildTypeAttr |
| 20 | import org.gradle.api.Project |
| 21 | import org.gradle.api.attributes.Attribute |
| 22 | import org.gradle.api.attributes.Usage |
| 23 | |
| 24 | /** |
| 25 | * Creates `testAarAsJar` configuration that can be used for JVM tests that need to Android library |
| 26 | * classes on the classpath. |
| 27 | */ |
Ember Rose | c662e1a | 2022-09-16 14:37:50 -0400 | [diff] [blame^] | 28 | fun configureAarAsJarForConfiguration(project: Project, configurationName: String) { |
| 29 | val testAarsAsJars = project.configurations.create("${configurationName}AarAsJar") { |
Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 30 | it.isTransitive = false |
| 31 | it.isCanBeConsumed = false |
| 32 | it.isCanBeResolved = true |
| 33 | it.attributes.attribute( |
| 34 | BuildTypeAttr.ATTRIBUTE, |
| 35 | project.objects.named(BuildTypeAttr::class.java, "release") |
| 36 | ) |
| 37 | it.attributes.attribute( |
| 38 | Usage.USAGE_ATTRIBUTE, |
| 39 | project.objects.named(Usage::class.java, Usage.JAVA_API) |
| 40 | ) |
| 41 | } |
| 42 | val artifactType = Attribute.of("artifactType", String::class.java) |
| 43 | project.dependencies.registerTransform(IdentityTransform::class.java) { spec -> |
| 44 | spec.from.attribute(artifactType, "jar") |
| 45 | spec.to.attribute(artifactType, "aarAsJar") |
| 46 | } |
| 47 | |
| 48 | project.dependencies.registerTransform(ExtractClassesJarTransform::class.java) { spec -> |
| 49 | spec.from.attribute(artifactType, "aar") |
| 50 | spec.to.attribute(artifactType, "aarAsJar") |
| 51 | } |
| 52 | |
| 53 | val aarAsJar = testAarsAsJars.incoming.artifactView { viewConfiguration -> |
| 54 | viewConfiguration.attributes.attribute(artifactType, "aarAsJar") |
| 55 | }.files |
Ember Rose | c662e1a | 2022-09-16 14:37:50 -0400 | [diff] [blame^] | 56 | project.configurations.getByName(configurationName).dependencies.add( |
Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 57 | project.dependencies.create(aarAsJar) |
| 58 | ) |
| 59 | } |