0

I use the code below to generate an .apk file and it works fine. But, to be able to debug I need to comment the code around "applicationVariants.all", otherwiser Android Studio says the file was not found.

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def apk = output.outputFile;
                    def newName = "app-release-" + getDate() + ".apk";
                    output.outputFile = new File(apk.parentFile, newName);
                }
            }

        }
    }

How can I do to make it work for generate .apk file and also to debug on Android Studio?

UPDATE

I found what is happening, actually as I use date and time in the file name, the time in the generated file is different from the time that Android Studio tries to install.

My function getDate() returns this:

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmm')
    return formattedDate
}

The file created is app-release-201507110957.apk. However, in the Android Studio console, the error is:

Uploading file
    local path: /Volumes/Macintosh HD/AndroidstudioProjects/App/app/build/outputs/apk/app-release-201507110956.apk
    remote path: /data/local/tmp/com.domain.app
Local path doesn't exist.

The file name on generated file has 1 minute ahead than the file name Android Studio tries to install. Any idea about how I can fix that? I would like to have hour and minute in the file name because I might generate more than one version a day for QA team.

fcberg
  • 764
  • 13
  • 29
  • `applicationVariants` is not a meber of `buildTypes.release` ... it is a sibling to `buildTypes` – Selvin Jul 09 '15 at 11:08

2 Answers2

0

At the moment the renaming is part of your release build. Just put the renaming as general action, like this:

android {

  ...

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

    debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  applicationVariants.all { variant ->
      variant.outputs.each { output ->
      def apk = output.outputFile;
      def newName = "app-release-" + getDate() + ".apk";
      output.outputFile = new File(apk.parentFile, newName);
    }
  }
}
Rich
  • 1,015
  • 1
  • 10
  • 22
  • Still same problem: "Uploading file local path: /Volumes/Macintosh HD/AndroidstudioProjects/App/app/build/outputs/apk/app-release-201507091037.apk remote path: /data/local/tmp/com.domain.myapp Local path doesn't exist." – fcberg Jul 09 '15 at 13:41
  • did you try to add a debug build type as you did for the release type (see my updated answer). – Rich Jul 10 '15 at 05:03
  • I found what is happening. Please see my updated question. – fcberg Jul 11 '15 at 13:09
  • did you take a look on this: http://stackoverflow.com/questions/17132770/android-studio-local-path-doesnt-exist – Rich Jul 13 '15 at 05:20
0

I solved my problem by verifying if the variant is debuggable or not.

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                if (!variant.buildType.isDebuggable()) {
                    variant.outputs.each { output ->
                        def apk = output.outputFile;
                        def newName = "app-release-" + getDate() + ".apk";
                        output.outputFile = new File(apk.parentFile, newName);
                    }
                }
            }
        }
    }

Like that I apply my file name only for release version. Debuggable ones will continue with same name set by Android Studio, so it won't generate problems when debugging the app.

fcberg
  • 764
  • 13
  • 29