You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

flutter_phone_direct_caller无法使用,构建报错Namespace未指定

解决flutter_phone_direct_caller包构建失败(Namespace未指定)的问题

问题场景

在Flutter项目中引入flutter_phone_direct_caller包后,执行Android构建时出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':flutter_phone_direct_caller'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Specify a namespace in the module's build file.

BUILD FAILED in 24s
Error: Gradle task assembleDebug failed with exit code 1

移除该包后项目可正常编译运行。

解决方案

1. 升级包到最新版本

旧版本的flutter_phone_direct_caller未适配新版Android Gradle Plugin(AGP)的namespace要求,优先尝试升级包版本:

  • 在pubspec.yaml中更新包的版本号为最新:
    dependencies:
      flutter_phone_direct_caller: ^最新版本号
    
  • 执行命令更新依赖:
    flutter pub get
    

2. 手动为依赖模块指定Namespace

如果升级后问题仍存在,可在项目的android/build.gradle中添加配置,为该模块强制指定namespace:

subprojects {
    if (project.name == 'flutter_phone_direct_caller') {
        android {
            namespace "com.jrai.flutter_phone_direct_caller" // 对应原包AndroidManifest中的package值
        }
    }
}

3. 替换为兼容的替代包

若上述方法均无效,可使用url_launcher包实现拨打电话功能,该包适配新版AGP:

  • 添加依赖到pubspec.yaml:
    dependencies:
      url_launcher: ^6.2.0
    
  • 实现拨打电话的示例代码:
    import 'package:url_launcher/url_launcher.dart';
    
    Future<void> callNumber(String phone) async {
      final uri = Uri(scheme: 'tel', path: phone);
      if (await canLaunchUrl(uri)) {
        await launchUrl(uri);
      } else {
        throw '无法拨打号码 $phone';
      }
    }
    

原因说明

新版AGP要求所有Android模块必须在build.gradle中明确指定namespace属性,而旧版flutter_phone_direct_caller包未配置该属性,导致构建流程中断。

内容的提问来源于stack exchange,提问作者Ahmad Najim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:23:11