ひさびさのAndoridネタ。ビルドごとに定数を書き換えてたりファイルを用意していたけど、
build.gradleでBuildConfigに追加したり、resouceを自動生成できるらしい。
こんな感じ。
apply plugin: 'com.android.application' android { compileSdkVersion 28 buildToolsVersion '28.0.3' defaultConfig { applicationId "jp.memorylovers.myapp" minSdkVersion 28 targetSdkVersion 28 versionCode 1 versionName "1.0.0" } buildTypes { debug { debuggable true // ソース内で"BuildConfig.DEBUG_FLAG"を利用できるようになる。 buildConfigField "boolean", "DEBUG_FLAG", "true" // resouce内で"@string/debug_flag"を利用できるようになる。 resValue "string", "debug_flag", "true" } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' // buildTypeごとに設定できる。 buildConfigField "boolean", "DEBUG_FLAG", "false" resValue "string", "debug_flag", "false" } } }
注意点としては、以下の3点
- buildConfigFieldの第3引数は文字列じゃないといけない
- resValueの型はresouceの型なので、"String"ではなく"string"
- defaultConfigにも設定できるけど、buildTypes内で上書きすると警告が出る
以上!!