Gradle: create build info in assets folder while build - no compile error within Eclipse - with helper function in AppUtils

This commit is contained in:
Peter Siegmund
2015-03-31 15:54:23 +02:00
parent c1e64cc20b
commit 99d767d1b0
4 changed files with 76 additions and 34 deletions
+1
View File
@@ -32,3 +32,4 @@ out/
.gradle
local.properties
git.xml
build.info
@@ -3,8 +3,20 @@ package com.sap.sailing.android.shared.util;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AppUtils {
private final static String TAG = AppUtils.class.getName();
private AppUtils() {
}
public static PackageInfo getPackageInfo(Context app) {
try {
return app.getPackageManager().getPackageInfo(app.getPackageName(), 0);
@@ -12,4 +24,25 @@ public class AppUtils {
return null;
}
}
public static String getBuildInfo(Context context) {
String buildInfo = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(context.getAssets().open("build.info")));
buildInfo = reader.readLine();
} catch (IOException e) {
Log.d(TAG, "Can't open file with build info", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buildInfo;
}
}
+42 -28
View File
@@ -1,30 +1,44 @@
/**
* returns current git hash
*/
def getGitHash = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine "git", "log", "-n", "1", "--format='%h'"
standardOutput = stdout
}
return stdout.toString().trim().replace("'", "")
}
/**
* set git hash into git.xml
*/
preBuild.doLast {
copy {
from ("../git.template.xml")
into ("res/values")
rename {
String fileName -> fileName.replace(".template", "")
}
filter {
String line -> line.replaceAll("#GITHASH#", getGitHash())
}
// get the current commit hash in git
def git_version = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', '-1', '--format=%h'
standardOutput = git_version
}
}
git_version = git_version.toString().trim()
// get the current git branch, if any
def git_branch = new ByteArrayOutputStream()
exec {
commandLine 'git', 'symbolic-ref', '--short', '-q', 'HEAD'
standardOutput = git_branch
// ignore error output as we might not be on a branch
ignoreExitValue = true
}
// if we are not on a branch, try to get a tag for the commit
def git_branch_or_tag
if (git_branch.size() > 0) {
git_branch_or_tag = git_branch.toString().trim()
} else {
def git_tag = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--exact-match'
standardOutput = git_tag
// ignore error output
errorOutput = new ByteArrayOutputStream()
ignoreExitValue = true
}
git_branch_or_tag = git_tag.toString().trim()
}
def build_time = new Date().toString()
// save the combined build info into assets/build.info file
def result_line = git_branch_or_tag + "-" + git_version + " (" +
build_time + ")\n"
def assetsDir = android.sourceSets.main.assets.srcDirs.toArray()[0]
def buildInfoFile = new File(assetsDir, 'build.info').getAbsolutePath()
new File(buildInfoFile).write(result_line)
}
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="git_hash" translatable="false">#GITHASH#</string>
</resources>