1 /*
2  * Copyright 2024 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.build.testConfiguration
18 
19 import com.android.build.api.variant.BuiltArtifactsLoader
20 import java.io.File
21 import org.gradle.api.DefaultTask
22 import org.gradle.api.file.ConfigurableFileCollection
23 import org.gradle.api.file.DirectoryProperty
24 import org.gradle.api.file.RegularFileProperty
25 import org.gradle.api.provider.Property
26 import org.gradle.api.tasks.InputFiles
27 import org.gradle.api.tasks.Internal
28 import org.gradle.api.tasks.OutputFile
29 import org.gradle.api.tasks.PathSensitive
30 import org.gradle.api.tasks.PathSensitivity
31 import org.gradle.api.tasks.SkipWhenEmpty
32 import org.gradle.api.tasks.TaskAction
33 import org.gradle.work.DisableCachingByDefault
34 
35 /**
36  * Copy test APK (androidTest or standalone test) needed for building androidTest.zip
37  *
38  * If test requires instrumented app apks, they will be copied separately in either
39  * [CopyApkFromArtifactsTask] or [CopyApksFromOutputProviderTask] depending on project type.
40  */
41 @DisableCachingByDefault(because = "Only filesystem operations")
42 abstract class CopyTestApksTask : DefaultTask() {
43 
44     /** File existence check to determine whether to run this task. */
45     @get:InputFiles
46     @get:SkipWhenEmpty
47     @get:PathSensitive(PathSensitivity.NONE)
48     abstract val androidTestSourceCode: ConfigurableFileCollection
49 
50     @get:InputFiles
51     @get:PathSensitive(PathSensitivity.NONE)
52     abstract val testFolder: DirectoryProperty
53 
54     @get:Internal abstract val testLoader: Property<BuiltArtifactsLoader>
55 
56     @get:OutputFile abstract val outputApplicationId: RegularFileProperty
57 
58     @get:OutputFile abstract val outputTestApk: RegularFileProperty
59 
60     @TaskAction
createApksnull61     fun createApks() {
62         val testApk =
63             testLoader.get().load(testFolder.get())
64                 ?: throw RuntimeException("Cannot load required APK for task: $name")
65         val testApkBuiltArtifact = testApk.elements.single()
66         val destinationApk = outputTestApk.get().asFile
67         File(testApkBuiltArtifact.outputFile).copyTo(destinationApk, overwrite = true)
68 
69         val outputApplicationIdFile = outputApplicationId.get().asFile
70         outputApplicationIdFile.writeText(testApk.applicationId)
71     }
72 }
73