Skip to content

Instantly share code, notes, and snippets.

@mbmc
Last active November 13, 2015 01:30
Show Gist options
  • Save mbmc/edea6fef8b05fa732d7f to your computer and use it in GitHub Desktop.
Save mbmc/edea6fef8b05fa732d7f to your computer and use it in GitHub Desktop.
Include last git commit sha-1 hash and comment in Android app
task gitCommitInfo(dependsOn: 'compileDebugSources') {
def gitInfo = 'git log --oneline -n 1'.execute().text.trim()
def path = "${project.android.sourceSets.debug.java.srcDirs.first()}" + "<path_to>/VersionUtil.java"
def updatedContent = new File(path).getText('UTF-8').replaceAll("\".*\"", "\"${gitInfo}\"")
new File(path).write(updatedContent, 'UTF-8')
}
tasks.assembleDebug.doLast {
def path = "${project.android.sourceSets.debug.java.srcDirs.first()}" + "<path_to>/VersionUtil.java"
def updatedContent = new File(path).getText('UTF-8').replaceAll("\".*\"", "\"info\"")
new File(path).write(updatedContent, 'UTF-8')
}
public void setCommitInfo() {
if (BuildConfig.DEBUG) {
textView.setText(VersionUtil.COMMIT_INFO);
}
}
package your.package;
// Should be under app/src/debug/<package>/VersionUtil.java
public class VersionUtil {
public static final String COMMIT_INFO = "info";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment