Android 用 App Configuration クライアント SDK
App Configuration サービスでは、Kotlin または Java プログラミング言語で作成された Android アプリケーションと統合するための Android クライアント SDK をご用意しています。
前提条件
Android 用 App Configuration サービス SDK を使用するための前提条件は以下のとおりです。
- Android API レベル 22 以降
- Android Studio
- Gradle
Kotlin で書かれた Android アプリ用のクライアント SDK の統合
App Configuration サービスは、Android アプリケーションと統合される Android クライアント SDK を提供します。 SDK を統合することで、プロパティーおよび機能フラグの値を評価できます。
-
以下のいずれかのオプションを使用して SDK をインストールします。
- パッケージをダウンロードし、Android Studio プロジェクトにインポートします。
- Gradle からパッケージを取得するには、以下を追加します。
-
プロジェクト・レベルの
build.gradle
ファイルに App Configuration Android クライアント SDK 従属関係を追加します。repositories { mavenCentral() }
-
モジュール・レベルの
build.gradle
ファイルに App Configuration Android クライアント SDK 従属関係を追加します。dependencies { implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1" }
-
-
AndroidManifest.xml
ファイルにインターネット許可を構成します。<uses-permission android:name="android.permission.INTERNET"/>
-
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 サービスの API キー。 ダッシュボードのサービス資格情報セクションから取得してください。collectionId
- 「コレクション」セクションの下の App Configuration サービス・インスタンスで作成されたコレクションの ID。environmentId
-「環境」セクションの下のアプリ構成サービス・インスタンスで作成された環境の ID。
-
機能またはプロパティーのデータ変更に対してリスナーを設定します。
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 サービスは、特徴フラグとプロパティを以下のデータ型で設定します:Boolean、Numeric、String。 Stringデータ型は、テキスト文字列、JSONまたはYAMLのフォーマットである。 したがって、SDK は、表 1 に示すように各フォーマットを処理します。
フィーチャーまたはプロパティーの値 | データ型 | データ形式 | getCurrentValue() によって返されるデータのタイプ |
出力例 |
---|---|---|---|---|
true |
BOOLEAN | 適用外 | java.lang.Boolean |
true |
25 |
NUMERIC | 適用外 | java.lang.Integer |
25 |
"a string text" | STRING | TEXT | java.lang.String |
a string text |
{"firefox": { "name": "Firefox", "pref_url": "about:config" }} |
ストリング | JSON | org.json.JSONObject |
{"firefox":{"name":"Firefox","pref_url":"about:config"}} |
men: - John Smith - Bill Jones ¥ n women: - Mary Smith - Susan Williams |
STRING | YAML | java.lang.String |
`"men:
|
機能フラグ
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)
プロパティー (Property)
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 を統合することで、プロパティーおよび機能フラグの値を評価できます。
-
以下のいずれかのオプションを使用して SDK をインストールします。
- パッケージをダウンロードし、Android Studio プロジェクトにインポートします。
- Gradle からパッケージを取得するには、以下を追加します。
-
プロジェクト・レベルの
build.gradle
ファイルに App Configuration Android クライアント SDK 従属関係を追加します。repositories { mavenCentral() }
-
モジュール・レベルの
build.gradle
ファイルに App Configuration Android クライアント SDK 従属関係を追加します。dependencies { implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1" }
-
-
AndroidManifest.xml
ファイルにインターネット許可を構成します。<uses-permission android:name="android.permission.INTERNET"/>
-
以下の手順で、Java プロジェクトに Kotlin を統合します。
-
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' }
-
-
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 サービスの API キー。 ダッシュボードのサービス資格情報セクションから取得してください。collectionId
- App Configuration サービス・インスタンスで作成されたコレクションの ID。environmentId
- App Configuration サービス・インスタンスの「環境」セクションで作成された環境の ID。
-
機能変更を listen します。
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 サービスは、特徴フラグとプロパティを以下のデータ型で設定します:Boolean、Numeric、String。 「文字列」データ・タイプは、テキスト文字列、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 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()