Search code examples
androidgradle

Android using new version catalog to add local modules


I've created a new project with new libs.versions.toml file, then added a core module, then I've added it as dependency, into app module's build.gradle.kts file using the following code:

implementation(project(":core"))
// other libs using version catalog:
implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)

Everything works fine, now I'm looking for a way to add the :core module inside the libs.versions.toml file and unify adding dependencies, think about :core:data module for instance, and implementation like it:

implementation(libs.local.core.data)

Solution

  • Finally, I came up with a solution that can be applied to non-version-catalog projects as well:

    1- in your settings.gradle(.kts) add enableFeaturePreview somewhere at the root block of the file:

    enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") // <- Add this line
    
    rootProject.name = "MyApplication"
    include(":app")
    include(":core:data") // <- Sample local module
    

    2- Now you'll be able to have AutoCompilation for all local modules, using the projects keyword. Then use it like this (in app build.gradle(.kts)):

    dependencies {
        implementation(projects.core.data) // <- Now add your own module like this
    
        implementation(libs.androidx.core.ktx)
        // ...
    }