Flutter-->AAPT: error: resource android:attr/lStar not found.

更新Flutter 3.24.0之后, 打包出现AAPT: error: resource android:attr/lStar not found.问题, 这里出一个我的解决方案.

更新Flutter 3.24.0之后, Android编译sdk需要使用34, 否则就会出现很多问题...

由于很多库都不可能及时更新适配到Android sdk 34, 所以可以等pub get将子库拉取到本地之后, 在本地手动将compileSdkVersioncompileSdk修改为34,即可解决本文问题.

作为程序猿,那肯定不可能手动修改, 这种体力活是干不会的.

祭出本文干货dart脚本:

dart 复制代码
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);
    }
  }
}

Flutter工程的任意位置, 新建一个dart文件, 粘贴上述代码, 使用dart运行main方法即可.


群内有各(pian)种(ni)各(jin)样(qun)的大佬,等你来撩.

联系作者

点此QQ对话 该死的空格 点此快速加群

相关推荐
然后就去远行吧37 分钟前
小程序 wxml 语法 —— 37 setData() - 修改对象类型数据
android·前端·小程序
熙曦Sakura1 小时前
【MySQL】数据类型
android·mysql·adb
故事与他6451 小时前
CTFHub-上传文件
android·ide·windows·web安全·网络安全·android studio·xss
大胃粥1 小时前
Android app 冷启动(7) 执行动画
android
yi诺千金2 小时前
Android U 分屏——SystemUI侧处理
android
顾林海2 小时前
Flutter Dart 流程控制语句详解
android·前端·flutter
Cui晨2 小时前
Android 滑块开关 自定义Switch
android
&有梦想的咸鱼&2 小时前
Android Retrofit 框架注解定义与解析模块深度剖析(一)
android·retrofit
烬奇小云2 小时前
安卓7.0到11.0的更新变化(简单理解)
android·安卓逆向
whatever who cares2 小时前
android:实现圆角效果
android