LoginSignup
7
4

More than 5 years have passed since last update.

Debugビルドだけアプリのapp_nameとapplicationIdSuffixをGitのブランチ名にする

Last updated at Posted at 2017-08-19

動機

ブランチごとに別アプリとしてインストールして一気にテストしたい!applicationIdSuffixだけじゃなく、ホームアプリで表示されるアプリ名を変えておきたい!という、怠惰が故の動機でした。
短時間でサクッとやりたかったのにちょっとだけはまったのでメモとしてね。

前提:gitのコミットが一つ以上ある環境でおためしください。

※注意:それが可能というだけで有用かどうか、問題があるかないか?という事については触れていません。これを書いた理由はブランチを変更すると僕の環境では高確率でMainActivityがみつらかないと怒られるからです。それに確かめてないですが、もしかしたらビルド時間が長くなるかもしれません。用法、用量を考えてお使いくださいませ。

手順

その1:ブランチ名を取得できるようにしてみる

まず。ブランチ名を取得して―ってところをやってみましょう。
https://stackoverflow.com/questions/39393477/how-to-change-applicationid-dynamically

def branch_name = ""
try {
    branch_name = "git rev-parse --abbrev-ref HEAD".execute().text.trim()
} catch (ignored) {
    println "Git not found"
}

これでbrancn名が取れるみたいです。

その2:ブランチ名でapplicationIdSuffixが動的に変更されるようにする

これだけではダメなのでbuildTypesをいじります。

    buildTypes {
        release {
           ...
        }
        debug {
            applicationIdSuffix "debug." + branch_name
        }
    }

これでsyncするとまずインストール時にapplicationIdがブランチ名ごとに違うので、違うブランチのAPKを上書きすることはありません。

その3:ここまでうまくいっているか確認

これでアプリケーションがブランチ名によって別々のアプリとしてAndroid端末にインストール可能になりました。
BuildConfig.javaファイルも確認してみましょう。
BuildConfigとMainActivityクラス内で打てば、BuildConfigクラスを定義してあるjavaファイルをリンクしてくれるのでそちらで移動して確認

package com.example.branchnameapp;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.branchnameapp.debug.master";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

APPLICATION_IDにはしっかりmasterと書かれていますね!:congratulations::congratulations::congratulations:

$ git checkout -b test

とでもやってみましょう。
そしてAndroidStudioのメニューにあるGradleのsyncマークボタンをクリックしてー...。

ちょっと待つ。(2分以上かかるならトイレとかコーヒーとか、モードを変えて気分転換が大事ですよ)


:coffee:

package com.example.branchnameapp;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.branchnameapp.debug.test";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

ちゃんと変わってますねー!うんうん!

その4:どうやってホームアプリ上に表示されているアプリ名を動的に変更するか?

さて。ここで困ってしまいました。(ここではまってました)

どうやら
applicationIdSuffix "debug." + branch_nameとやったように
アプリケーション名を変更するためのapplicationNameのような要素は定義されていないようです。
どうすれば...。

まぁググればすぐ解決するんですが。

簡単に言うと、strings.xmlで定義してあるapp_nameをbuild.gradleのものに置き換える!という方法です。

まずstrings.xmlにあるapp_name定義を消します。(そのままにしたい場合は以下のコードのapp_nameを別のものにして以下脳内でapp_nameと入れ替えて対応してください)

次に、ためしとして上のStackOverFlowにあるように

    resValue 'string', 'app_name', '"MyApp (Debug)"'

を追加します。

さてビルドしてインストールしてみましょう!
Runボタンどーん!


:coffee:

ちゃんとできてますねー。(目を凝らして探してみてくださいw)

その5:app_nameにbranch_nameを適用しよう!

ここまできたら、もうできたもんでしょう!


    resValue 'string', 'app_name', branch_name

でビルド!

ぶー! :no_good::no_good::no_good::no_good::no_good: 

こう?

    resValue 'string', 'app_name', '"${branch_name}"'

ぶー! :no_good::no_good::no_good::no_good::no_good:

    resValue 'string', 'app_name', "\"${branch_name}\""

:ok_woman::ok_woman::ok_woman::ok_woman:
できました!!

完成品

apply plugin: 'com.android.application'


def branch_name = ""
try {
    branch_name = "git rev-parse --abbrev-ref HEAD".execute().text.trim()
} catch (ignored) {
    println "Git not found"
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.branchnameapp"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix "debug." +branch_name
            // remove strings.xml app_name in debug flavor
            resValue 'string', 'app_name', "\"${branch_name}\""
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
}

こんな感じです。

僕はちょっといじって

 resValue 'string', 'app_name', "\"0${branch_name}\""

と0を最初にいれるようにして、ホームアプリのアプリ一覧で名前順で一番上の方にでるようにしてます。
こうしてると何かと楽なのでね!

では良いAndroidアプリ開発生活を!:innocent:

7
4
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
7
4