0

I am trying to follow a tutorial in order to implement and use a room database YouTube-link. My problem is that my gradle files look so different compared to what is shown in the video that I have no idea where to put the id "navigation.safeargs.kotlin" and the

classpath "androidx.navigation-safe-args-gradle-plugin:2.3.0-rc01"

or anything else that I might need. I have tried a bunch of places but it never works and I couldn't really find an answer, so if anyone knows, pls help me out.

build.gradle(Project:...):

plugins {
        id 'com.android.application' version '7.2.0' apply false
        id 'com.android.library' version '7.2.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

build.gradle(Module:...):

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example. ..."
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.exifinterface:exifinterface:1.3.3'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation fileTree(dir: "libs", include: ["*.jar"])
    // Room components
    implementation "androidx.room:room-runtime:2.4.2"
    kapt "androidx.room:room-compiler:2.4.2"
    implementation "androidx.room:room-ktx:2.4.2"
    androidTestImplementation "androidx.room:room-testing:2.4.2"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
}

settings.gradle(...):

    pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "..."
include ':app'

3 Answers 3

1

In your build.gradle (project level) try adding:

plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.5.1' apply false
}

and in the build.gradle (module level) add:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
}
0

Go straight to the source IMO

Top-level build.gradle:

buildscript {
    repositories {
        google()
    }
    dependencies {
        val nav_version = "2.4.2"
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
    }
}

App/module build.gradle:

plugins {
    id("androidx.navigation.safeargs")
    // or for Kotlin-only projects
    id("androidx.navigation.safeargs.kotlin")
}

So in your top-level file, you need google() in your repositories block, and the classpath in your dependencies block. If you don't have one of those blocks, just create it - they go inside the buildscript block, see? The nav_version val is just a variable so you can make all your Navigation components use the same version by referring to that, and the actual version number is only set in one place. You can just write the number in the dependency statement if you like - make sure all the Navigation components are using the same version though!

Stick the id("androidx.navigation.safeargs") line in your module's build file's plugins block. The order sometimes matters for some of these, so try it at the bottom. The syntax is a little different from how they look in your current file, but it's just another way of writing it - either's fine, you can mix and match

4
  • Thx a lot for the detailed answer :D
    – nikenzo
    Commented May 28, 2022 at 11:05
  • 1
    Took some time for me to understand what you meant but it worked in the end so thx a lot!
    – nikenzo
    Commented May 31, 2022 at 12:38
  • @nikenzo yeah gradle can be a bit weird at first, but you end up having to add and update things in there fairly often, so getting your head around the basics ends up pretty important! Glad you got it sorted out Commented May 31, 2022 at 18:00
  • any update if using KSP? I have a weird issue following these advices, it says: Cannot query the value of task ':app:compileDebugKotlin' property 'moduleName' because it has no value available. Commented Oct 30, 2023 at 16:28
0

Use this one:

id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.