LoginSignup
27
22

More than 5 years have passed since last update.

公開したくない情報をlocal.propertiesに定義しbuild.gradleに読み込む

Posted at

GitHubでコードを公開する場合にAPI KEY等は一緒に公開したくありません。
一般的に local.properties.gitignore に指定するため、ここに書いて読み込めば大丈夫だろうということで local.properties に定義した値を読み込む方法を記載します。

local.properties
sdk.dir=/Users/hoge/Documents/Library/android-sdk
api_key= ...
build.gradle
android {
    defaultConfig {
      def properties = new Properties()
      properties.load(project.rootProject.file('local.properties').newDataInputStream())
      // local.propertiesのapi_keyに定義されている値を読み込む
      def API_KEY = properties.getProperty("api_key")
      // ついでにbuildConfigFieldに定義
      buildConfigField("String", "API_KEY", "\"${API_KEY}\"")
    }
}

参考

How do i read properties defined in local.properties in build.gradle

27
22
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
27
22