Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change apk filename in gradle

I am trying to use android build tools "com.android.tools.build:gradle:3.0.0-alpha4" in my project. In my build script I rename the output apk which worked fine in the past but does not seem to be supported any more.

applicationVariants.all { variant ->

    def filename = "foo-${variant.baseName}-${variant.versionName}-(${android.defaultConfig.versionCode}).apk"

    variant.outputs.all { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                filename
        )
    }
}

Now the propery I am trying to change became immutable:

Error: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=stageDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.

Is there a new or an alternate way how to do this?

like image 686
Hendrik Avatar asked Nov 08 '22 20:11

Hendrik


1 Answers

  1. Change each -> to all
  2. Change output.outputFile -> to outputFileName

before:

android.applicationVariants.all { variant ->

    variant.outputs.each { output ->
        def finalVersionCode =v10000 + versionCode
        output.versionCodeOverride = finalVersionCode
        output.outputFile = new File(
             output.outputFile.parent,       output.outputFile.name.replace(".apk","-${finalVersion}.apk"))
    }
}

after:

android.applicationVariants.all { variant ->

    variant.outputs.all { output ->
        def finalVersionCode = 10000 + versionCode
        output.versionCodeOverride = finalVersionCode
        outputFileName = new File(
             output.outputFile.parent,
             outputFileName.replace(".apk", "-${finalVersionCode}.apk"))
    }
}
like image 128
jp1017 Avatar answered Nov 28 '22 21:11

jp1017