Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

The release build dies inside R8 with a NullPointerException

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

  • Applies to Android Gradle Plugin 8.6 to 8.8, Gradle 8.x, JDK 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --stacktrace
  2. 2Match what you see, then jump to its fix

    1. 1NullPointerException: Cannot invoke "com.android.tools.r8.ir.analysis.type.TypeElement.asClassType()"R8 in AGP 8.6.0 crashes on TypeElement.asClassType()Fix
    2. 2R8 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. 3Cannot 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

What does the error look like?

  • Execution failed for task ':app:minifyReleaseWithR8'.
  • java.lang.NullPointerException
  • Cannot 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:assembleRelease

Confirmed by developer.android.com

Did this fix it?

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

2Fix

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:assembleRelease

Confirmed by developer.android.com

Did this fix it?

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:assembleRelease

Confirmed by developer.android.com

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.