Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android + Gradle: Best place to set dynamic archivesBaseName

In our Android project we want the filename of our APK to contain information such as the date, git branch name and short commit hash.

We've been setting this as part of our defaultConfig

android {
   defaultConfig {
      setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")    
   }
}

This works most of the time but our build sometimes fails with errors pointing to the value being stale (for example after switching branches). Forcing Gradle Sync in Android Studio usually fixes it.

So is there a better, more robust way of making sure that property stays up to date and forces a gradle sync, if necessary?

like image 217
Krzysztof Kozmic Avatar asked May 12 '16 03:05

Krzysztof Kozmic


2 Answers

I was having same issue, they were not always uptodate with the build.

I solved it by using:

project.ext.set("archivesBaseName", "myAppName"); 

under the defaultConfig block, and I stopped having the problem.

Not sure it will work for the branch, build number or date, but it's worth giving it a shot. Here's my configs, including the defaultConfig Block. For my case I was only using a hard coded versionName and build Number (I call it versionCode because I set it in the manifest dynamically). But maybe you can tweak it to your needs:

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 22

    versionCode System.getenv("BUILD_NUMBER") as Integer ?: 0

    versionName "6.2.5." + versionCode

}
project.ext.set("archivesBaseName", "myAppname" + defaultConfig.versionName);
like image 183
Y2theZ Avatar answered Nov 15 '22 18:11

Y2theZ


try to call clean.execute() before

android { ... }

it is the cheapest 'sync' task I can think of :)

   clean.execute()
   android {
       defaultConfig {
          setProperty("archivesBaseName", "$projectName.$branchName.$date.$buildNumber.$versionCode-$versionName-$versionSha")    
       }
    }
like image 30
kalin Avatar answered Nov 15 '22 20:11

kalin