Gradle: Naming the APK outputFile

18,742 views
Skip to first unread message

Stefan Hoth

unread,
Apr 11, 2013, 7:59:36 AM4/11/13
to adt...@googlegroups.com
Hi again,

I was trying to get the version number of the "installMyFlavorRelease"-build. I was trying to modify the outputFile property in build variants but this is read only.

My prefered output format would be <projectname>-<flavor>-<version>-<build type>.apk

How can I do this?

- Stefan

Xavier Ducrohet

unread,
Apr 12, 2013, 1:31:46 AM4/12/13
to adt...@googlegroups.com
What is read-only?

I've done some major refactoring in there recently, so I'm not sure what the state was before.

But you should be able to do

variant.packageApplication.outputFile = file('...')





- Stefan

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Stefan Hoth

unread,
Apr 16, 2013, 7:01:56 AM4/16/13
to adt...@googlegroups.com
Xavier,

thanks for the hint, I now override the names manually by doing this:

android {
    [...]
    buildTypes {
        debug {
            packageNameSuffix = ".debug"
            versionNameSuffix = "-DEBUG"
            signingConfig signingConfigs.debug
        }

        release {
            signingConfig signingConfigs.release
        }
        buildVariants.each { variant ->

            apk = variant.packageApplication.outputFile;

            if (variant.buildType.name == "release") {
                newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-RELEASE.apk");
            } else {
                newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-SNAPSHOT.apk");
            }

            newName = newName.replace("-" + variant.buildType.name, "");

            variant.packageApplication.outputFile = new File(apk.parentFile, newName);
            if (variant.zipAlign) {
                variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
            }
        }
    }

}

It's not pretty but it gets the job done.

Cheers,
Stefan

Mike Penz

unread,
Jul 25, 2013, 8:11:11 AM7/25/13
to adt...@googlegroups.com
Hey.

I tried this with the newest gradle plugin: classpath 'com.android.tools.build:gradle:0.5.4'
but it doesn't work.

Is there a different solution to define the outputFile with the versionName?

Thanks in advance.

Xavier Ducrohet

unread,
Jul 25, 2013, 2:01:35 PM7/25/13
to adt...@googlegroups.com
Can you post your code?

Alex Vasilenko

unread,
Jul 31, 2013, 3:45:04 PM7/31/13
to adt...@googlegroups.com
Hi all,

Here's my build.gradle as example of apk custom naming using latest available android gradle plugin. Also it has auto increment of versionCode. That's pretty ugly, but it works.
Hope it helps.

Xavier, are there any plans for built-in output file naming rules like in maven-android-plugin. Also it would be great to simplify versionCode autoincrement.

Regards,
Alex

Michael Janin

unread,
Aug 2, 2013, 10:24:22 AM8/2/13
to adt...@googlegroups.com
It works fine with :

android.applicationVariants.all { variant ->
def outputName = // filename
 variant.outputFile = file(path_to_filename)
}

I believe that : variant.packageApplication.outputFile, and variant.zipAlign.outputFile are also available.


@Alex - thanks for this nice example

Xavier Ducrohet

unread,
Aug 2, 2013, 2:00:09 PM8/2/13
to adt...@googlegroups.com
Yes, variant.packageApplication.outputFile, and variant.zipAlign.outputFile are also available but then you have to do some logic about whether the packaged is signed/aligned. 

variant.outputFile takes care of this for you.

Jeff Campbell

unread,
Aug 9, 2013, 6:06:57 PM8/9/13
to adt...@googlegroups.com
I would be nice to have an easier way to put the version number on the apk filename

ke...@silverchalice.com

unread,
Nov 26, 2013, 4:35:58 PM11/26/13
to adt...@googlegroups.com
It would be really nice to add some properties to the "android" extension object so that changing the final APK filename is easy. Something like:

android {
    outputFileName "MyEasilyRenamedApk-${productFlavor}-${buildType}-v${versionName}.apk"

    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 18
    }

    productFlavors {
        ...
    }
}

I'd even be happy to write it, if the Android Tools team agreed that it's useful.

Jean-Michel Cazaux

unread,
Jun 3, 2014, 1:52:39 PM6/3/14
to adt...@googlegroups.com
Hi there,
I am trying to achieve teh same thing (renaming the apk file), but it seems the sample code does not work anymore with a recent Android plugin...

Both variant.packageApplication.outputFile and variant.outputFile are erroring out.

Any pointers to get this working on the last android plugin version ?
Has it been replaced by something else ?

Many thanks in advance,
JM

Jean-Michel Cazaux

unread,
Jun 10, 2014, 4:35:38 AM6/10/14
to adt...@googlegroups.com
Some help on this would really be much apopreciated ;)

JM

Xavier Ducrohet

unread,
Jun 10, 2014, 8:53:32 PM6/10/14
to adt...@googlegroups.com
Can you provide your actual code and the actual error?

variant.outputFile = ...
or 
variant.setOutputFile(..)

should work.

Using variant.packageApplication should work as well.

We have a test app (in the samples, called "renamedApk") that does simply this:

android.applicationVariants.all { variant ->
    variant.outputFile = file("$project.buildDir/${variant.name}.apk")
}



--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan Hoth

unread,
Jun 11, 2014, 3:00:27 AM6/11/14
to adt...@googlegroups.com
Hi Jean-Michel,

sorry for the late reply. My current configuration looks like this and still works https://gist.github.com/stefanhoth/faedeb4c58615992937d

Hope that helps. If not, please do as Xavier says and post the exact error messages.

Cheers,
Stefan

Jean-Michel Cazaux

unread,
Jun 11, 2014, 5:11:06 AM6/11/14
to adt...@googlegroups.com
Hi Stephan,
I have pasted your code in the android section of my build file, but it complains it does not know applicationVariants...

Here iss my build file :

apply plugin: 'android'

android {

    /*
        buildVariants.each { variant ->

        apk = variant.packageApplication.outputFile;

        if (variant.buildType.name == "release") {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-release.apk");
        } else {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-beta.apk");
        }

        newName = newName.replace("-" + variant.buildType.name, "");

        variant.packageApplication.outputFile = new File(apk.parentFile, newName);
        if (variant.zipAlign) {
            variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }

     */
    signingConfigs {
        release {
            storeFile file("../r.keystore")
            storePassword "*****"
            keyAlias "*****"
            keyPassword "******"
        }

    }

    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    defaultConfig {
        applicationId 'biz.****.android'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 0
        versionName '1.0.0.DEV'
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }


    productFlavors {
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

     applicationVariants.all { variant ->

        def apk = variant.outputFile;
        def newName;

        // newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + ".apk");
        if (variant.buildType.name == "release") {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-release.apk");
        } else {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-beta.apk");
        }

//        newName = newName
//                .replace("-" + variant.buildType.name, "")
//                .replace(project.name, "MyCoolCompany-MyGreatProduct");

        variant.outputFile = new File(apk.parentFile, newName);

        if (variant.zipAlign) {
            variant.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }

        logger.info('INFO: Set outputFile to ' + variant.outputFile + " for [" + variant.name + "]");
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    /* Utilities & compatibility */
    compile 'org.apache.commons:commons-lang3:3.3.1'
    compile "com.android.support:support-v4:19.1.+"

    /* Car systems Common Library */
    compile project(':CommonsDroid')

    /* JSon tests */
    compile 'com.fasterxml.jackson.core:jackson-databind:2.4.0-rc3'
    compile 'com.google.code.gson:gson:1.7.2'

    /* ORM */
    compile 'com.j256.ormlite:ormlite-android:4.45'




}

Thank you all for your help,

JM

Stefan Hoth

unread,
Jun 11, 2014, 5:56:24 AM6/11/14
to adt...@googlegroups.com
Do you mean Android studio complains or the actual build fails?

I have a warning for that field too but it works perfectly anyway.

Cheers,
Stefan

Jean-Michel Cazaux

unread,
Jun 11, 2014, 7:46:19 AM6/11/14
to adt...@googlegroups.com
Sorry, apparently I had something in my eyes ;), it works indeed.

Thanks for your help,
JM

Draško Kokić

unread,
Jun 22, 2015, 1:51:06 PM6/22/15
to adt...@googlegroups.com
I am getting the following error:
    Error:(18, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@2bbea216.

Even thought code inspection is showing the `variant` is having a field `outputFile` ... what gives?!

(I am using `com.android.tools.build:gradle:1.1.1`)

Tomáš Procházka

unread,
Jun 29, 2015, 10:57:09 AM6/29/15
to adt...@googlegroups.com
I'm using this quit ugly way

variant.outputs.each { output ->
File file = output.outputFile
String gitHash = scmService.getLastCommitHashShort()

def parentProjectName = (project.parent.name != project.name) ? project.parent.name + '-' : ''

def name = output.outputFile.name.replace("-", ",").replace(project.name.replace("-", ",") + ",",
parentProjectName + project.name + "-" +
project.ext.getProjectVersion() + "-" +
variant.mergedFlavor.versionCode + "-" + gitHash + "-")

output.outputFile = new File(file.parent, name)
}

Because I want to keep all possible suffix that gradle plugin add at the end of the name by default like debug, release, unsigned or possible split variants, etc.
I not found the better way.
Would be great if output object have methods like getNameSuffix()



Dne čtvrtek 11. dubna 2013 13:59:36 UTC+2 Stefan Hoth napsal(a):
Reply all
Reply to author
Forward
0 new messages