공부방/Android

Android build value

soycrab 2019. 12. 19. 14:06

예를 들어 디버그와 릴리즈시 

코드상에 분기를 태워 각각 다른 값을 사용할수도 있고, 

다른 방법으로 Build.gradle(app) 에서 사용하는 방법도 있다. 

gradle 에 값을 추가하고 가져오는 방법에는 두가지 정도가 있는데, 

 

한가지는 BuildConfig 에서 가져오는 방법과 

res 에서 가져오는 방법이 있다. 

선언 방식 

 android {
      ...
      buildTypes {
        release {
          // These values are defined only for the release build, which
          // is typically used for full builds and continuous builds.
          buildConfigField("String", "BUILD_TIME_01", "\"릴리즈 맞춤형 필드 String값\"")
          buildConfigField("int", "BUILD_TIME_02", "1234")
          buildConfigField("boolean", "BUILD_TIME_03", "true")
          
          resValue("string", "build_time", "릴리즈 리소스 값")
          ...
        }
        debug {
          // Use static values for incremental builds to ensure that
          // resource files and BuildConfig aren't rebuilt with each run.
          // If these rebuild dynamically, they can interfere with
          // Apply Changes as well as Gradle UP-TO-DATE checks.
          buildConfigField("String", "BUILD_TIME_01", "\"디버그 맞춤형 필드 String값\"")
          buildConfigField("int", "BUILD_TIME_02", "4321")
          buildConfigField("boolean", "BUILD_TIME_03", "false")
          
          resValue("string", "build_time", "디버그 리소스 값")
        }
      }
    }
    ...
    

 

사용 방법 

맞춤형 필드 사용시 
Log.i(TAG, BuildConfig.BUILD_TIME_01)

리소스값 사용시
Log.i(TAG, getString(R.string.build_time))

 

선언해 놓고 빌드 or gradle sync 한번은 돌려줘야 

인식한다. 

 

 

 

 

 

 

출처 : 

https://developer.android.com/studio/build/gradle-tips

 

Gradle 도움말 및 레시피  |  Android Developers

Gradle과 Gradle용 Android 플러그인은 Android 앱이나 라이브러리를 컴파일, 빌드 및 패키징하기 위한 유연한 방법을 제공합니다.

developer.android.com

 

반응형