1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android build時にapkにcommitHashを埋め込んでアプリ上で確認する

Posted at

自分用メモ

/app/build.gradle

//commit hash 取得
def getCommitHasH() {
    def process = "git rev-parse --short HEAD".execute()
    process.waitFor()
    (process.exitValue() == 0) ? process.text.trim() : ""
}

android {
    defaultConfig {
        // BuildConfig へ埋め込み
        buildConfigField("String", "COMMIT_HASH", "\"" + getCommitHasH() + "\"")
    }
}

kotlinで表示制御する場合
HogeActivity.kt

//debugビルドなら表示する
if(BuildConfig.DEBUG){
  commitHashText.text = BuildConfig.COMMIT_HASH
  commitHashText.visibility = View.VISIBLE
}else{
  commitHashText.visibility = View.GONE
}

layoutxmlで表示制御する場合

<!-- import BuildConfig -->
<data>
    <import type="com.hoge.BuildConfig"/>
</data>

<!-- Data binding -->
<TextView
    tools:text="@{BuildConfig.COMMIT_HASH}"
    android:visibility="@{BuildConfig.DEBUG ? View.VISIBLE : View.GONE}"
/>

ref

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?