سؤال

I want to show the current Hash/Branch Ref of the GIT repo in the About dialog of my Android Application. I can copy it by hand, but it's much more interesting to do it programatically.

I have a library for my apps, so my activities and my applications inherit from my own library classes. So, I thought adding a method [getGitHash()] to my library application class. I can refer to the current branch by reading .git/HEAD, which is virtually referenced in a res/raw file under Eclipse project (look here to see how it's done). My project has the R.raw.HEAD resource now.

But now the library can't reference the R.raw.HEAD, so I can't implement the method in the library. The method in the library should be something like:

public static String getGitHash(int nDigits) {
    
    String hash = "";
    SB_ResourceReader.LoadRAWFile(R.raw.HEAD,SB_Application.getContext());
    return hash;
}

.LoadRAWFile() is a static method to read raw file content and SB_Application.getContext() is another static method to retrieve... well, the app context.

Is it possible for the library to 'trust' that there will be a certain resource although it is not accesible when compiling?

I hope I have explained myself :S

Thanks!

لا يوجد حل صحيح

نصائح أخرى

More easily (without any plugin), you can achieve this by setting a new resource in your app.gradle like so:

android {
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1"
        resValue "string", "gitBranch", gitBranch()
    }
    buildTypes {...}
}


def gitBranch() {
    def branch = ""
    def proc = "git rev-parse --abbrev-ref HEAD".execute()
    proc.in.eachLine { line -> branch = line }
    proc.err.eachLine { line -> println line }
    proc.waitFor()
    branch
}

So you'll have access to a new string resource

getString(R.string.gitBranch)

If you are using eclipse, try the following: add the .git folder as a source folder, and as inclusion pattern just write "HEAD". This will result in the HEAD file to be stored as an asset (not an android resource!) in the jar. You can access it using the getClass().getResourceAsStream("/HEAD") method:

    try {
        InputStream is = getClass().getResourceAsStream("/HEAD");
        byte buff[] = new byte[256];
        int len = is.read(buff);
        String s = new String(buff, 0, len);
        System.out.println("!!! HEAD: " + s);
    } catch (Exception e) {
        e.printStackTrace();
    }

So the way to do this is to script it in your ant build file. You should checkout the various ant file tasks. I think in you case the ReplaceRegEx is just what you need. Put some sort of unique string in the file where you want to put the current git branch your on, run a command to get the current git branch, and replace the unique string with the output of that command.

Thanks @Hugo Gresse for your answer. If you are using gradle.kts (Kotlin), then you will need following modifications:

In your build.gradle.kts file:

    android {
        defaultConfig {
            compileSdkVersion(29)
            applicationId = "xxx.xxxxxx.xxxx"
            minSdkVersion(21)
            targetSdkVersion(29)
            versionCode = 3
            versionName = "0.0.3"
            resValue("string", "gitBranch", getGitBranchName())
        }
    }

    // function to read current git branch name
    fun getGitBranchName(): String {
        val process = Runtime.getRuntime().exec("git rev-parse --abbrev-ref HEAD")
        val sb: StringBuilder = StringBuilder()
        while (true) {
            val char = process.inputStream.read()
            if (char == -1) break
            sb.append(char.toChar())
        }
        return sb.toString()
    }

Then you will have access to new string res named gitBranch:

getString(R.string.gitBranch)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top