IBM Cloud Docs
適用於 Android 的 App Configuration 用戶端 SDK

適用於 Android 的 App Configuration 用戶端 SDK

App Configuration 服務提供 Android 用戶端 SDK,可與以 Kotlin 或 Java 程式設計語言撰寫的 Android 應用程式整合。

必要條件

以下是使用 App Configuration for Android 服務 SDK 的必要條件:

整合以 Kotlin 撰寫的 Client SDK for Android 應用程式

App Configuration 服務提供 Android 用戶端 SDK 以與 Android 應用程式整合。 您可以整合 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-在 Collections 區段下 App Configuration 服務實例中建立的集合 ID。
    • environmentId-在 App Configuration 服務實例的「環境」區段下建立的環境 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 撰寫的 Client SDK for Android 應用程式

App Configuration 服務提供 Android 用戶端 SDK 以與 Android 應用程式整合。 您可以整合 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 代表達拉斯,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
        }
    });
    

針對以 Java 撰寫的 Android 應用程式使用內容及特性相關 API 的範例

請參閱使用內容及特性相關 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()