I am currently migrating my project from Groovy to Kotlin DSL, and as it is multi-module, I have chosen to use Convention Plugins.
The problem is that the minSdk is not being applied correctly. I have two main modules, one for the mobile and tablet app and one for wear. In the app module I use the following plugin:
u/Suppress("UnstableApiUsage")
class ApplicationsConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
}
extensions.configure<ApplicationExtension> {
configureKotlinAndroid(commonExtension = this)
defaultConfig {
versionCode = APP_VERSION_CODE
versionName = VERSION_NAME
targetSdk = APP_TARGET_SDK_VERSION
buildFeatures {
buildConfig = true
viewBinding = true
}
}
buildTypes {
AndroidBuildType.values().forEach {
maybeCreate(it.name).apply {
it.initWith?.let {
initWith(getByName(name))
}
applicationIdSuffix = it.applicationSuffixId
isDefault = it.isDefault
isDebuggable = it.isDebuggable
isMinifyEnabled = it.isMinifyEnabled
isShrinkResources = it.isShrinkResources
}
}
}
testOptions {
unitTests {
isIncludeAndroidResources = true
isReturnDefaultValues = true
}
animationsDisabled = true
}
}
}
}
}
And for the wear module, I use the following plugin:
@Suppress("UnstableApiUsage")
class WearableApplicationsConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
}
extensions.configure<ApplicationExtension> {
configureWearableKotlinAndroid(commonExtension = this)
defaultConfig {
versionCode = WEARABLE_VERSION_CODE
versionName = VERSION_NAME
targetSdk = WEARABLE_TARGET_SDK_VERSION
buildFeatures {
buildConfig = true
viewBinding = true
}
}
buildTypes {
AndroidBuildType.values().forEach {
maybeCreate(it.name).apply {
it.initWith?.let {
initWith(getByName(name))
}
applicationIdSuffix = it.applicationSuffixId
isDefault = it.isDefault
isDebuggable = it.isDebuggable
isMinifyEnabled = it.isMinifyEnabled
isShrinkResources = it.isShrinkResources
}
}
}
testOptions {
unitTests {
isIncludeAndroidResources = true
isReturnDefaultValues = true
}
animationsDisabled = true
}
}
}
}
}
Both plugins have a very similar method which is configureKotlinAndroid and configureWearableKotlinAndroid, in which, as I saw in the repository of NowInAndroid, within the defaultConfig block, apply the minSdk, where in my case, I apply on one side a minSdk of 26 for app and a minSdk of 30 for Wear OS, since a lower version I can not put in Wear OS.
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = COMPILE_SDK_VERSION
defaultConfig {
minSdk = 26
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
lint {
abortOnError = true
}
}
configureKotlin<KotlinAndroidProjectExtension>()
}
internal fun Project.configureWearableKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = COMPILE_SDK_VERSION
defaultConfig {
minSdk = 30
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
lint {
abortOnError = true
}
}
configureKotlin<KotlinAndroidProjectExtension>()
}
The problem is that when I'm trying to compile the project I'm finding that the minSdk is not being applied properly because Wear OS modules are failing due to the fact that the libraries require a minSdk of 30 and I'm using 26, so instead of taking the wear module it's taking the app one.
Here I have two questions, the first one: if I'm compiling the app module, why does it also compile the wearable one?
And, why doesn't it work if I have specified the minSdk to 30 in Wear OS? Also, if I put the minSdk directly in the app build.gradle.kts file and in the wearable build.gradle.kts file, so it works, what's different?