Android app crashes on older devices with a missing java.nio.file class
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/nio/file/LinkOption;Short answer
The app uses a Java API from java.nio.file that the older Android version on the device does not have. Since Android Gradle Plugin 7.4.0, desugaring can supply a subset of those classes. Enable core library desugaring, add the desugar_jdk_libs dependency, and check that your plugin version is 7.4 or newer.
Fix it fast
1Find out which cause you have
Read the minSdk and desugaring settings in the app module. The crash happens only on devices older than the API level that introduced the class, so also note the Android version of the device that crashes.
grep -n "minSdk\|isCoreLibraryDesugaringEnabled\|coreLibraryDesugaring\|com.android.application" app/build.gradle*2Match what you see, then jump to its fix
- 1
The crash is on an older Android version, and the build file has no coreLibraryDesugaring dependencyCore library desugaring is not enabledFix - 2
Desugaring is enabled, but the plugin version in the project is below 7.4.0, or the desugar_jdk_libs version is 1.xThe Android Gradle Plugin is older than 7.4.0Fix
- 1
What does the error look like?
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/nio/file/LinkOption;NoClassDefFoundError: Failed resolution of: [Ljava/nio/file/LinkOption;
Cause 1 of 2
Core library desugaring is not enabled
1Check
Android documents that, with plugin 7.4.0 or higher, a subset of java.nio.file becomes available through desugaring, but only after you turn the feature on in the app module.
grep -n "coreLibraryDesugaring" app/build.gradle*2Fix
Enable the flag in compileOptions, add multiDexEnabled when minSdk is 20 or lower, and add the library dependency.
android {
defaultConfig {
multiDexEnabled = true // needed when minSdk is 20 or lower
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.3")
}3Verify
Rebuild and run on the older device or emulator that crashed.
./gradlew assembleDebugConfirmed by developer.android.com
Cause 2 of 2
The Android Gradle Plugin is older than 7.4.0
1Check
The java.nio.file subset arrives with plugin 7.4.0 and desugar_jdk_libs 2.0.x. The documented pairing is 2.0.3 with plugin 7.4 or higher; 1.2.3 is for 7.3 and 1.1.9 is for 4.0 to 7.2.
grep -rn "com.android.application\|desugar_jdk_libs" --include=*.gradle --include=*.kts --include=*.toml .2Fix
Upgrade the plugin to 7.4 or newer, then use desugar_jdk_libs 2.0.3 as in cause 1.
// gradle/libs.versions.toml (or the plugins block)
agp = "8.7.3"3Verify
Sync the project and rebuild. The version pair is accepted by the build.
./gradlew :app:assembleDebugConfirmed by developer.android.com
How do I stop it coming back?
- Turn on core library desugaring when you raise the Java language level or add a library that uses newer Java APIs.
- Run the app on an emulator at your minSdk before each release.
- Keep the Android Gradle Plugin current, as the documentation ties desugar_jdk_libs versions to plugin versions.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix