{"id":1860,"date":"2016-06-28T20:18:17","date_gmt":"2016-06-28T20:18:17","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=1860"},"modified":"2016-06-28T20:18:17","modified_gmt":"2016-06-28T20:18:17","slug":"automatic-apk-versioning-with-git-and-gradle","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2016\/06\/automatic-apk-versioning-with-git-and-gradle\/","title":{"rendered":"Automatic APK versioning with git and gradle"},"content":{"rendered":"<p>Until recently, all my <a href=\"http:\/\/www.frozax.com\/\">Android mobile games<\/a> were built using <a href=\"http:\/\/www.eclipse.org\">Eclipse<\/a>. As I made a new game recently, I found out that now everything is made for <a href=\"https:\/\/developer.android.com\/studio\/index.html\">Android Studio and Gradle<\/a>. I didn&#8217;t want to change my build system at first but recently found two reasons to make the change:<\/p>\n<ul>\n<li>I want to use recent APIs (Android TV, AdMob) and all the docs are mostly for Android Studio<\/li>\n<li>I find Android Studio much better (and lighter) than Eclipse<\/li>\n<\/ul>\n<p>I&#8217;m using <a href=\"http:\/\/www.cocos2d-x.prg\">cocos2d-x<\/a> and they are still using Eclipse but with the help of some resources available online, I could have a proper Gradle build. I didn&#8217;t know Gradle and to learn a bit about it, I decided to rename my APK directly in Gradle instead of using external scripts.<\/p>\n<p>When building, I like to create an APK with a version number. I&#8217;m therefore using the git commit counts as a version number. For example, the latest APK of <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.frozax.thermometers\">Thermometers Puzzles<\/a> is called <code>swg26-release-943.apk<\/code>.<br \/>\n* <code>swg26<\/code> is the name of the project (basically, it&#8217;s my 26th game, but not all have been released \ud83d\ude09 ).<br \/>\n* it&#8217;s either a <code>release<\/code> (signed) or <code>debug<\/code> version<br \/>\n* it&#8217;s version <code>943<\/code> (943 git commits were made)<\/p>\n<p>To do that automatically, I took inspiration from <a href=\"https:\/\/github.com\/google\/eddystone\/blob\/master\/tools\/eddystone-validator\/EddystoneValidator\/variants.gradle\">this script<\/a> from Google and came up with this:<br \/>\n<script src=\"https:\/\/gist.github.com\/frozax\/15464da76b4de9bbf896f551cb635e49.js\"><\/script><\/p>\n<p>In this script, I also compute the git commit id (<code>gitSha<\/code>) just as an example, but I prefer to use only the git commit count for readability. I save my APK on a server in a directory with the version name, it allows me to have builds from all platforms (iOS, Amazon, Windows Phone&#8230;) on the same location.<\/p>\n<p>To use it, I include it in my main <code>build.gradle<\/code> using:<br \/>\n<code>project.ext.applicationName = \"swg26\"<br \/>\napply from: \"..\/apk-naming.gradle\"<\/code><\/p>\n<p>Feel free to use and share this code! \ud83d\ude42<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2016%2F06%2Fautomatic-apk-versioning-with-git-and-gradle%2F&amp;linkname=Automatic%20APK%20versioning%20with%20git%20and%20gradle\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2016%2F06%2Fautomatic-apk-versioning-with-git-and-gradle%2F&amp;linkname=Automatic%20APK%20versioning%20with%20git%20and%20gradle\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2016%2F06%2Fautomatic-apk-versioning-with-git-and-gradle%2F&amp;linkname=Automatic%20APK%20versioning%20with%20git%20and%20gradle\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2016%2F06%2Fautomatic-apk-versioning-with-git-and-gradle%2F&amp;linkname=Automatic%20APK%20versioning%20with%20git%20and%20gradle\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Until recently, all my Android mobile games were built using Eclipse. As I made a new game recently, I found out that now everything is made for Android Studio and Gradle. I didn&#8217;t want to change my build system at first but recently found two reasons to make the change: I want to use recent&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2016\/06\/automatic-apk-versioning-with-git-and-gradle\/\">Continue reading <span class=\"screen-reader-text\">Automatic APK versioning with git and gradle<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,12],"tags":[],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/1860"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=1860"}],"version-history":[{"count":10,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions"}],"predecessor-version":[{"id":1870,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions\/1870"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}