在 Flutter 中安装任何第三方包并尝试运行我的代码时,总是出现错误,在构建 gradle 文件中找不到命名空间
举个例子,这个
A problem occurred configuring project ':google_mlkit_commons'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
> Namespace not specified. Please specify a namespace in the module's build.gradle file like so:
android {
namespace 'com.example.namespace'
}
If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.
所以要解决这个问题,我应该转到提到的包,例如这里是
':google_mlkit_commons'
,然后扔掉它的android文件并获取构建gradle,然后在我们的示例的android标签中添加它的命名空间
android {
namespace "com.google_mlkit_text_recognition"
compileSdkVersion 31
....
}
我的问题是 1-我应该为我想要使用的每个包执行此步骤吗? 2-为什么现在会出现此错误?和新的flutter版本有关系吗?
扑医生-v
[√] Flutter (Channel stable, 3.16.5, on Microsoft Windows [Version 10.0.19045.3930], locale en-US)
• Flutter version 3.16.5 on channel stable at C:\Users\engma\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 78666c8dc5 (4 weeks ago), 2023-12-19 16:14:14 -0800
• Engine revision 3f3e560236
• Dart version 3.2.3
• DevTools version 2.28.4
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:\Users\engma\AppData\Local\Android\sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[X] Visual Studio - develop Windows apps
X Visual Studio not installed; this is necessary to develop Windows apps.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2023.1)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)
[√] VS Code (version 1.85.1)
• VS Code at C:\Users\engma\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.80.0
[√] Connected device (4 available)
• DRA LX2 (mobile) • 5LX9K18817915788 • android-arm64 • Android 8.1.0 (API 27)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.3930]
• Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.217
• Edge (web) • edge • web-javascript • Microsoft Edge 120.0.2210.133
[√] Network resources
• All expected network resources are available.
! Doctor found issues in 1 category.
在较新版本的 Android Gradle 插件和 Gradle 中,
namespace
从 AndroidManifest.xml
转移到 app/build.gradle
。一般来说,这意味着以下内容:
在所有三个 AndroidManifest.xml 文件(main、debug、profile)中,删除
package
属性:
<!-- This is how it should look like: -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- YOUR CONTENT HERE -->
</manifest>
在
app/build.gradle
中,添加以下行:
android {
namespace "com.example.yourpackage"
...
}
但是,这需要项目中的所有模块添加
namespace
。您的 package 似乎还没有此属性。