IBM Cloud Docs
Android용 App Configuration 클라이언트 SDK

Android용 App Configuration 클라이언트 SDK

App Configuration 서비스는 Kotlin 또는 Java 프로그래밍 언어로 작성된 Android 애플리케이션과 통합할 수 있는 Android 클라이언트 SDK를 제공합니다.

전제조건

다음은 Android용 App Configuration 서비스 SDK를 사용하기 위한 선행 조건입니다.

Kotlin으로 작성된 Android 앱용 클라이언트 SDK 통합

App Configuration 서비스는 Android 애플리케이션과 통합할 수 있도록 Android 클라이언트 SDK를 제공합니다. SDK를 통합하여 특성 및 기능 플래그의 값을 평가할 수 있습니다.

  1. 다음 옵션 중 하나를 사용하여 SDK를 설치합니다.

    • 패키지를 다운로드하여 Android Studio로 가져오십시오.
    • 다음을 추가하여 Gradle을 통해 패키지를 가져오십시오.
      • App Configuration Android 클라이언트 SDK 종속 항목을 프로젝트 레벨 build.gradle 파일에 추가하십시오.

        repositories {
            mavenCentral()
        }
        
      • App Configuration Android 클라이언트 SDK 종속 항목을 모듈 레벨 build.gradle 파일에 추가하십시오.

        dependencies {
           implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1"
        }
        
  2. 인터넷 권한을 위해 AndroidManifest.xml 파일을 구성하십시오.

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. SDK를 초기화하십시오.

    import com.ibm.cloud.appconfiguration.android.sdk.AppConfiguration
    
    val collectionId = "airlines-webapp"
    val environmentId = "dev"
    
    val appConfigClient = AppConfiguration.getInstance()
    //application is a member of the AppCompatActivity() class, if you have inherited the
    //AppCompatActivity() class then you can call the application variable.
    appConfigClient.init(application,
                         "region",
                         "guid",
                         "apikey")
    appConfigClient.setContext(collectionId, environmentId)
    

    여기서:

    • region- App Configuration 서비스 인스턴스가 생성되는 지역 이름입니다. 지원되는 위치 목록은 여기를 참조하세요. 예: us-south, au-syd
    • guid - App Configuration 서비스의 GUID입니다. 대시보드의 서비스 인증 정보 섹션에서 가져올 수 있습니다.
    • apikey - App Configuration 서비스의 ApiKey입니다. 대시보드의 서비스 인증 정보 섹션에서 가져올 수 있습니다.
    • collectionId - 콜렉션 섹션 아래의 App Configuration 서비스 인스턴스에서 작성된 콜렉션 ID입니다.
    • environmentId - 환경 섹션 아래의 앱 구성 서비스 인스턴스에서 작성된 환경 ID입니다.
  4. 기능 또는 특성 데이터 변경에 대한 리스너 설정

    appConfiguration.registerConfigurationUpdateListener(object : ConfigurationUpdateListener {
    
        override fun onConfigurationUpdate() {
            // ADD YOUR CODE
        }
    })
    

Kotlin에 작성된 Android 애플리케이션용 특성 및 기능 관련 API 사용 예제

  • 단일 기능 가져오기

    val feature: Feature? = appConfiguration.getFeature("featureId")
    
  • 모든 기능 가져오기

    val features: HashMap<String, Feature>? = appConfiguration.getFeatures();
    
  • 기능 평가

    feature.getCurrentValue() 메소드를 사용하여 기능 플래그의 값을 평가할 수 있습니다. 고유 entityId을(를) 매개변수로 전달하여 기능 플래그 평가를 수행하십시오. 기능 플래그가 App Configuration 서비스에 있는 세그먼트로 구성되어 있는 경우 속성 값을 JSONObject로 설정할 수 있습니다.

    JSONObject entityAttributes = new JSONObject();
    try {
        entityAttributes.put("city", "Bangalore");
        entityAttributes.put("country", "India");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    val appConfiguration = AppConfiguration.getInstance()
    val feature: Feature? = appConfiguration.getFeature("featureId")
    
    if (feature?.getFeatureDataType() === Feature.FeatureType.NUMERIC) {
        val value = feature.getCurrentValue("entityId", entityAttributes)
    } else if (feature?.getFeatureDataType() === Feature.FeatureType.BOOLEAN) {
        val value = feature.getCurrentValue("entityId", entityAttributes)
    } else if (feature?.getFeatureDataType() === Feature.FeatureType.STRING) {
        val value = feature.getCurrentValue("entityId", entityAttributes)
    }
    
  • 단일 특성 가져오기

    val property: Property? = appConfiguration.getProperty("propertyId")
    
  • 모든 특성 가져오기

    val properties: HashMap<String, Property>? = appConfiguration.getProperties();
    
  • 특성 평가

    property.getCurrentValue() 메소드를 사용하여 특성 값을 평가할 수 있습니다. 고유 entityId을(를) 매개변수로 전달하여 특성 평가를 수행하십시오. 특성이 App Configuration 서비스에 있는 세그먼트로 구성되어 있는 경우 속성 값을 JSONObject로 설정할 수 있습니다.

    JSONObject entityAttributes = new JSONObject();
    try {
        entityAttributes.put("city", "Bangalore");
        entityAttributes.put("country", "India");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    val appConfiguration = AppConfiguration.getInstance()
    val property: Property? = appConfiguration.getProperty("propertyId")
    val value = property.getCurrentValue("entityId", entityAttributes)
    

지원되는 데이터 유형

App Configuration 서비스는 다음 데이터 유형으로 기능 플래그와 속성을 구성합니다: 부울, 숫자, 문자열. 문자열 데이터 유형은 텍스트 문자열, JSON 또는 YAML 형식일 수 있습니다. 따라서 SDK는표 1에 표시된 대로 각 형식을 처리합니다.

출력 예시
기능 또는 특성 값 데이터 유형 데이터 형식 getCurrentValue() 에서 리턴된 데이터 유형 예제 출력
true BOOLEAN 적용 불가능 java.lang.Boolean true
25 NUMERIC 적용 불가능 java.lang.Integer 25
"문자열 텍스트" STRING TEXT java.lang.String a string text
{"firefox": {
"name": "Firefox",
"pref_url": "about:config"
}}
STRING JSON org.json.JSONObject {"firefox":{"name":"Firefox","pref_url":"about:config"}}
men:
- John Smith
- Bill Jones
women:
- Mary Smith
- Susan Williams
STRING YAML java.lang.String

`"men:

  • John Smith
  • Bill Jones\women:
  • Mary Smith
  • Susan Williams"`

기능 플래그

val feature: Feature? = appConfiguration.getFeature("json-feature")
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // JSON

// Example below (traversing the returned JSONObject)
if (feature != null) {
   val result = feature.getCurrentValue(entityId, entityAttributes) as JSONObject
   result.get("key") // returns the value of the key
}

val feature: Feature? = appConfiguration.getFeature("yaml-feature")
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // YAML
feature.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check Table 1)

특성

val property: Property? = appConfiguration.getProperty("json-property")
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // JSON

// Example below (traversing the returned JSONObject)
if (property != null) {
  val result = property.getCurrentValue(entityId, entityAttributes) as JSONObject
  result.get("key") // returns the value of the key
}

val property: Property? = appConfiguration.getProperty("yaml-property")
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // YAML
property.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above Table 1)
  • 서버에서 구성을 강제 페치합니다.

    appConfiguration.fetchConfigurations()
    

Java로 작성된 Android 앱용 클라이언트 SDK 통합

App Configuration 서비스는 Android 애플리케이션과 통합할 수 있도록 Android 클라이언트 SDK를 제공합니다. SDK를 통합하여 특성 및 기능 플래그의 값을 평가할 수 있습니다.

  1. 다음 옵션 중 하나를 사용하여 SDK를 설치합니다.

    • 패키지를 다운로드하여 Android Studio로 가져오십시오.
    • 다음을 추가하여 Gradle을 통해 패키지를 가져오십시오.
      • App Configuration Android 클라이언트 SDK 종속 항목을 프로젝트 레벨 build.gradle 파일에 추가하십시오.

        repositories {
           mavenCentral()
        }
        
      • App Configuration Android 클라이언트 SDK 종속 항목을 모듈 레벨 build.gradle 파일에 추가하십시오.

        dependencies {
           implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1"
        }
        
  2. 인터넷 권한을 위해 AndroidManifest.xml 파일을 구성하십시오.

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. 다음 단계에 따라 Kotlin을 Java 프로젝트에 통합하십시오.

    • Kotlin Gradle 플러그인을 모듈 레벨 build.gradle에 추가하십시오.

      dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
      }
      
    • kotlin-android 플러그인을 앱 레벨 build.gradle에 추가하십시오.

      plugins {
         id 'com.android.application'
         id 'kotlin-android'
      }
      
  4. SDK를 초기화하십시오.

    AppConfiguration appConfiguration = AppConfiguration.getInstance();
    appConfiguration.init(getApplication(), "region", "guid", "apikey");
    
    // To start the configuration fetching operation, set the collectionId in the following way.
    appConfiguration.setContext("collectionId", "environmentId");
    

    여기서:

    • region - 서비스 인스턴스가 작성되는 지역 이름입니다. 달라스의 경우 AppConfiguration.REGION_US_SOUTH ,워싱턴 DC의 경우 AppConfiguration.REGION_US_EAST ,런던의 경우 AppConfiguration.REGION_EU_GB ,시드니의 경우 AppConfiguration.REGION_AU_SYD을(를) 사용하십시오.
    • guid - App Configuration 서비스의 GUID입니다. 대시보드의 서비스 인증 정보 섹션에서 가져올 수 있습니다.
    • apikey - App Configuration 서비스의 ApiKey입니다. 대시보드의 서비스 인증 정보 섹션에서 가져올 수 있습니다.
    • collectionId - App Configuration 서비스 인스턴스에서 작성된 콜렉션의 ID입니다.
    • environmentId-환경 섹션 아래의 App Configuration 서비스 인스턴스에서 작성된 환경의 ID.
  5. 기능 변경사항 확인

    appConfiguration.registerConfigurationUpdateListener(new ConfigurationUpdateListener() {
        @Override
        public void onConfigurationUpdate() {
           // ADD YOUR CODE
        }
    });
    

로 작성된 Android 앱의 속성 및 기능 관련 API 사용 예제 Java

특성 및 기능 관련 API를 사용하려면 예제를 참조합니다.

  • 단일 기능 가져오기

    Feature feature = appConfiguration.getFeature("featureId");
    
  • 모든 기능 가져오기

    HashMap<String,Feature> features =  appConfiguration.getFeatures();
    
  • 기능 평가

    feature.getCurrentValue() 메소드를 사용하여 기능 플래그의 값을 평가할 수 있습니다. 고유 entityId을(를) 매개변수로 전달하여 기능 플래그 평가를 수행하십시오. 기능 플래그가 App Configuration 서비스에 있는 세그먼트로 구성되어 있는 경우 속성 값을 JSONObject로 설정할 수 있습니다.

    JSONObject entityAttributes = new JSONObject();
    
    try {
        entityAttributes.put("city", "Bengaluru");
        entityAttributes.put("country", "India");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    AppConfiguration appConfiguration = AppConfiguration.getInstance();
    Feature feature = appConfiguration.getFeature("featureId")
    if(feature != null)
        switch (feature.getFeatureDataType())
            case STRING:
                String value = (String) feature.getCurrentValue(entityId, entityAttributes);
                System.out.println(value);
                break;
            case BOOLEAN:
                Boolean boolVal = (Boolean) feature.getCurrentValue(entityId, entityAttributes);
                System.out.println(boolVal);
                break;
            case NUMERIC:
                Integer intVal = (Integer) feature.getCurrentValue(entityId, entityAttributes);
                System.out.println(intVal);
                break;
        }
    }
    
  • 단일 특성 가져오기

    Property property = appConfiguration.getProperty("propertyId");
    
  • 모든 특성 가져오기

    HashMap<String,Property> properties =  appConfiguration.getProperties();
    
  • 특성 평가

    property.getCurrentValue() 메소드를 사용하여 특성 값을 평가할 수 있습니다. 고유 entityId을(를) 매개변수로 전달하여 특성 평가를 수행하십시오. 특성이 App Configuration 서비스에 있는 세그먼트로 구성되어 있는 경우 속성 값을 JSONObject로 설정할 수 있습니다.

    JSONObject entityAttributes = new JSONObject();
    
    try {
        entityAttributes.put("city", "Bengaluru");
        entityAttributes.put("country", "India");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    AppConfiguration appConfiguration = AppConfiguration.getInstance();
    Property property = appConfiguration.getProperty("propertyId");
    String value = (String) property.getCurrentValue(entityId, entityAttributes);
    

지원되는 데이터 유형

App Configuration 서비스는 다음 데이터 유형으로 기능 플래그와 속성을 구성합니다: 부울, 숫자, 문자열. 문자열 데이터 유형은 텍스트 문자열, JSON 또는 YAML 형식이 될 수 있습니다. SDK는 표 1과 같이 각 형식을 적절히 처리합니다.

기능 플래그

Feature feature = appConfiguration.getFeature("json-feature");
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // JSON

// Example below (traversing the returned JSONObject)
if (feature != null) {
  JSONObject result = (JSONObject) feature.getCurrentValue(entityId, entityAttributes);
  result.get("key") // returns the value of the key
}

Feature feature = appConfiguration.getFeature("yaml-feature");
feature.getFeatureDataType(); // STRING
feature.getFeatureDataFormat(); // YAML
feature.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check above Table 1)

특성

Property property = appConfiguration.getProperty("json-property");
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // JSON

// Example below (traversing the returned JSONObject)
if (property != null) {
  JSONObject result = (JSONObject) property.getCurrentValue(entityId, entityAttributes);
  result.get("key") // returns the value of the key
}

Property property = appConfiguration.getProperty("yaml-property");
property.getPropertyDataType(); // STRING
property.getPropertyDataFormat(); // YAML
property.getCurrentValue(entityId, entityAttributes); // returns the stringified yaml (check Table 1)
  • 서버에서 구성을 강제 페치합니다.

    appConfiguration.fetchConfigurations()