DEV Community

junian
junian

Posted on • Originally published at junian.net on

Troubleshooting Flutter Android App to Target SDK 35 Upgrade

Another year, another chore: upgrading the Android app target SDK. This year, Google requires Target SDK 35.

So, here’s my experience upgrading an existing Flutter Android app. There were a few issues, but every problem has a solution.

Note: At the time of writing this tutorial, I was using Flutter v3.10.6.

Set Target SDK to 35

First, update the Android target SDK version manually by editing the android/local.properties file:

-flutter.compileSdkVersion=34
+flutter.compileSdkVersion=35
-flutter.targetSdkVersion=34
+flutter.targetSdkVersion=35
 flutter.minSdkVersion=21
 flutter.ndkVersion=26.1.10909125
Enter fullscreen mode Exit fullscreen mode

After updating, build the Android bundle:

flutter build appbundle
Enter fullscreen mode Exit fullscreen mode

As expected, some issues appeared.

Update Gradle to 8.5

The first error was RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processReleaseResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction
   > Android resource linking failed
     aapt2 E 09-30 01:01:15 26752 18645483 LoadedArsc.cpp:94] RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.
     aapt2 E 09-30 01:01:15 26752 18645483 ApkAssets.cpp:149] Failed to load resources table in APK '/opt/homebrew/share/android-commandlinetools/platforms/android-35/android.jar'.
     error: failed to load include path /opt/homebrew/share/android-commandlinetools/platforms/android-35/android.jar.
Enter fullscreen mode Exit fullscreen mode

This happened because I was still using Gradle v7.3. Upgrade to at least v8.5 (I chose v8.9):

cd android/
./gradlew wrapper --gradle-version=8.9
Enter fullscreen mode Exit fullscreen mode

Then update the Gradle plugin version in android/build.gradle:

dependencies {
-    classpath 'com.android.tools.build:gradle:7.3.0'
+    classpath 'com.android.tools.build:gradle:8.5.0'
    // ...
}
Enter fullscreen mode Exit fullscreen mode

Rebuild the app after updating.
Naturally, another issue came up. No surprises there.

Flutter In-App WebView Issue

Next, an error related to :flutter_inappwebview appeared:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':flutter_inappwebview'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file: /Users/junian/.pub-cache/hosted/pub.dev/flutter_inappwebview-5.8.0/android/build.gradle. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

     If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.
Enter fullscreen mode Exit fullscreen mode

Based on this Stack Overflow answer, add the following subprojects section to android/build.gradle:

// ...

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                if (namespace == null) {
                    namespace project.group
                }
            }
        }
    }
}

// ...
Enter fullscreen mode Exit fullscreen mode

The journey was not over. There were more issues ahead.

Java 8 Desugaring Support

If your project uses Java 8, you may see:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkReleaseAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > An issue was found when checking AAR metadata:

       1.  Dependency ':flutter_local_notifications' requires core library desugaring to be enabled
           for :app.

           See https://developer.android.com/studio/write/java8-support.html for more
           details.
Enter fullscreen mode Exit fullscreen mode

Enable desugaring by setting coreLibraryDesugaringEnabled to true and adding the dependency in android/app/build.gradle:

android {
    // ...

    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // ...
}

// ...

dependencies {
    // ...

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
}
Enter fullscreen mode Exit fullscreen mode

One final problem ahead!

Kotlin Version Update

Another issue when building the Android bundle was something to do with Kotlin version.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileReleaseKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details
Enter fullscreen mode Exit fullscreen mode

At the end of the error message, there was a note:

[!] Your project requires a newer version of the Kotlin Gradle plugin.                                                                                             │
Find the latest version on https://kotlinlang.org/docs/releases.html#release-details, then update /Users/junian/Projects/flutter-app/android/build.gradle: 
ext.kotlin_version = '<latest-version>'
Enter fullscreen mode Exit fullscreen mode

The solution was to update the Kotlin version from v1.7 to v1.9 in the buildscript section of android/build.gradle:

buildscript {
-    ext.kotlin_version = '1.7.10'
+    ext.kotlin_version = '1.9.25'
}
Enter fullscreen mode Exit fullscreen mode

Let's see if there was another bug to smash.

Final Build. Finally!

With all fixes applied, I could finally build the Android bundle successfully:

$ flutter build appbundle
Running Gradle task 'bundleRelease'...                             52.0s
✓ Built build/app/outputs/bundle/release/app-release.aab (39.7MB).
Enter fullscreen mode Exit fullscreen mode

I submitted the new .aab release to Google Play and waiting for review.

Conclusion

Thanks for reading! I hope your upgrade to Android Target SDK 35 goes smoothly.

If you have questions or want to discuss, feel free to leave a comment below.

Thank you and see you next time!

References

Top comments (0)