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对话 该死的空格 点此快速加群

相关推荐
编程乐学6 分钟前
基于Android Studio 蜜雪冰城(奶茶饮品点餐)—原创
android·gitee·android studio·大作业·安卓课设·奶茶点餐
problc1 小时前
Android中的引用类型:Weak Reference, Soft Reference, Phantom Reference 和 WeakHashMap
android
IH_LZH1 小时前
Broadcast:Android中实现组件及进程间通信
android·java·android studio·broadcast
去看全世界的云1 小时前
【Android】Handler用法及原理解析
android·java
机器之心2 小时前
o1 带火的 CoT 到底行不行?新论文引发了论战
android·人工智能
机器之心2 小时前
从架构、工艺到能效表现,全面了解 LLM 硬件加速,这篇综述就够了
android·人工智能
AntDreamer2 小时前
在实际开发中,如何根据项目需求调整 RecyclerView 的缓存策略?
android·java·缓存·面试·性能优化·kotlin
运维Z叔4 小时前
云安全 | AWS S3存储桶安全设计缺陷分析
android·网络·网络协议·tcp/ip·安全·云计算·aws
Reese_Cool5 小时前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
平凡シンプル5 小时前
安卓 uniapp跨端开发
android·uni-app