1

I have set a custom outputFileName in the module level gradle file. When I try to run the application by pressing the play button (or using menu commands for debu/release build), the apk is getting built but the application is not getting installed on the connected device.

Getting the following error.

Finished with error: Gradle build failed to produce an .apk file. It's likely that this file was generated under C:\Users\BnayA\Work\Office\sangrahapp\build, but the tool couldn't find it.

I googled the solution for it, but nothing helped me to fix it.

Below is my app/module level Gradle config.

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 GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

ant.condition(property: 'os', value: 'windows') {
    os(family: 'windows')
}
ant.condition(property: 'os', value: 'unix') {
    os(family: 'unix')
}

// Based on http://stackoverflow.com/questions/17097263#24121734
def getMasterCommitCount = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            switch (ant.properties.os) {
                case 'windows':
                    commandLine 'cmd', '/c', 'git', 'rev-list', '--first-parent', '--count', 'master'
                    break
                case 'unix':
                    commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
                    break
            }
            standardOutput = stdout
        }
        return Integer.parseInt(stdout.toString().trim())
    } catch (ignored) {
        return -1
    }
}

def getVersionName = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            switch (ant.properties.os) {
                case 'windows':
                    commandLine 'cmd', '/c', 'git', 'describe', '--tags', '--dirty', '--always'
                    break
                case 'unix':
                    commandLine 'git', 'describe', '--tags', '--dirty', '--always'
                    break
            }
            standardOutput = stdout
        }
        return stdout.toString().trim()
    } catch (ignored) {
        return null
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    compileSdkVersion 29

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

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "abc.def.ghi"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode getMasterCommitCount()
        versionName getVersionName()
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        archivesBaseName = 'abc'
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '_debug'
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            outputFileName = new File(outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))
        }
    }

    splits {
        abi {
            universalApk true
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
}
bnayagrawal
  • 1,044
  • 3
  • 12
  • 22
  • can you share the output of `flutter doctor`? – Sanjay Sharma May 31 '20 at 08:55
  • @SanjaySharma "Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, v1.12.13+hotfix.9, on Microsoft Windows [Version 10.0.18363.657], locale en-IN) [√] Android toolchain - develop for Android devices (Android SDK version 29.0.3) [√] Android Studio (version 3.6) [√] VS Code (version 1.45.1) [√] Connected device (1 available) • No issues found!" – bnayagrawal May 31 '20 at 09:21

1 Answers1

0

Try to replace

outputFileName = new File(outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))

with the following code

 outputFileName = new File(
             output.outputFile.parent,
             outputFileName.replace(".apk", "-${defaultConfig.versionName}.apk"))

Update: replace this

splits {
        abi {
            universalApk true
        }
    }

with the following

splits {
        abi {
            enable true
            reset()
            universalApk false
        }
    }

Please add signing config in debug also.

signingConfig signingConfigs.debug
Sanjay Sharma
  • 3,687
  • 2
  • 22
  • 38