Search code examples
javaandroidandroid-roomandroid-architecture-navigationandroid-navigation-editor

Jetpack Navigation - How to handle dependency warning when adding nav_graph.xml?


I want to use the new navigation library of android jetpack. As I wanted to add a navigation XML to my project I got an error saying:

This operation requires the libraries android.arch.navigation:navigation-fragment:+, android.arch.navigation:navigation-ui:+.

Problem: Inconsistencies in the existing project dependencies found. Version incompatibility between: - android.arch.persistence.room:runtime:1.1.1 and: - com.android.support:appcompat-v7:27.1.1

With the dependency: - com.android.support:support-annotations:26.1.0 versus: - com.android.support:support-annotations:27.1.1

The project may not compile after adding these libraries. Would you like to add them anyway?

I hit "cancel" since I didn't know what the consequences for my little project would be. Why is that happening? Is room not compatible with the navigation library? Do I risk that my project will not compile after adding those? Should I save the gradle file and just try?

I would be very grateful for clarification. Thank you:)

My gradle file:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "de.test"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:recyclerview-v7:27.1.1'

    // Room components
    implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
    annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
    androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
    annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
}

Solution

  • There's two parts to this:

    1) The dialog is incorrect. The current Android Studio looks at test dependencies, which are included in the POM files, but don't affect your app (the test dependencies are only used internally for the tests testing the libraries themselves). This is partially fixed in Android Studio 3.5 Beta 1.

    2) Your app would not compile when you add the Navigation dependencies because you use compileSdkVersion 27 and the android.arch.navigation dependencies depends on version 28.0.0 of the Support Library, which requires that you compile with API 28.

    If you fix your app to use compileSdkVersion 28, then you can safely ignore warning of the dialog and add Navigation to your project. You can, of course, upgrade your Support Library to 28.0.0 prior to adding Navigation, which will ensure that there's no other unrelated to Navigation behavior changes caused by upgrading from 27.1.1 to 28.0.0.