0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

local.propertiesの使い方(Android)

Last updated at Posted at 2024-09-22

local.propertiesの使い方忘備録

local.properties

local.propertiesにAPIキーなどの情報を追加する。

local.properties
hoge=HogeHoge

appレベルのbuild.gradle

buildConfigを有効にする。

app/build.gradle.kts
android {
    buildFeatures {
        buildConfig = true
    }
}

local.propertiesから情報を取ってくるコードを追加する。

app/build.gradle.kts
import java.util.Properties

android {
    defaultConfig {
        // 既存のコード

        val localProperties = Properties()
        localProperties.load(rootProject.file("local.properties").inputStream())
        buildConfigField("String", "hoge", "\"${localProperties.getProperty("hoge")}\"" 
    }
}        

build.gradle.ktsに追加ができたら、一度アプリをビルドする。
ビルドすることでBuildConfig.javaが更新されてプロパティの値を利用できるようになる。

コード内での利用

BuildConfig.{プロパティ名}でコード内で利用できる。

MainActivity.kt
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello ${BuildConfig.hoge}!",
        modifier = modifier
    )
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?