2

Gradle - 8.2.1, AGP - 8.0.2, Kotlin - 1.9.0

buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
}
dependencies {
    implementation("com.android.tools.build:gradle-api:8.0.2")
}

gradle/support/common-app-config.gradle.kts

plugins {
    id("com.android.application")
    ...
}
android {
    ...
}
dependencies {
    implementation(project(":feature-one"))
    implementation(project(":feature-two"))
    ...
}

Similarly, gradle/support/common-lib-config.gradle.kts

plugins {
    id("com.android.library")
    ...
}
android {
    ...
}
dependencies {
    implementation(project(":platform"))
    ...
}

Also, gradle/support/common-dependencies.gradle.kts

dependencies {
    implementation("androidx", "lifecycle", "navigation"... )
    /** You get the gist of this block **/
    ...
}

Is there a clean approach to centralizing the android {} blocks for application and library, separately is still fine, as well as dependencies {} block for re-use ?

So far, app/build.gradle.kts

apply {
    from(file("${rootProject.projectDir}/gradle/support/common-app-config.gradle.kts"))
}

fails with "Unresolved reference : android", despite declaring the "com.android.application" plugin in the common-app-config.gradle.kts file.

4
  • Why have you put the convention plugins in the gradle/support/ directory? Usually they go in buildSrc/src/main/kotlin/ Commented Jul 18, 2023 at 6:25
  • Version-catalogs appear to be visible only to outer build-script files - ./build.gradle.kts, ./settings.gradle.kts, ./buildSrc/**.gradle.kts, ( Convention plugins folder, let's say ) ./gradle/support/**.gradle.kts, but version-catalogs are not visible inside the convention plugins folders ./gradle/support/src/main/kotlin/*, primarily because kotlin-dsl extract plugins {} block from ./gradle/support/src/main/kotlin/**.gradle.kts files into ./gradle/support/build/kotlin-dsl/plugins-blocks/extracted/**.gradle.kts, and that's where version-catalogs aren't resolving ? Commented Jul 18, 2023 at 17:35
  • Wants to implement similar functionality in my project too Commented Nov 2, 2023 at 2:09
  • I'm interested in this topic too. What I'm doing is to check in the project if has the app or the library plugin with project.plugins.hasPlugin("com.android.application"), and depending on that applying a gradle script file or another (In this gradle file I can apply the library or application plugins to access the android). But in the end I need to have the android part duplicated. In Groovy you can do this very easily. Commented Jan 9, 2024 at 15:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.