io.github.lyxnx.gradle.android.BaseAndroidPlugin.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-android Show documentation
Show all versions of plugin-android Show documentation
Plugins to reduce boilerplate in Android modules
@file:Suppress("UnstableApiUsage")
package io.github.lyxnx.gradle.android
import io.github.lyxnx.gradle.KradlePlugin
import io.github.lyxnx.gradle.android.internal.AndroidCommonExtension
import io.github.lyxnx.gradle.android.internal.android
import io.github.lyxnx.gradle.android.internal.androidComponents
import io.github.lyxnx.gradle.android.internal.test
import io.github.lyxnx.gradle.kotlin.dsl.configureKotlin
import io.github.lyxnx.gradle.kotlin.setTestOptions
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getPlugin
public abstract class BaseAndroidPlugin internal constructor() : KradlePlugin() {
protected val configPlugin: AndroidConfigPlugin
get() = project.plugins.getPlugin(AndroidConfigPlugin::class)
protected fun Project.applyBasePlugin(pluginId: String) {
val configPlugin = plugins.apply(AndroidConfigPlugin::class)
apply {
plugin(pluginId)
plugin("kotlin-android")
}
if (configPlugin.hasCacheFixPlugin) {
apply(plugin = CACHE_FIX_PLUGIN)
} else {
logger.warn(
"""
Could not find Android cache fix plugin. It is recommended to apply this plugin, but not required.
(See https://github.com/gradle/android-cache-fix-gradle-plugin)
If it would like to be used, make sure it is added as a build dependency
E.g. Add using apply false to the root project:
plugins {
id("$CACHE_FIX_PLUGIN") version "" apply false
}
""".trimIndent()
)
}
configureKotlin(configPlugin.jvmTarget)
configureAndroid()
androidComponents {
finalizeDsl { extension ->
configPlugin.run {
extension.applyAndroidOptions(
options = androidOptions,
javaVersion = jvmTarget
)
filterTestTaskDependencies(androidOptions)
}
}
}
}
private fun Project.configureAndroid() = android {
buildFeatures {
aidl = false
shaders = false
renderScript = false
}
}
private fun AndroidCommonExtension.applyAndroidOptions(
options: AndroidOptions,
javaVersion: Provider
) {
setCompileSdkVersion(options.compileSdk.get())
defaultConfig {
minSdk = options.minSdk.get()
}
compileOptions {
sourceCompatibility = javaVersion.get()
targetCompatibility = javaVersion.get()
}
testOptions {
unitTests.all { it.setTestOptions(options.test) }
}
}
private fun AndroidCommonExtension.setCompileSdkVersion(version: String) {
val intVersion = version.toIntOrNull()
if (intVersion != null) {
compileSdk = intVersion
} else {
compileSdkPreview = version
}
}
private fun Project.filterTestTaskDependencies(options: AndroidOptions) {
afterEvaluate {
tasks.named("test") {
val testTasksFilter = options.testTasksFilter.get()
setDependsOn(dependsOn.filter { it !is TaskProvider<*> || testTasksFilter(it) })
}
}
}
}