parent
7f5f4877a9
commit
17afcf4302
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid
|
||||||
|
|
||||||
|
import com.android.build.api.artifact.SingleArtifact
|
||||||
|
import com.android.build.api.variant.AndroidComponentsExtension
|
||||||
|
import com.android.build.api.variant.BuiltArtifactsLoader
|
||||||
|
import com.android.build.api.variant.HasAndroidTest
|
||||||
|
import org.gradle.api.DefaultTask
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.file.Directory
|
||||||
|
import org.gradle.api.file.DirectoryProperty
|
||||||
|
import org.gradle.api.provider.ListProperty
|
||||||
|
import org.gradle.api.provider.Property
|
||||||
|
import org.gradle.api.provider.Provider
|
||||||
|
import org.gradle.api.tasks.Input
|
||||||
|
import org.gradle.api.tasks.InputDirectory
|
||||||
|
import org.gradle.api.tasks.InputFile
|
||||||
|
import org.gradle.api.tasks.InputFiles
|
||||||
|
import org.gradle.api.tasks.Internal
|
||||||
|
import org.gradle.api.tasks.TaskAction
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
internal fun Project.configurePrintApksTask(extension: AndroidComponentsExtension<*, *, *>) {
|
||||||
|
extension.onVariants { variant ->
|
||||||
|
if (variant is HasAndroidTest) {
|
||||||
|
val loader = variant.artifacts.getBuiltArtifactsLoader()
|
||||||
|
val artifact = variant.androidTest?.artifacts?.get(SingleArtifact.APK)
|
||||||
|
val testSources = variant.androidTest?.sources?.java?.all
|
||||||
|
if (artifact != null && testSources != null) {
|
||||||
|
tasks.register(
|
||||||
|
"${variant.name}PrintTestApk",
|
||||||
|
PrintApkLocationTask::class.java
|
||||||
|
) {
|
||||||
|
apkFolder.set(artifact)
|
||||||
|
builtArtifactsLoader.set(loader)
|
||||||
|
variantName.set(variant.name)
|
||||||
|
sources.set(testSources)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal abstract class PrintApkLocationTask : DefaultTask() {
|
||||||
|
@get:InputDirectory
|
||||||
|
abstract val apkFolder: DirectoryProperty
|
||||||
|
|
||||||
|
@get:InputFiles
|
||||||
|
abstract val sources: ListProperty<Directory>
|
||||||
|
|
||||||
|
@get:Internal
|
||||||
|
abstract val builtArtifactsLoader: Property<BuiltArtifactsLoader>
|
||||||
|
|
||||||
|
@get:Input
|
||||||
|
abstract val variantName: Property<String>
|
||||||
|
|
||||||
|
@TaskAction
|
||||||
|
fun taskAction() {
|
||||||
|
val hasFiles = sources.orNull?.any { directory ->
|
||||||
|
directory.asFileTree.files.any { it.isFile }
|
||||||
|
} ?: throw RuntimeException("Cannot check androidTest sources")
|
||||||
|
|
||||||
|
// Don't print APK location if there are no androidTest source files
|
||||||
|
if (!hasFiles) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val builtArtifacts = builtArtifactsLoader.get().load(apkFolder.get())
|
||||||
|
?: throw RuntimeException("Cannot load APKs")
|
||||||
|
if (builtArtifacts.elements.size != 1)
|
||||||
|
throw RuntimeException("Expected one APK !")
|
||||||
|
val apk = File(builtArtifacts.elements.single().outputFile).toPath()
|
||||||
|
println(apk)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue