I am new to flutter development and so far I have been testing the android app in debug mode. I want to release the app to share with friends and family. However, I keep getting errors when I try to build the apk (release).
To be honest, I do not fully understand the build.gradle files; hence I am reaching out here for any help, thank you.
I am on Android 14 and I intend to only support Android 14+.
I have downloaded the SDK's for API level 34 and 35 from Android Studio.
My app is entirely on Flutter's dart.
This is the error while building the apk (release):
Note: Recompile with -Xlint:deprecation for details.
Font asset "MaterialIcons-Regular.otf" was tree-shaken, reducing it from 1645184 to 4212 bytes (99.7% reduction). Tree-shaking can be disabled by providing the --no-tree-shake-icons flag when building your app.
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 07-22 17:30:35 77990 77990 LoadedArsc.cpp:94] RES_TABLE_TYPE_TYPE entry offsets overlap actual entry data.
aapt2 E 07-22 17:30:35 77990 77990 ApkAssets.cpp:149] Failed to load resources table in APK '/home/username/Android/Sdk/platforms/android-35/android.jar'.
error: failed to load include path /home/username/Android/Sdk/platforms/android-35/android.jar.
android/build.gradle
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = "../build"
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(":app")
}
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
android/app/build.gradle
plugins {
id "com.android.application"
id 'com.google.gms.google-services'
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader("UTF-8") { reader ->
localProperties.load(reader)
}
}
android {
namespace = "com.example.flutter_application_1"
compileSdk = 35 // max SDK available today
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId = "com.example.flutter_application_1"
minSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
buildTypes {
release {
signingConfig = signingConfigs.debug
}
}
}
flutter {
source = "../.."
}
I've been able to reproduce this issue with compileSdk = 35
(and other sdk-related fields) and agp 7.3.0
.
In my case, updating Android Gradle Plugin up to latest 8.5.0
solved this problem.
To do this, you should:
gradle
to compatible (or latest) version. In my case it was 8.9.0
./gradlew wrapper --gradle-version=8.9
settings.gradle
plugins {
...
id "com.android.application" version "8.5.0" apply false
...
}
After this changes my build completes successfully. However there is still warning in build log:
WARNING: We recommend using a newer Android Gradle plugin to use compileSdk = 35
This Android Gradle plugin (8.5.0) was tested up to compileSdk = 34.
You are strongly encouraged to update your project to use a newer
Android Gradle plugin that has been tested with compileSdk = 35.
If you are already using the latest version of the Android Gradle plugin,
you may need to wait until a newer version with support for compileSdk = 35 is available.
For more information refer to the compatibility table:
https://d.android.com/r/tools/api-level-support
To suppress this warning, add/update
android.suppressUnsupportedCompileSdk=35
to this project's gradle.properties.
I suppose that's because sdk 35 was released in stable channel after agp 8.5.0
and in nearest version of agp this warning will be fixed.
That said, I want to warn OP and anyone who will read this. Flutter has own map of compatible versions of android-platform dependencies here. I do believe that it is wise to stick to these versions if there're not some very special requirements in your project.
Using flutter's compatible versions will provide you with (at least, somehow) tested configuration, and it will be updated when new version of Flutter will support new platforms.
You can use these versions like this:
android {
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
defaultConfig {
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
}
}