Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
}
like image 628
dreamcoder Avatar asked Apr 19 '19 04:04

dreamcoder


2 Answers

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).

like image 185
ianhanniballake Avatar answered Oct 04 '22 04:10

ianhanniballake


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'
like image 21
Diego Oliveira Avatar answered Oct 04 '22 03:10

Diego Oliveira