Flutter - Gradle构建失败

jhkqcmku  于 12个月前  发布在  Flutter
关注(0)|答案(2)|浏览(126)

我正在开发一个Flutter应用程序。这是由日志导致的错误

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\franc\Documents\Github\x\android\build.gradle' line: 26

* What went wrong:
A problem occurred evaluating root project 'android'.
> A problem occurred configuring project ':app'.
   > Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
      > 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
Exception: Gradle task assembleDebug failed with exit code 1

下面是android build.gradle文件

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.0-alpha09'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我真的不知道该怎么办了。
我已经尝试了Android Studio上“工具”窗口上的“AGP升级助手"按钮,但它似乎没有任何作用。
我尝试构建应用程序,将Gradle上的“:app”部分更改为项目名称,但不起作用。

q35jwt9p

q35jwt9p1#

对我来说,用flutter clean清理项目(正如@canilho所写的)和清理Gradle($HOME/.gradle)和Flutter($HOME/.pub-cache)缓存没有帮助。我已经创建了新项目,从旧项目迁移了代码,升级了Gradle Package 器并添加了Flutter包。所有这些我都没有在VS Code中打开新项目,以确保没有我的同意就没有任何更改。
这是为Linux/MacOS,但我认为你会得到它
1.创建新项目flutter create new_project
1.进入新目录cd new_project
1.删除lib目录rm -rf lib
1.从旧项目mv ../old_project/lib lib复制lib目录
1.在VSC以外的其他编辑器中编辑android/gradle/wrapper/gradle-wrapper.properties并将distributionUrl更改为distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
1.添加软件包flutter pub add <list of packages>
1.构建apk镜像flutter build apk-相反,你可能会从VSC构建它
希望这有帮助

wwtsj6pe

wwtsj6pe2#

在使用Visual Studio Code构建应用程序时,这种情况在我身上发生过几次。
出于某种原因,有时构建会创建文件的不可恢复状态。

可能方案一:

有时只需运行flutter clean就可以解决问题。

可能方案二:

如果您使用Visual Studio Code,则关闭并重新打开应用程序可能会解决某些构建问题。

可能方案三:

有几次,我被迫删除项目中的**.dart_tool**文件夹。
只需确保在删除文件夹后,在运行构建之前,在终端flutter pub get上运行以获取所有依赖项。

相关问题