Error Launching Flutter App After Upgrading to SDK Version 3.24
Asked Answered
N

8

8

I upgraded the SDK to Flutter version 3.24 yesterday, but today I'm encountering an error when trying to launch the app. I added the new version of Kotlin and changed the kotlin-stdlib-jdk7 version, but the result didn't change. Do you have any suggestions for a solution?

Error messages:

AAPT: error: resource android:attr/lStar not found.

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 the version number of the plugin with id "org.jetbrains.kotlin.android" in the plugins block of path

settings

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.2.2" apply false
    id "org.jetbrains.kotlin.android" version "1.9.25" apply false
    id "com.google.gms.google-services" version "4.3.15" apply false
}

app/gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.22"
    implementation 'com.google.android.gms:play-services-location:21.1.0'
    implementation "org.jetbrains.kotlin:kotlin-script-runtime:1.9.0"
    implementation 'com.android.support:multidex:2.0.1'
}

configurations.all {
    resolutionStrategy {
        eachDependency {
            if ((requested.group == "org.jetbrains.kotlin")
                    &&(requested.name.startsWith("kotlin-stdlib"))) {
                useVersion("2.0.10")
            }
        }
    }
}
Nystagmus answered 9/8 at 11:18 Comment(2)
Also, the error says you need a newer Kotlin Gradle Plugin, so updating the Kotlin stdlib is not what it demands. Try with a newer version of the Kotlin Gradle Plugin.Aftersensation
I tried this solution first, but it didn't fix the problem.Nystagmus
N
4

I solved it by downgrading the version to 3.22.2. It's not a complete solution.

flutter downgrade 3.22.2

Those who did not have problems with the previous version can use it.

Nystagmus answered 9/8 at 12:20 Comment(0)
W
4

There is a safer way to fix this error, suggested by a member of the Flutter team

subprojects {
    afterEvaluate { project ->
        if (project.extensions.findByName("android") != null) {
            Integer pluginCompileSdk = project.android.compileSdk
            if (pluginCompileSdk != null && pluginCompileSdk < 31) {
                project.logger.error(
                        "Warning: Overriding compileSdk version in Flutter plugin: "
                                + project.name
                                + " from "
                                + pluginCompileSdk
                                + " to 31 (to work around https://issuetracker.google.com/issues/199180389)."
                                + "\nIf there is not a new version of " + project.name + ", consider filing an issue against "
                                + project.name
                                + " to increase their compileSdk to the latest (otherwise try updating to the latest version)."
                )
                project.android {
                    compileSdk 31
                }
            }
        }
    }
    
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(":app")
}

You can check his post in the closed issue of flutter repo : https://github.com/flutter/flutter/issues/153281

Weightless answered 23/8 at 8:21 Comment(0)
N
3
  1. First, open your build.gradle file and update the Kotlin version to a more recent compatible one by changing the line: ext.kotlin_version = '1.5.31' or any other compatible version.

  2. Next, navigate to android/gradle/gradle-wrapper.properties and update the distributionUrl to match the Gradle version compatible with your Kotlin version. For example, change the line to: distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-rc-1-all.zip.

You can also follow the provided link for guidance on how to accomplish these steps using Android Studio: Android Java Gradle Migration Guide.

Finally, perform a flutter clean.

Nisus answered 10/8 at 10:0 Comment(0)
U
3

As mentioned by erdemyerebasmaz in this issue, this can be temporarily solved by adding the following code inside the subprojects block in android/build.gradle file

afterEvaluate { project ->
   if (project.plugins.hasPlugin("com.android.application") ||
           project.plugins.hasPlugin("com.android.library")) {
       project.android {
           compileSdkVersion 34
           buildToolsVersion "34.0.0"
       }
    }
}

android/build.gradle file should look like this

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    /// Temporary fix for apk release build failing after upgrading to flutter 3.24.0
    afterEvaluate { project ->
       if (project.plugins.hasPlugin("com.android.application") ||
               project.plugins.hasPlugin("com.android.library")) {
           project.android {
               compileSdkVersion 34
               buildToolsVersion "34.0.0"
           }
        }
    }
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
     
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}
Unwitting answered 14/8 at 10:29 Comment(0)
C
3

In my case with Flutter 3.24.0, I created a project with flutter < 3.19.0, to solve the error I changed in the gradle the code of the sub projects.

  1. Enter android/build.gradle at android level

    project
     | android
       |-build.gradle
    
  2. Change this

    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
    }
    subprojects {
        project.evaluationDependsOn(':app')
    }
    
  3. For this

    rootProject.buildDir = '../build' 
    subprojects {
        afterEvaluate{
            android{
                compileSdkVersion 34
            }
        }
    }
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
        project.evaluationDependsOn(':app')
    }
    
Consequent answered 15/8 at 13:28 Comment(0)
J
3

What I believe happening is And it's probably not a flutter issue

The 3rd party dependencies have set compileSdk to less than 34, which is probably not supported by latest flutter 3.24 sdk's gradle (7.6.3)

So it's failing to compile those dependencies. What the workaround does is force all those dependencies compileSdk to 34.

So in the meantime ,it might be worth creating an issue on the dependency's github to use the latest compileSdk 34.

Jaan answered 15/8 at 13:56 Comment(0)
R
2

It seems an issue with the Flutter 3.24.0.

It has been reported here and here.

And this article might be of help, just translate it to English, it seems an okay translation.

Summary of the article:

After the update, Flutter 3.24.0 Android needs to use the compile SDK version 34⁣; otherwise this issue will arise.

Since many libraries have not been updated and adapted in time to Android SDK 34, you can manually change the compileSdkVersion or compileSdk after pulling the sub-library (package) which can solve the problem.

This is the code from the article:

import 'dart:convert';
import 'dart:io';

///
/// 新版本的flutter 3.24.0使用Android 34编译,
/// 所有子库不使用sdk 34编译的话, 就会在打包的时候报错.
/// ```
/// AAPT: error: resource android:attr/lStar not found.
/// ```
/// ```
/// android {
///     // Conditional for compatibility with AGP <4.2.
///     if (project.android.hasProperty("namespace")) {
///         namespace 'com.rmawatson.flutterisolate'
///     }
///
///     compileSdkVersion 34
///
///     defaultConfig {
///         minSdkVersion 16
///     }
/// }
/// ```
///
/// 此脚本用于在打包前, 修改子库的compileSdkVersion编译版本.
///
void main() async {
  final currentPath = Directory.current.path;
  print('脚本工作路径->$currentPath');

  //Android sdk compile sdk version
  final compileSdk = 34;

  //获取所有依赖的子库
  final dependenciesFile = File("$currentPath/.flutter-plugins-dependencies");
  final androidDependencies =
      jsonDecode(dependenciesFile.readAsStringSync())?["plugins"]?["android"];
  if (androidDependencies is List) {
    int index = 0;
    for (final dependency in androidDependencies) {
      final name = dependency["name"];
      final path = dependency["path"];
      if (path != null) {
          print(
              "正在修改[${index + 1}/${androidDependencies.length}]->$path -> compileSdk:$compileSdk");
          amendAndroidCompileSdkVersion(path, compileSdk);
      }
      index++;
    }
  }
}

/// 核心修改方法
/// 修改子库flutter工程中android工程中`build.gradle`文件中的`compileSdkVersion`和`compileSdk`
/// [flutterPath] flutter工程路径
/// [compileSdk] 修改后的编译版本
void amendAndroidCompileSdkVersion(String flutterPath, int compileSdk) {
  final androidPath = "$flutterPath/android";
  final androidPathFile = File("$androidPath/build.gradle");
  if (androidPathFile.existsSync()) {
    final androidPathFileContent = androidPathFile.readAsStringSync();
    if (androidPathFileContent.contains("compileSdkVersion")) {
      //修改compileSdkVersion
      final newContent = androidPathFileContent
          .replaceAllMapped(RegExp(r"compileSdkVersion\s+(\d+)"), (match) {
        return "compileSdkVersion $compileSdk";
      });
      //修改compileSdk
      final newContent2 =
          newContent.replaceAllMapped(RegExp(r"compileSdk\s+(\d+)"), (match) {
        return "compileSdk $compileSdk";
      });
      //写入文件
      androidPathFile.writeAsStringSync(newContent2);
      print("修改成功->$androidPathFile", 250);
    }
  }
}

Credits

Reverberate answered 12/8 at 10:46 Comment(0)
P
2

Try adding the below code to build.gradle file and clean and re-run the project.

subprojects {
    afterEvaluate { project ->
        if (project.plugins.hasPlugin("com.android.application") ||
                project.plugins.hasPlugin("com.android.library")) {
            project.android {
                compileSdkVersion 34
                buildToolsVersion "34.0.0"
            }
        }
    }
}
Popularly answered 21/8 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.