
# apikey.properties
API_KEY="testkey"
BASE_URL="https://api.themoviedb.org/3/"
BASE_URL_IMAGE="https://image.tmdb.org/t/p/w1280"
properties 형태입니다.스트림에 저장되고 로드된다고합니다!
open class Properties : Hashtable<Any!, Any!>
import java.util.Properties
## properties 파일이 존재하는 지확인후 파일을 읽어옴
val properties = Properties()
val apiPropertiesFile = project.rootProject.file("apikey.properties")
if (apiPropertiesFile.exists()) {
properties.load(apiPropertiesFile.inputStream())
}
android {
namespace = "com.eunho.moviewshow"
compileSdk = 34
## 키값에 맞는 value들을 가져옵니다
val apiKey = properties.getProperty("API_KEY")
val baseUrl = properties.getProperty("BASE_URL")
val baseUrlImage = properties.getProperty("BASE_URL_IMAGE")
defaultConfig {
## defaultConfig 안에서 buildConfig에 등록해줍니다.
buildConfigField("String", "API_KEY", apiKey)
buildConfigField("String", "BASE_URL", baseUrl)
buildConfigField("String", "BASE_URL_IMAGE", baseUrlImage)
}
buildFeatures {
compose = true
## buildconfig true 추가해주어야합니다
buildConfig = true
}
}
}
gradle.properties(project단) 파일에 아래코드를 복붙하시면됩니다
android.defaults.buildfeatures.buildconfig=true
빌드를 하게된다면
BuildConfig.java에 final 클래스로 생성이됩니다.
간단하게 BuildConfig에서 불러오면됩니다.
val apiKey = BuildConfig.API_KEY