commit
46deba844e
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import com.android.build.api.variant.LibraryAndroidComponentsExtension
|
||||||
|
import com.google.samples.apps.nowinandroid.configureJacoco
|
||||||
|
import org.gradle.api.Plugin
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.artifacts.VersionCatalogsExtension
|
||||||
|
import org.gradle.kotlin.dsl.dependencies
|
||||||
|
import org.gradle.kotlin.dsl.getByType
|
||||||
|
import org.gradle.kotlin.dsl.kotlin
|
||||||
|
|
||||||
|
class AndroidHiltConventionPlugin : Plugin<Project> {
|
||||||
|
override fun apply(target: Project) {
|
||||||
|
with(target) {
|
||||||
|
with(pluginManager) {
|
||||||
|
apply("org.jetbrains.kotlin.kapt")
|
||||||
|
apply("dagger.hilt.android.plugin")
|
||||||
|
}
|
||||||
|
|
||||||
|
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
|
||||||
|
dependencies {
|
||||||
|
"implementation"(libs.findLibrary("hilt.android").get())
|
||||||
|
"kapt"(libs.findLibrary("hilt.compiler").get())
|
||||||
|
"kaptAndroidTest"(libs.findLibrary("hilt.compiler").get())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,53 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import com.diffplug.gradle.spotless.SpotlessExtension
|
|
||||||
import org.gradle.api.Plugin
|
|
||||||
import org.gradle.api.Project
|
|
||||||
import org.gradle.api.artifacts.VersionCatalogsExtension
|
|
||||||
import org.gradle.kotlin.dsl.configure
|
|
||||||
import org.gradle.kotlin.dsl.getByType
|
|
||||||
|
|
||||||
class SpotlessConventionPlugin : Plugin<Project> {
|
|
||||||
override fun apply(target: Project) {
|
|
||||||
with(target) {
|
|
||||||
pluginManager.apply("com.diffplug.spotless")
|
|
||||||
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
|
|
||||||
|
|
||||||
extensions.configure<SpotlessExtension> {
|
|
||||||
kotlin {
|
|
||||||
target("**/*.kt")
|
|
||||||
targetExclude("**/build/**/*.kt")
|
|
||||||
ktlint(libs.findVersion("ktlint").get().toString()).userData(mapOf("android" to "true"))
|
|
||||||
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
|
|
||||||
}
|
|
||||||
format("kts") {
|
|
||||||
target("**/*.kts")
|
|
||||||
targetExclude("**/build/**/*.kts")
|
|
||||||
// Look for the first line that doesn't have a block comment (assumed to be the license)
|
|
||||||
licenseHeaderFile(rootProject.file("spotless/copyright.kts"), "(^(?![\\/ ]\\*).*$)")
|
|
||||||
}
|
|
||||||
format("xml") {
|
|
||||||
target("**/*.xml")
|
|
||||||
targetExclude("**/build/**/*.xml")
|
|
||||||
// Look for the first XML tag that isn't a comment (<!--) or the xml declaration (<?xml)
|
|
||||||
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[^!?])")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* 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.tasks.Input
|
||||||
|
import org.gradle.api.tasks.InputDirectory
|
||||||
|
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 javaSources = variant.androidTest?.sources?.java?.all
|
||||||
|
val kotlinSources = variant.androidTest?.sources?.kotlin?.all
|
||||||
|
|
||||||
|
val testSources = if (javaSources != null && kotlinSources != null) {
|
||||||
|
javaSources.zip(kotlinSources) { javaDirs, kotlinDirs ->
|
||||||
|
javaDirs + kotlinDirs
|
||||||
|
}
|
||||||
|
} else javaSources ?: kotlinSources
|
||||||
|
|
||||||
|
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 && it.parentFile.path.contains("build${File.separator}generated").not()
|
||||||
|
}
|
||||||
|
} ?: 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)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
val ktlintVersion = "0.43.0"
|
||||||
|
|
||||||
|
initscript {
|
||||||
|
val spotlessVersion = "6.7.2"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath("com.diffplug.spotless:spotless-plugin-gradle:$spotlessVersion")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allprojects {
|
||||||
|
if (this == rootProject) {
|
||||||
|
return@allprojects
|
||||||
|
}
|
||||||
|
apply<com.diffplug.gradle.spotless.SpotlessPlugin>()
|
||||||
|
extensions.configure<com.diffplug.gradle.spotless.SpotlessExtension> {
|
||||||
|
kotlin {
|
||||||
|
target("**/*.kt")
|
||||||
|
targetExclude("**/build/**/*.kt")
|
||||||
|
ktlint(ktlintVersion).userData(mapOf("android" to "true"))
|
||||||
|
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
|
||||||
|
}
|
||||||
|
format("kts") {
|
||||||
|
target("**/*.kts")
|
||||||
|
targetExclude("**/build/**/*.kts")
|
||||||
|
// Look for the first line that doesn't have a block comment (assumed to be the license)
|
||||||
|
licenseHeaderFile(rootProject.file("spotless/copyright.kts"), "(^(?![\\/ ]\\*).*$)")
|
||||||
|
}
|
||||||
|
format("xml") {
|
||||||
|
target("**/*.xml")
|
||||||
|
targetExclude("**/build/**/*.xml")
|
||||||
|
// Look for the first XML tag that isn't a comment (<!--) or the xml declaration (<?xml)
|
||||||
|
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[^!?])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue