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

相关推荐
投笔丶从戎1 小时前
Kotlin Multiplatform--01:项目结构基础
android·开发语言·kotlin
Lary_Rock2 小时前
Android 编译问题 prebuilts/clang/host/linux-x86
android·linux·运维
玫瑰花开一片一片3 小时前
Flutter IOS 真机 Widget 错误。Widget 安装后系统中没有
flutter·ios·widget·ios widget
王江奎3 小时前
Android FFmpeg 交叉编译全指南:NDK编译 + CMake 集成
android·ffmpeg
limingade3 小时前
手机打电话通话时如何向对方播放录制的IVR引导词声音
android·智能手机·蓝牙电话·手机提取通话声音
hepherd4 小时前
Flutter 环境搭建 (Android)
android·flutter·visual studio code
_一条咸鱼_5 小时前
揭秘 Android ListView:从源码深度剖析其使用原理
android·面试·android jetpack
_一条咸鱼_5 小时前
深入剖析 Android NestedScrollView 使用原理
android·面试·android jetpack
_一条咸鱼_5 小时前
揭秘 Android ScrollView:深入剖析其使用原理与源码奥秘
android·面试·android jetpack