我尝试为我的项目构建一个发布 apk,但我遇到了一个异常: 失败:构建失败并出现异常。
* What went wrong:
A problem occurred configuring project ':flutter_barcode_scanner'.
> 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.
* 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 1s
Running Gradle task 'assembleRelease'... 1,849ms
Gradle task assembleRelease failed with exit code 1
我在 GitHub 中搜索解决方案,得到了这个解决方案:
For now it seems that package doesn't work with AGP and Gradle 8, I downgraded agp to 7.4.2 and gradle to 7.5
所以我在 build.gradle 和 gradle-wrapper.properties 中制定解决方案:
构建.gradle:
buildscript {
ext.kotlin_version = '1.9.25'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.4.1'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
gradle-wrapper.properties:
#Tue Apr 20 01:37:31 IST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
但是在这个解决方案之后,我遇到了同样的异常,我询问了ChatGPT,他告诉我在包文件中的build.gradle中进行更改: 编辑前的build.gradle:
group 'com.amolg.flutterbarcodescanner'
version '1.0-SNAPSHOT'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.google.android.gms:play-services-vision:20.1.3'
}
编辑后的build.gradle:
group 'com.amolg.flutterbarcodescanner'
version '1.0-SNAPSHOT'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
}
}
rootProject.allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 21
targetSdkVersion 34
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.google.android.gms:play-services-vision:20.1.3'
}
只需转到app/build.gradle
包中的
flutter_barcode_scanner
,然后添加以下行即可解决问题:
android { namespace "add flutter_barcode_scannerpackage namespace" ---------}
要获取正确的命名空间,请检查 AndroidManifest.xml 文件中
flutter_barcode_scanner
的包名称。
另外,请检查这个答案👇🏻其他解决方案
您可以通过在
android/build.gradle
文件中添加以下脚本来解决更新到 AGP 8.+ 后的命名空间问题:
subprojects {
afterEvaluate { project ->
if (project.hasProperty('android')) {
project.android {
if (namespace == null) {
namespace project.group
}
}
}
}
}