Cause: androidx.navigation.safeargs can only be used with an androidx project
A

9

29

I am facing issue with navigation component called safeargs.

I have used classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.0.0' in build gradle.

While applying plugin androidx.navigation.safeargs.kotlin in app/build.gradle, I am getting following error:

Cause: androidx.navigation.safeargs can only be used with an androidx project

app/build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'androidx.navigation.safeargs.kotlin'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.test.navigationexample"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
    implementation 'com.google.android.material:material:1.1.0-alpha05'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
}
Anyways answered 19/4, 2019 at 4:20 Comment(2)
Can you post your app/build.gradle?Cornstalk
@Cornstalk added build.gradle file. Please check it.Anyways
C
41

As per the Migrate to AndroidX, your gradle.properties file must contain the following lines:

android.useAndroidX=true
android.enableJetifier=true

That first line, android.useAndroidX=true, is what the androidx.navigation.safeargs.kotlin uses to ensure you are using an AndroidX project (it is also used by other tools in Android Studio to generate the right classes, such as when you are using any of the templates).

Cornstalk answered 19/4, 2019 at 4:56 Comment(1)
I already have this in my gradle.properties and I am still facing the issue. Any clues?Hypotrachelium
O
33

One other thing to remember is that apply plugin:"androidx.navigation.safeargs.kotlin" shouldn't be the top most apply on build.gradle.

This doesn't work:

apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

This works:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs.kotlin'
Olympie answered 15/3, 2020 at 12:32 Comment(0)
B
11

The only thing that worked for me was.

In top of build.gradle file for the project:

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

In the same file under plugins add:

plugins {
    ...
    id 'androidx.navigation.safeargs.kotlin' version '2.4.2' apply false
}

In the build.gradle file for Module, under plugins add:

plugins {
    ...
    id 'androidx.navigation.safeargs.kotlin'
}
Beware answered 4/5, 2022 at 13:44 Comment(0)
P
5

Put classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0" to your project build.gradle in buildscript.dependencies section.

But put apply plugin: 'androidx.navigation.safeargs' to your app build.gradle.

Perigee answered 15/7, 2020 at 20:33 Comment(0)
I
3

I think there is a bug. If your problem continues, you can try it like this. It worked for me too.

id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin'
Interdict answered 23/3, 2021 at 13:32 Comment(1)
This errors if you just replace 'apply plugin:' with 'id'. Remember now everyone knows what you are talking about. This needs to be wrapped in 'plugins {}' See this https://mcmap.net/q/501219/-apply-android-gradle-plugin-using-new-syntaxStromboli
P
2

Solution

Create gradle.properties file inside app (if not exist already) folder and add the content of this example file into it. Sync your project and it worked for me.

Example gradle.properties File

Chill Pill :)

Prerequisite answered 11/1, 2022 at 6:35 Comment(0)
K
1

If you have added the below lines in Gradle properties file then its okay

android.useAndroidX=true
android.enableJetifier=true

Please add this line to your project-level build.gradle

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"

or current updated version.

Kono answered 10/8, 2021 at 5:40 Comment(0)
C
1

Here is the full plugins block:

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

make sure id 'androidx.navigation.safeargs.kotlin' is at the last down position...that's how I did

Coppersmith answered 2/4, 2023 at 15:51 Comment(0)
G
0

If you have multiple modules, you have to put the classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5" into build.graddle in the root of project, not the one in the module. Then you can use id "androidx.navigation.safeargs" in the module.

Greggrega answered 3/3, 2022 at 14:22 Comment(1)
I have done all of these you said but problem still existVesica

© 2022 - 2024 — McMap. All rights reserved.