KISS πŸ‡ΊπŸ‡¦

Stop the war!

Stop the war in Ukraine! Fuck putin!

More information is at: https://war.ukraine.ua/.

There is a fund to support the Ukrainian Army: https://savelife.in.ua/en/donate/, and there is a special bank account that accepts funds in multiple currencies: https://bank.gov.ua/en/about/support-the-armed-forces. I donated to them. Please donate if you can!

Killer putin

Killer putin. Source: politico.eu.

Arrested putin

"It hasn't happened yet, but it will happen sooner or later. Beautiful photo, isn't it?" Source: twitter.

Save build info of an Android project with gradle

| comments

Half a year ago, I published a script that automates inserting git build info into an iOS app with Xcode. Today, I’d like to share a solution for the same task for an Android app using gradle.

Here’s an extract with comments that does the equivalent of the script written in Groovy, that you can insert at the end of the project’s build.gradle file:

(build_info.gradle) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
task saveBuildInfo {
    doFirst {
        // 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)
    }
}

// once the project is evaluated and the android tasks are created,
// make the new task depend on packageDebug
// http://stackoverflow.com/questions/16853130/run-task-before-compilation-using-android-gradle-plugin/16853376#16853376
gradle.projectsEvaluated {
    packageDebug.dependsOn(saveBuildInfo)
}

Here’s a simple function that returns the build info:

(build_info.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private String getBuildInfo() {
    String buildInfo = null;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(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;
}

This is one of my first scripts in Groovy, so if you have any suggestions for improvements, please leave a comment!

Comments