Home > Blockchain >  "Exception: Gradle build failed" only for flutter project in AndroidStudio
"Exception: Gradle build failed" only for flutter project in AndroidStudio

Time:01-31

My Flutter project works fine, I can build it as Android app with VisualStudio Code. With AndroidStudio I am able to build the Android app if I open only the android folder and build the Android app as android project. But if I open the whole Flutter project in AndroidStudio and try to build it for Android (iOS works fine) I get this error: Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under .../build, but the tool couldn't find it. Seems like an IDE bug, is there anyone with a solution for me?

Android Studio version : Android Studio Electric Eel | 2022.1.1 (Build #AI-221.6008.13.2211.9477386, built on January 11, 2023)

Update: 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"
}

CodePudding user response:

You have no default flavor defined, so you need to specify flavor to build. From command line:

flutter run --flavor appCenter (or googlePlay)

In Android Studio, go to Run->Edit configurations and set flavor to build (in your case appCenter or googlePlay. You can also add new configuration to be able to swith between them to build different flavor.

  • Related