Files
FocusBuddy/ANDROID_BUILD_FIX.md
2025-11-22 18:30:20 +08:00

141 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🔧 Android 构建修复 - Core Library Desugaring
**日期**: 2025-11-22
**问题**: flutter_local_notifications 需要 core library desugaring
---
## ❌ 错误信息
```
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
> An issue was found when checking AAR metadata:
1. Dependency ':flutter_local_notifications' requires core library desugaring to be enabled
for :app.
```
---
## 🔍 问题原因
`flutter_local_notifications` 包使用了 Java 8+ 的新 API例如 `java.time` 包),这些 API 在较旧的 Android 版本上不可用。
Core library desugaring 允许应用使用这些新 API同时保持对旧版 Android 的兼容性。
---
## ✅ 解决方案
修改 `android/app/build.gradle.kts` 文件:
### 1. 启用 desugaring
`compileOptions` 中添加:
```kotlin
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = true // ✨ 新增
}
```
### 2. 添加 desugaring 库依赖
在文件末尾添加:
```kotlin
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4")
}
```
---
## 📝 完整修改
### 修改前
```kotlin
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
flutter {
source = "../.."
}
```
### 修改后
```kotlin
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = true // ✨ 新增
}
flutter {
source = "../.."
}
dependencies { // ✨ 新增
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4")
}
```
---
## 🧪 验证修复
运行以下命令测试:
```bash
flutter clean
flutter build apk --debug
```
或直接运行:
```bash
flutter run -d <android-device>
```
---
## 📚 相关信息
### 什么是 Core Library Desugaring?
Desugaring 是一个过程,它将使用较新 Java API 的代码转换为在旧版 Android 上可运行的等效代码。
### 支持的 API
启用 desugaring 后,可以使用:
- `java.time.*` (日期和时间 API)
- `java.util.stream.*` (Stream API)
- `java.util.function.*` (函数式接口)
- 以及其他 Java 8+ 的核心库 API
### 版本说明
- `desugar_jdk_libs:2.0.4` 是当前稳定版本
- 最低支持 Android API 21 (Android 5.0)
---
## 🔗 参考链接
- [Android Developer - Java 8+ Support](https://developer.android.com/studio/write/java8-support.html)
- [Core Library Desugaring Guide](https://developer.android.com/studio/write/java8-support#library-desugaring)
---
**状态**: ✅ 已修复
**文件**: `android/app/build.gradle.kts`
**影响**: Android 构建现在应该可以正常工作