更新
Flutter 3.24.0
之后, 打包出现AAPT: error: resource android:attr/lStar not found.
问题, 这里出一个我的解决方案.
更新Flutter 3.24.0
之后, Android
编译sdk需要使用34
, 否则就会出现很多问题...
由于很多库都不可能及时更新适配到Android sdk 34
, 所以可以等pub get
将子库拉取到本地之后, 在本地手动将compileSdkVersion
或compileSdk
修改为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)
的大佬,等你来撩.