blob: 590be01b2346356b4f020369d2f88d969baab4a3 [file] [log] [blame]
Aurimas Liutikas7d0ea3322022-09-13 18:47:56 -07001/*
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 Rosec662e1a2022-09-16 14:37:50 -040017package androidx.build.transform
Aurimas Liutikas7d0ea3322022-09-13 18:47:56 -070018
Aurimas Liutikas7d0ea3322022-09-13 18:47:56 -070019import com.android.build.api.attributes.BuildTypeAttr
20import org.gradle.api.Project
21import org.gradle.api.attributes.Attribute
22import 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 Rosec662e1a2022-09-16 14:37:50 -040028fun configureAarAsJarForConfiguration(project: Project, configurationName: String) {
29 val testAarsAsJars = project.configurations.create("${configurationName}AarAsJar") {
Aurimas Liutikas7d0ea3322022-09-13 18:47:56 -070030 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 Rosec662e1a2022-09-16 14:37:50 -040056 project.configurations.getByName(configurationName).dependencies.add(
Aurimas Liutikas7d0ea3322022-09-13 18:47:56 -070057 project.dependencies.create(aarAsJar)
58 )
59}