androidandroid-proguardandroid-studio-3.1.3

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated with android studio 3.1.3


Hello friends,
I am facing one issue in app.gradle file when use proguard ruls with new android studio version is 3.1.3. its working proper on old studio version 2.3.3.
My full error Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

App.Gradle file:
android { ..................... buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk")) } } } } }

Thnaks,


Solution

  • For Gradle 3.0+**

    android {
    ...
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.all {
                    def formattedDate = new Date().format('yyyyMMddHHmmss')
                    def flavor = variant.name
                    def versionName = variant.versionName
                    outputFileName = "AppName_${versionName}_${flavor}_${formattedDate}.apk"
                }
            }
        }
    }
    ...
    }
    

    Result :

    AppName_release_1_20200121175123.apk