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 通过预编译

相关推荐
千里马学框架21 小时前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台21 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone1 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc1 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo1 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077001 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼1 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone1 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen1 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone1 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui