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

使用contacts_service:^0.6.3包编译失败,提示未指定Namespace

问题描述

使用contacts_service包(版本:^0.6.3),已添加至pubspec.yaml并执行pub get成功,但编译时出现以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
  A problem occurred configuring project ':contacts_service'.

> 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. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

     If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

源代码如下:

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ContactListPage(),
    );
  }
}

class ContactListPage extends StatefulWidget {
  @override
  _ContactListPageState createState() => _ContactListPageState();
}

class _ContactListPageState extends State<ContactListPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Contact List'),
      ),

    );
  }
}
解决方案

报错原因是contacts_service 0.6.3版本未适配Android Gradle Plugin(AGP)7.0+的namespace要求,AGP 7.0+强制每个模块在build.gradle中指定namespace,而该旧版本插件未配置此项。可通过以下方法解决:

方法一:升级contacts_service到最新版本

  1. 打开项目根目录的pubspec.yaml文件,将contacts_service的版本号修改为pub.dev上的最新稳定版(例如当前最新版为^0.6.4及以上)。
  2. 执行命令flutter pub get更新依赖。
  3. 重新编译项目即可。

方法二:手动为contacts_service模块添加namespace(不升级版本时使用)

  1. 找到项目中android/.flutter-plugins文件,查找contacts_service的本地缓存路径,格式类似:contacts_service=你的用户目录/.pub-cache/hosted/pub.dev/contacts_service-0.6.3
  2. 进入该路径下的android子目录,打开build.gradle文件。
  3. 在android代码块内添加namespace "com.example.contacts_service"(对应插件AndroidManifest.xml中的package值),修改后的代码块示例:
android {
    namespace "com.example.contacts_service"
    compileSdkVersion 30

    defaultConfig {
        minSdkVersion 16
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    // 其他原有代码...
}
  1. 保存文件后重新编译项目。

方法三:降低AGP版本(不推荐)

若上述方法无法使用,可将项目的AGP版本降至7.0以下,但此操作可能导致项目其他依赖兼容性问题,需谨慎操作:

  1. 打开项目android/build.gradle文件,修改buildscript中classpath的AGP版本为com.android.tools.build:gradle:4.2.2。
  2. 打开android/gradle/wrapper/gradle-wrapper.properties,修改distributionUrl为https\://services.gradle.org/distributions/gradle-6.7.1-all.zip。
  3. 执行flutter clean后重新编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:20:21