AndroidStudio: Renamed APK is not used when installing the app

1,346 views
Skip to first unread message

Stefan Hoth

unread,
Jun 19, 2013, 10:03:01 AM6/19/13
to adt...@googlegroups.com
Hi,

to have a better idea what I just build I renamed my APKs according a few rules using this:

buildTypes {
        //debug, release, etc...

        applicationVariants.each { variant ->

            def apk = variant.packageApplication.outputFile;
            def newName = "";

            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + ".apk");
            
            variant.packageApplication.outputFile = new File(apk.parentFile, newName);
            if (variant.zipAlign) {
                variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
            }
        }
    }

Result is something like "MyCoolApp-v1.0.15-RELEASE.apk"

While this is working well on the cmd line I get an error when trying to install the app from Android Studio since it doesn't pick up the changed name and only fails with
"Local path doesn't exist."

Is there a rename the apk within gradle so Android Studio understands it?

Cheers,
Stefan

Xavier Ducrohet

unread,
Jun 19, 2013, 11:11:44 AM6/19/13
to adt...@googlegroups.com
did you reload the build.gradle?


--
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,
Jun 19, 2013, 11:32:25 AM6/19/13
to adt...@googlegroups.com
Yes I did, of course. :-)

The output after assembleMyFlavorDebug is called is:

Uploading file
local path: C:\workspaces\myproject\app\build\apk\app-myFlavor-debug.apk
remote path: /data/local/tmp/com.myapp.debug
Local path doesn't exist.

Can you reproduce this behavior?

Stefan

Xavier Ducrohet

unread,
Jun 19, 2013, 11:48:37 AM6/19/13
to adt...@googlegroups.com
I just took a quick look at it and the output file coming from the model is disconnected from the final task output (ie it's a copy not a link).

Stefan Hoth

unread,
Jun 19, 2013, 12:03:07 PM6/19/13
to adt...@googlegroups.com
That's what I thought.

"1 person starred this issue"

Thanks,
Stefan

Xavier Ducrohet

unread,
Jun 19, 2013, 12:25:07 PM6/19/13
to adt...@googlegroups.com
BTW this code of yours is awful... (my fault, not yours).

You should be able to do just this

android.applicationVariants.each { variant ->

    def apk = variant.outputFile // you can already do that, and it'll return the right name whether you zip align or not

    // change apk to something else
    ...

    variant.outputFile = apk
}

Those methods already exist but the setter on variant.outputFile doesn't do the right thing.

Rainer Burgstaller

unread,
Jun 20, 2013, 12:42:37 AM6/20/13
to adt...@googlegroups.com
I am wondering whether such a naming convention for output files such as Stefan has proposed shouldnt be the default? IMHO very few people will be happy with every apk having the same name. In the "ant" days, you always hooked into the build chain and then had to rename it. I have seen such a process in many projects. Of course customization is important but good defaults would be nice.

cheers
- Rainer

Xavier Ducrohet

unread,
Jun 20, 2013, 1:13:51 AM6/20/13
to adt...@googlegroups.com
That's true, we can definitively improve the default name.


Stefan Hoth

unread,
Jul 3, 2013, 7:35:11 AM7/3/13
to adt...@googlegroups.com
Since the bug is not fixed in 1.0.9 I want to ask if there's way to recognize that a build is started from Android Studio instead of the console. This way I could skip the renaming while building in AS. Any advice how I could achieve this?

Thanks,
Stefan

Xavier Ducrohet

unread,
Jul 3, 2013, 2:22:37 PM7/3/13
to adt...@googlegroups.com
The fix requires a change on the gradle side. We have an upcoming synchronized released of the Gradle plugin and Studio with a modified model that will support this.

Vishal Vyas

unread,
Mar 27, 2015, 12:36:58 PM3/27/15
to adt...@googlegroups.com
Hi Xavier Ducrohet, even I was facing the similar issue and in the below script in the getDate() function

def formattedDate = date.format('yyyy-MM-dd-HH:mm') // this works
def formattedDate = date.format('yyyy-MM-dd-HH:mm:ss') // this doesn't. and it gives local path doesnot exists (it has the 'ss' seconds switch)

// script start
android {
    
    ....

    defaultConfig {....}

    buildTypes {
        debug {
            def formattedDate = getDate()
            versionNameSuffix "debug-" + formattedDate
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(output.outputFile.parent,
                            output.outputFile.name.replace("-debug", "-" + formattedDate)
                    )
                }
            }
        }
    }

    sourceSets {....}

}

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyy-MM-dd-HH:mm') // this works
//    def formattedDate = date.format('yyyy-MM-dd-HH:mm') // this doesn't. and it gives local path doesnot exists.
    return formattedDate
}

// script end

AndroidStudio Current version : 1.2 Preview 3 EAP.0 (build 141.1793788)
Gradle version : 2.2.1

any thoughts on this?

Xavier Ducrohet

unread,
Mar 27, 2015, 1:38:35 PM3/27/15
to adt...@googlegroups.com
I'm guessing this works for a minute. If you wait a bit it'll stop working :)

For more options, visit https://groups.google.com/d/optout.

Alex D.

unread,
Apr 6, 2015, 3:17:08 PM4/6/15
to adt...@googlegroups.com
I keep getting irregular "I/O error (The system cannot find the file specified)" errors, I highly suspect there is an issue with gradle.


    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def fileName = outputFile.name.replace(".apk", "-" + get_some_string() + ".apk")
                output.outputFile = new File(outputFile.parent, fileName)
Reply all
Reply to author
Forward
0 new messages