flutterdartbuildbuild.gradle

solution for changing release build name in a dynamic way


Code i used for changing the build name dynamically but its not working properly the desired name is showing on the log as a printing statement,build name remain as same apk-release.apk instead of new name as mentioned above.Is there any chance of getting error due to the gradle or flutter version.

buildTypes {
    release {
        signingConfig signingConfigs.debug
        applicationVariants.all { variant ->
            if (variant.buildType.name == "release") {
                variant.outputs.all { output ->
                    def date = new Date().format("yyyyMMdd_HHmmss")
                    def newAppName = "MyApp_v${versionName}build${versionCode}${date}"
                    println("Changing APK name to : ${newAppName}")
                    output.outputFileName = "${newAppName}.apk"
                }
            }
        }
        signingConfig signingConfigs.release
        buildConfigField 'boolean', 'UPDATE_ENABLED', 'true'
        minifyEnabled true
        shrinkResources true
        //useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Solution

  • You have written this inside the buildTypes which I don't know will work or not but what worked for me was I wrote this

    android {
    // Change apk name
         applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def appName = "MYAPP" 
                def formattedDate = new Date().format('yyyyMMdd_HHmmss')
                def newApkName = "${appName}_${formattedDate}.apk"
                output.outputFileName = "../../flutter-apk/${newApkName}"
            }
        }
    // Other tags
    }
    

    by default the flutter build apk --release will create the default app-release.apk on the same location along with this it will create another apk with the custom name.

    Previously I was using

     output.outputFileName = newApkName
    

    and it was building the apk inside Flutter Projects\my_project\build\app\outputs\apk\release along with the default app-release.apk then I Used

    output.outputFileName = "../../flutter-apk/${newApkName}"
    

    now it is creating the apk in the same location as build\app\outputs\flutter-apk\app-release.apk