Home > database >  Using safeargs in Android Studio causes build to fail due to duplicate classes
Using safeargs in Android Studio causes build to fail due to duplicate classes

Time:11-04

Before anyone asks, I have read all of these questions and none of them fixed my issue: 1, 2, 3, 4, 5, 6.

I am currently learning Android app development and I have been following through the first tutorial on the Android Developer site. I created my project using the "Basic Activity" template (as instructed).
In step 5 on the page linked above, it says to enable safe args by doing the following:

Open Gradle Scripts > build.gradle (Module: My First App) Find the dependencies section in the buildscript section, and add the following lines after the other classpath entries:
def nav_version = "2.3.0-alpha02"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

Open Gradle Scripts > build.gradle (Module: app) Just below the other lines that begin with apply plugin add a line to enable SafeArgs:
apply plugin: 'androidx.navigation.safeargs.kotlin'

Android Studio should display a message about the Gradle files being changed. Click Sync Now on the right hand side. After a few moments, Android Studio should display a message in the Sync tab that it was successful.

Choose Build > Make Project. This should rebuild everything so that Android Studio can find FirstFragmentDirections.

Unfortunately, the "Basic Activity" template in my version of Android Studio (see below) doesn't seem to be structured in a way that supports the changes in the instructions.

Android Studio Dolphin | 2021.3.1 Patch 1
Build #AI-213.7172.25.2113.9123335, built on September 30, 2022
Runtime version: 11.0.13 0-b1751.21-8125866 amd64

Firstly, it says to "find the dependencies section in the buildscript section". In my build.gradle (:app) file (I'm not even sure if this is the right file), there is just a top-level dependencies section containing implementation entry. Adding a classpath entry doesn't seem correct.

Next, it says to add the apply plugin entry to my (Module: app) file "just below the other lines that begin with apply plugin". No such lines exist, which makes be suspect the tutorial is out of date.


After a bit of searching, I came to the conclusion that I needed to add safeargs in a different way. In my build.gradle project file, I've made it look like so (this is the full file):

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.5.3' apply false  // I added this
}

And the top of my build.gradle module file looks like this:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id "androidx.navigation.safeargs.kotlin" // I added this
}

There are no other changes to the files.

Syncing works fine, but I run into issues when I try to rebuild (even if I clean first). I find that I always get the error below, after the checkDebugDuplicateClasses check fails:

Duplicate class javax.activation.ActivationDataFlavor found in modules jakarta.activation-api-1.2.1 (jakarta.activation:jakarta.activation-api:1.2.1) and javax.activation-1.2.0 (com.sun.activation:javax.activation:1.2.0)
Duplicate class javax.activation.CommandInfo found in modules jakarta.activation-api-1.2.1 (jakarta.activation:jakarta.activation-api:1.2.1) and javax.activation-1.2.0 (com.sun.activation:javax.activation:1.2.0)
Duplicate class javax.activation.CommandInfo$Beans found in modules jakarta.activation-api-1.2.1 (jakarta.activation:jakarta.activation-api:1.2.1) and javax.activation-1.2.0 (com.sun.activation:javax.activation:1.2.0)

[...]

Duplicate class org.xmlpull.v1.XmlPullParser found in the following modules: kxml2-2.3.0 (net.sf.kxml:kxml2:2.3.0), xmlpull-1.1.3.1 (xmlpull:xmlpull:1.1.3.1) and xpp3-1.1.4c (xpp3:xpp3:1.1.4c)
Duplicate class org.xmlpull.v1.XmlPullParserException found in the following modules: kxml2-2.3.0 (net.sf.kxml:kxml2:2.3.0), xmlpull-1.1.3.1 (xmlpull:xmlpull:1.1.3.1) and xpp3-1.1.4c (xpp3:xpp3:1.1.4c)
Duplicate class org.xmlpull.v1.XmlPullParserFactory found in the following modules: kxml2-2.3.0 (net.sf.kxml:kxml2:2.3.0), xmlpull-1.1.3.1 (xmlpull:xmlpull:1.1.3.1) and xpp3-1.1.4c (xpp3:xpp3:1.1.4c)
Duplicate class org.xmlpull.v1.XmlSerializer found in the following modules: kxml2-2.3.0 (net.sf.kxml:kxml2:2.3.0), xmlpull-1.1.3.1 (xmlpull:xmlpull:1.1.3.1) and xpp3-1.1.4c (xpp3:xpp3:1.1.4c)

Go to the documentation to learn how to Fix dependency resolution errors.

It is unclear what needs to be done from here. I am unable to run the app in the emulator at all because the build completely fails. Any suggestions would be appreciated.

CodePudding user response:

I don't know when exactly I did this, but I made a change to the end of my build.gradle app file so it looks like the following.

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3'  // <--- this line
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

The line I point out here is the culprit. This must've been causing something to double up somewhere along the line. Removing this line should fix the issue.

I found this github repo useful (link to internet archive, with relevant files archived). The build.gradle and app/build.gradle files is where you should look.

  • Related