isar_flutter_libs 引发 Namespace not specified

问题

在运行 Flutter Android 项目遇到启动问题提示 Namespace not specified 相关异常,一般的这种异常通常是插件或者 gradle 配置与 APG 不匹配导致

A problem occurred configuring project ':isar_flutter_libs

这次的问题是 isar_flutter_libs 插件没有支持最新的 AGP 适用,这个库最后停止在两年前,看起来想更新他支持最新 APG 短期不太现实

解决

解决思路就是要么对 AGP 降级,要么在 Android 配置中增加动态属性设置

app\android\build.gradle.kts

groovy 复制代码
// === HOTFIX_AGP8_NAMESPACE_isar_flutter_libs ===
// 动态为三方库模块注入 namespace(AGP 8+)。
subprojects {
    if (name == "isar_flutter_libs") {
        plugins.withId("com.android.library") {
            val ext = extensions.findByName("android")
            val setter = ext?.javaClass?.methods?.firstOrNull { it.name == "setNamespace" && it.parameterTypes.size == 1 }
            val getter = ext?.javaClass?.methods?.firstOrNull { it.name == "getNamespace" && it.parameterTypes.isEmpty() }
            val hasNs = try { getter?.invoke(ext) != null } catch (_: Throwable) { false }
            if (!hasNs && setter != null) {
                setter.invoke(ext, "dev.isar.flutter.libs")
                println("[hotfix] set namespace for :isar_flutter_libs -> dev.isar.flutter.libs")
            }
        }
    }
}

重新清除依赖运行 看到日志里有如下更新表明注入已生效

csharp 复制代码
[hotfix] set namespace for :isar_flutter_libs -> dev.isar.flutter.libs

然后又遇到其他问题

flutter_local_notifications(以及其依赖链)在低于 Android 8.0(API 26)设备上使用了 Java 8+ 核心库 API;AGP 会在 :app:checkDebugAarMetadata 阶段检查到未开启 desugaring 直接失败。

app\android\app\build.gradle.kts 增加如下声明

android 配置增加

groovy 复制代码
android {
    namespace = "com.example.ble_chat_flutter"
    compileSdk = flutter.compileSdkVersion
  + ndkVersion = "27.0.12077973"

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
      + isCoreLibraryDesugaringEnabled = true
    }

defaultConfig 配置增加

groovy 复制代码
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.monorepo"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
      + minSdk = 23

末尾加

groovy 复制代码
+ dependencies {
+     coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.0.4")
+ }

整体完成插件适配, groovy 通过预编译

相关推荐
booboosui5 小时前
清单 40 件是硬上限:过期的活挂了 48 小时才被打标记
android
FlightYe5 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
方白羽6 小时前
SP vs DataStore:解锁 Android 本地存储的并发秘密
android·app·客户端
GY-936 小时前
【Android】View和Componse
android
王二蛋与他的张大花8 小时前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
Paper_Love8 小时前
android安全公告网站
android
StarsGreen12 小时前
Android ADB(Android Debug Bridge)命令详解
android
阿健君12 小时前
现代 Android 屏幕适配完全指南
android·kotlin
软件交付观察室14 小时前
用户关掉手机定位权限后,原生App还能显示哪些位置?
android·ios·移动开发
黄林晴14 小时前
Jetpack Compose 1.13.0 来了:大量 API 弃用,表单代码要重构
android·android jetpack