DEV Community

chuross
chuross

Posted on

4

Androidでアプリの定数をBuildConfigではなくYamlから自動生成したい - Gradle Config Plugin

経緯

やりたかったこと

  • アプリケーションで使う定数的な値を一括管理したい
  • ProductFlavorごとにAPIの向き先など設定情報を変えたい
  • それらを管理するためのクラスを自分で作りたくない

BuildConfigを使わない理由

  • build.gradleが肥大化する
  • 定数の記述が多少面倒
    • いちいち型を記述しないといけないので面倒
    • すべての値を文字列で埋め込むのも好みでない

Gradle Config Plugin

https://github.com/tmiyamon/gradle-config

特徴

  • ProductFlavorごとにYamlを定義できる
  • Yamlから値に参照するためのクラスが自動生成される
    • 値の型はYamlの記述内容に合わせて自動的に解決してくれる

導入

buildscriptにプラグインを追加

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.tmiyamon:gradle-config:0.2.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

プラグインを適用する

アプリのプロジェクトがあるbuild.gradleに以下を追記
(app/build.config)

apply plugin: 'com.tmiyamon.config'

使い方

コンフィグファイルを作る

アプリのプロジェクトルートにconfigディレクトリを追加する

ファイル名は以下が対応している

config/default.yml
config/default_secret.yml
config/${productFlavor}.yml
config/${productFlavor}_secret.yml
config/${buildType}.yml
config/${buildType}_secret.yml
Enter fullscreen mode Exit fullscreen mode

ProductFlavorに応じてこれらのファイルが適宜読み込まれて1つのクラスが生成される

*_seciet.ymlのファイルは秘匿情報を入れることができる。使う場合はバージョン管理下に置かないように.gitignoreに入れておくこと

Yamlに項目を埋めていく

あとはYamlを作っていくだけ

size: 2
server: google.com
section:
  size: 3
  servers: [ {name: yahoo.com}, {name: amazon.com} ]
Enter fullscreen mode Exit fullscreen mode

このように記述するとビルド時にSettingsという名前のクラスが生成される

生成されたクラスはこのように値を参照できるようになっている

Settings.size   // => 2
Settings.server // => google.com
Settings.section.size // => 3
Settings.section.servers.get(0).name // => yahoo.com
Settings.section.servers.get(1).name // => amazon.com
Enter fullscreen mode Exit fullscreen mode

便利!!!😃

応用

kotlinの拡張を使って定数と引数を組み合わせた文字列を作る

例えばベースのURLだけYamlに書いておいて、パスの組み合わせをkotlinの拡張で実現する

url:
  base: https://hoge.com
Enter fullscreen mode Exit fullscreen mode

続いてkotlin側でプロパティを拡張する

fun Settings.Url.getDetail(id: String) = "$base/detail/$id"
Enter fullscreen mode Exit fullscreen mode

こんな感じでアプリ上で参照できるようになる

Settings.url.getDetail("1") // => https://hoge.com/detail/1
Enter fullscreen mode Exit fullscreen mode

あとがき

設定情報は何かと細かく組み合わせたり、たくさん記述したりする部分なので、管理しやすくしたい

このプラグインはそうした部分の要求を叶えてくれるよいアプローチに思えた

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (2)

Collapse
 
krutikkhatri profile image
krutikkhatri

You can also use yamlonline.com/ for the yaml validator as well as yaml converter to json,csv,xml,base64 also for beautify and minify YAML.

Collapse
 
jamesmalvi profile image
Jaimie Malvi

Love you share a tool which helps to validate YAML data.

codebeautify.org/yaml-validator

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools