LoginSignup
0
0

More than 1 year has passed since last update.

【Flutter/Android】DartDefinesで、flavorをする

Posted at

FlutterのAndroidで、flavorをする場合、下記の2つある

  • flutter_flavorizrを使う
  • DartDefinesを使う

flutter_flavorizrは、コマンドで自動的にファイルを作成・変更を行ってくれるので楽なのですが、ファイルが複数出来たり、管理が煩雑になります。
DartDefinesは、手動でファイルを修正する必要がありますが、ファイルが増える訳ではないので、管理が楽になります。
今回は、DartDefinesでflavorを実現したいと思います。

build.gradleを編集

app/build.gradle
...

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

+def dartDefines = [:]
+if (project.hasProperty('dart-defines')) {
+    dartDefines = dartDefines + project.property('dart-defines')
+            .split(',')
+            .collectEntries {
+                def pair = new String(it.decodeBase64(), 'UTF-8').split('=')
+                [(pair.first()): pair.last()]
+            }
+}
...
android {
...
    defaultConfig {
...
+        if (dartDefines.FLAVOR != 'prod') {
+            applicationIdSuffix ".${dartDefines.FLAVOR}"
+        }
+        resValue "string", "app_name", "アプリ名" + (dartDefines.FLAVOR == 'prod' ? '' : "(${dartDefines.FLAVOR})")
...

AndroidManifest.xmlを編集

build.gradleで作成したapp_nameをandroid:labelを設定します。

app/src/main/AndroidManifest.xml
   <application
-        android:label="アプリ名"
+        android:label="@string/app_name"
        android:name="${applicationName}"

###Android Studioのビルドを編集

ビルド環境を編集します。
Run/Debug Configurationsの「Additional run args」に書きを追加します。

--dart-define=FLAVOR=xxx

xxxを使用したいflavorを入力してください。

###デバッグを実行

デバッグでアプリ名が「アプリ名(xxx)」となっていれば成功しています。

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