Fartab
Fartab

Reputation: 5513

Android gradle build config - Rename generated APKs

I need to save each APK with a special name whenever I build one.
This is the desired name format: "buildType appName_version_Date_Time.apk"
Example: "debug myAppName_v1.0_20161009_0854.apk"
Example: "release myAppName_v1.0_20161009_0854.apk"

For this purpose I added this gradle script:

 buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.release
    }
    debug {

    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def formattedDate = new Date().format('yyyyMMdd_HHmm')
            println(output.outputFile)
            def newName = variant.name + " myAppName_v" + defaultConfig.versionName + "_" + formattedDate + ".apk"
            output.outputFile = new File(output.outputFile.parent, newName)
        }
    }

}

This script works, but android studio couldn't find the renamed APK. I also tried clean/rebuild project but it ocures again.

Android stuio error:
The APK file path\to\project\app\build\outputs\apk\debug myAppName_v1.0_[20161009_0854].apk does not exist on disk.
Error while Installing APK

When I check "build\outputs\apk" directory i see that an APK has been built and its name is "myAppName_v1.0_[20161009_0856].apk".

What's wrong here??

Upvotes: 1

Views: 788

Answers (2)

orip
orip

Reputation: 75537

Instead of including the current time (with minutes, no less) in the APK filename, consider the following approaches:

  • Take the timestamp of the latest version control commit (that stays stable)
  • Instead of changing the apk filename, copy it from it's non-timestamped name - that Android Studio will easily find - to a timestamped apk.

Here's an example to try (haven't run it but should give the general idea). Choose either Files.copy or the copy task, not both.

applicationVariants.all { variant ->
  variant.outputs.each { output ->
    def copyApkTask = tasks.create(name: "copy" + variant.name + "Apk") {
      def newName = ... // can include the timestamp with minutes
      println(newName)

      // Using java.nio.Files.copy
      def targetPath = new File(output.outputFile.parent, newName).toPath()
      Files.copy(output.outputFile.toPath(), targetPath)

      // Using gradle's Copy logic (clunky for single-file copy and rename)
      copy {
        from output.outputFile
        into output.outputFile.parent
        rename { String fileName ->
          newName
        }
      }
    }
    copyApkTask.mustRunAfter variant.assemble
  }
}

Upvotes: 2

Amir Ziarati
Amir Ziarati

Reputation: 15097

you are telling gradle to include the minute in the name of your apk.

then it builds it with the name you want in the n minute, and when it wants to refer to it in the n+1 minute it cant find the file. so it encounters this error.

you better use the commit hash or the date in your apk name. (not including small units like minute or second or even hour)

Upvotes: 2

Related Questions