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

flutter_bluetooth_serial: ^0.4.0是否可用?构建遇命名空间报错

问题描述

我想用HC-05蓝牙模块控制智能车,但使用flutter_bluetooth_serial: ^0.4.0构建Flutter应用时构建失败,报错信息如下:

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...

FAILURE: Build failed with an exception.

  • What went wrong:
    A problem occurred configuring project ':flutter_bluetooth_serial'.

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.

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.
  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.

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

Exited (1).

核心代码:

import 'package:controle_car/models/control/control.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: FlutterBluetoothSerial.instance.requestEnable(),
      builder: (context, future) {
        if (future.connectionState == ConnectionState.waiting) {
          return Scaffold(
            body: Container(
              width: double.infinity,
              height: double.infinity,
              color: Colors.grey[700],
              child: const Center(
                child: Icon(
                  Icons.bluetooth_disabled,
                  size: 200,
                  color: Colors.blue,
                ),
              ),
            ),
          );
        } else if (future.connectionState == ConnectionState.done) {
          return Control();
        } else {
          return Control();
        }
      },
    );
  }
}

相关配置文件:

pubspec.yaml

name: controle_car
description: "A new Flutter project."
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ^3.5.4

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.8
  flutter_bluetooth_serial: ^0.4.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^4.0.0

flutter:
  uses-material-design: true

android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.controle_car"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = "25.1.8937393"

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = 17
    }

    defaultConfig {
        applicationId = "com.example.controle_car"
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-all.zip

android/settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.3.2" apply false
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false
}

include ":app"

疑问:flutter_bluetooth_serial: ^0.4.0是否仍可正常使用?该如何解决上述构建错误?


解决方案

关于flutter_bluetooth_serial: ^0.4.0的可用性

这个版本的包最后一次更新是在2021年,已经停止维护,无法兼容你当前使用的Android Gradle Plugin(AGP 8.3.2)等新工具链,直接使用必然出现构建问题。如果坚持要用该包,需要做兼容修改;更推荐替换为维护活跃的蓝牙插件。

修复构建错误的方法

方法1:修改插件的build.gradle文件(临时方案)

  1. 找到插件本地目录:路径一般为android/.pub-cache/hosted/pub.dev/flutter_bluetooth_serial-0.4.0/android
  2. 打开该目录下的build.gradle文件
  3. 在android代码块中添加命名空间配置:
android {
    namespace "com.github.pauldemarco.flutterbluetoothserial"
    // 保留原有其他配置
}
  1. 保存后执行flutter run重新构建

方法2:降低AGP和Gradle版本(适配旧插件)

  1. 修改android/settings.gradle中的AGP版本为7.4.2,同时降低Kotlin版本:
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.4.2" apply false
    id "org.jetbrains.kotlin.android" version "1.7.20" apply false
}
  1. 修改android/gradle/wrapper/gradle-wrapper.properties中的Gradle版本为7.5:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
  1. 清理缓存并重新拉取依赖:执行flutter clean && flutter pub get,再重新构建

方法3:替换为维护活跃的蓝牙插件

推荐使用flutter_blue_plus,它兼容最新的Flutter和AGP版本:

  1. 在pubspec.yaml中替换依赖:
dependencies:
  # 移除flutter_bluetooth_serial: ^0.4.0
  flutter_blue_plus: ^1.13.3
  1. 执行flutter pub get更新依赖
  2. 根据插件官方文档修改代码,适配新的蓝牙API(包括开启蓝牙、设备连接、数据传输等逻辑)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 18:24:54