Search code examples
androidkotlingradlegroovyandroid-jetpack-compose

Cannot find 'libs' in build.gradle.kts


I am trying to migrate a gradle build from Groovy DSL to Kotlin DSL. In this, I have managed to convert nearly all the files, but my compiler cannot recognise the keyword libs. I have used libs many times in my previous projects, but now libs is not accessible. (I am very new so correct me if I have anything wrong)
Here is my project build.gradle.kts:

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.kotlin.android) apply false
    alias(libs.plugins.ksp) apply false
    alias(libs.plugins.kotlin.compose) apply false
}

And here iş my app build.gradle.kts

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

android {
    namespace = "compose.project.garbagebin"
    compileSdk = 35

    defaultConfig {
        applicationId = "compose.project.garbagebin"
        minSdk = 24
        targetSdk = 35
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
    }
    buildFeatures {
        compose = true
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.fragment)
    implementation(libs.androidx.biometric)
    implementation(libs.androidx.lifecycle.viewmodel.compose)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

And here is my settings.gradle.kts:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "SQLDemo"
include(":app")

This only results in the project build.gradle.kts, not in the app build.gradle.kts. Why?

I tried converting the file, and changing the syntax, and importing modules through the libs main module, but for some reason it failed. I expected the build to succeed, as all syntax was correct. But it did not, and it gave these errors. It also gave a org.gradle.internal.exceptions.LocationAwareException saying the same thing. Why did the error only result in 1 file and how can I resolve it?


Solution

  • @Michael's answer worked: creating a libs.versions.toml according to this official Android link. Thanks @Michael!