The release build dies inside R8 with a NullPointerException
Execution failed for task ':app:minifyReleaseWithR8'.Short answer
A NullPointerException inside minifyReleaseWithR8 is almost always a bug in the R8 copy that ships with your Android Gradle plugin, not in your code. Run the release build with --stacktrace, match the R8 class or message against the fixed-issue lists, and move AGP to the patch release that names it.
Fix it fast
1Find out which cause you have
The short Gradle message hides the part you need. Run the release build again with the full stack trace and read the first line that mentions com.android.tools.r8.
./gradlew :app:assembleRelease --stacktrace2Match what you see, then jump to its fix
- 1
NullPointerException: Cannot invoke "com.android.tools.r8.ir.analysis.type.TypeElement.asClassType()"R8 in AGP 8.6.0 crashes on TypeElement.asClassType()Fix - 2
R8 NullPointer started right after the Kotlin version went to 2.0R8 in AGP 8.6.0 or older fails after a move to Kotlin 2.0Fix - 3
Cannot read field "b" because the return value of "com.android.tools.r8.internal.BS.a(...)" is nullR8 in AGP 8.8.0 returns null inside an internal classFix
- 1
What does the error look like?
Execution failed for task ':app:minifyReleaseWithR8'.java.lang.NullPointerExceptionCannot invoke "com.android.tools.r8.ir.analysis.type.TypeElement.asClassType()"R8: java.lang.NullPointerException: Cannot read field "b" because the return value of "com.android.tools.r8.internal.BS.a(com.android.tools.r8.internal.cR)" is null
Cause 1 of 3
R8 in AGP 8.6.0 crashes on TypeElement.asClassType()
1Check
Print the AGP version the project really uses. The Android release notes list this exact NullPointerException (issue 358913905) as fixed in AGP 8.6.1.
./gradlew buildEnvironment | grep -i "com.android.tools.build:gradle"2Fix
Raise the plugin to 8.6.1 or later in the root build file. Keep Gradle at or above the version that AGP release lists as its minimum.
// build.gradle.kts (root)
plugins {
id("com.android.application") version "8.6.1" apply false
}3Verify
Run the release build again. R8 should finish and write the app bundle or APK.
./gradlew :app:assembleReleaseConfirmed by developer.android.com
Cause 2 of 3
R8 in AGP 8.6.0 or older fails after a move to Kotlin 2.0
1Check
Look at the Kotlin plugin version in git history. If the failure began in the same commit that raised Kotlin to 2.0 while AGP stayed below 8.6.0, this is the case the release notes list as fixed in 8.6.0 (issue 353475583).
git log -p -S"org.jetbrains.kotlin" -- build.gradle.kts gradle/libs.versions.toml2Fix
Upgrade AGP to 8.6.0 or later so the newer R8 is used.
// gradle/libs.versions.toml
[versions]
agp = "8.6.1"3Verify
Build the release variant again.
./gradlew :app:assembleReleaseConfirmed by developer.android.com
Cause 3 of 3
R8 in AGP 8.8.0 returns null inside an internal class
1Check
The 8.8.1 release notes list this message under issue 389508413 (a release build failing after a move to AGP 8.8). If your AGP is 8.8.0, this is the one.
./gradlew buildEnvironment | grep -i "com.android.tools.build:gradle"2Fix
Upgrade to AGP 8.8.1 or newer. The 8.8.1 notes also list a separate NPE in the R8 Enqueuer (issue 381812767), so the patch release covers both.
// build.gradle.kts (root)
plugins {
id("com.android.application") version "8.8.2" apply false
}3Verify
Run the release build again.
./gradlew :app:assembleReleaseConfirmed by developer.android.com
How do I stop it coming back?
- Take AGP patch releases (8.6.1, 8.8.1 style) when they ship. the fixed-issue lists show R8 fixes landing in them.
- Build the release variant in CI on every merge so an R8 crash shows up the day it starts, not on release day.
- Keep the full stack trace from --stacktrace in the bug report if you file one.
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