繁体   English   中英

“异常:Gradle 构建失败”仅适用于 AndroidStudio 中的 flutter 项目

[英]"Exception: Gradle build failed" only for flutter project in AndroidStudio

我的 Flutter 项目运行良好,我可以使用 VisualStudio 代码将其构建为 Android 应用程序。 使用 AndroidStudio,如果我只打开 android 文件夹并将 Android 应用程序构建为 android 项目,我就可以构建 Android 应用程序。 但是,如果我在 AndroidStudio 中打开整个 Flutter 项目并尝试为 Android 构建它(iOS 工作正常),我会收到此错误:异常:Gradle 构建无法生成 .apk 文件。 很可能这个文件是在.../build下生成的,但是工具找不到它。 似乎是一个 IDE 错误,有人可以为我提供解决方案吗?

Android工作室版本:Android电鳗工作室| 2022.1.1(构建#AI-221.6008.13.2211.9477386,构建于 2023 年 1 月 11 日)

更新:app/build.gradle

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
def flutterVersionName = localProperties.getProperty('flutter.versionName')


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 33

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    flavorDimensions "distribute"
    productFlavors {
        appCenter {
            dimension "distribute"
        }

        googlePlay {
            dimension "distribute"
        }
    }

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "myId"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 3
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

您没有定义默认风味,因此您需要指定要构建的风味。 从命令行:

flutter run --flavor appCenter (或 googlePlay)

在 Android Studio 中,go 运行->编辑配置并设置要构建的风格(在您的情况下appCentergooglePlay 。您还可以添加新配置,以便能够在它们之间切换以构建不同的风格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM