Search code examples
androidgradlebuildmoduledependencies

How to get dependencies in module from object class in buildSrc module


I Have structure: module app gradle :

import app.buildsrc.Libs

plugins {
    kotlin("jvm")
}

dependencies {
    implementation(Libs.material3)
}

and additional gradle module buildSrc:

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
    google()
}

And buildSrc open class with link to dependancies variable (Dependancies.kt):

package app.buildsrc

object Libs {
    const val material3 = "androidx.compose.material3:material3:1.1.2"
}

But then I try to sync the project I have got an error:

:app:main: Could not resolve androidx.compose.material3:material3:1.1.2.


Solution

  • That it does not work is because you do not define any repositories in your main build.

    That you define repositories in your buildSrc build script is irrelevant, those repositories are just for building the buildSrc build, not your main build.

    For your main build, I recommend you declare your repositories in dependencyResolutionManagement { repositories { ... } } in your settings script uniformly for all projects in that build.

    Besides that you should have a look at version catalogs which are the idiomatic way to centrally declare versions which has various advantages over a class in buildSrc.