Home > Back-end >  Share logic between application module and library module
Share logic between application module and library module

Time:02-01

I have module.gradle.kts :

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        ...
    }
}

And app.gradle.kts:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        ...
    }
}

I would like to define a common configuration of android.defaultConfig for both app.gradle.kts and module.gradle.kts in a dedicated gradle file, such as root.gradle.kts:

plugins {
    // What plugin to use here since this is neither an application module nor a library module ?
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        // Common configuration to be used by app.gradle.kts and module.gradle.kts
    }
}

Is there a common ancestor to "com.android.application" and "com.android.library" that can be used in that case?

CodePudding user response:

I've defined my own extension function in buildSrc/src/main/Extensions.kt:

import com.android.build.api.dsl.CommonExtension

fun CommonExtension<*, *, *, *>.applyRootConfiguration() {
    defaultConfig {
        // ...
    }
}

Then I can apply it in an application or a library by doing:

android {
    applyRootConfiguration()
}
  • Related