App Configuration-Client-SDK für Android

Der App Configuration-Service stellt das Android-Client-SDK für die Integration in Ihre Android-Anwendung bereit, die in der Programmiersprache Kotlin oder Java geschrieben ist.

Voraussetzungen

Im Folgenden finden Sie die Voraussetzungen für die Verwendung des App Configuration-Service-SDK für Android:

Client-SDK für in Kotlin geschriebene Android-App integrieren

Der App Configuration-Service stellt das Android-Client-SDK für die Integration in Ihre Android-Anwendung bereit. Sie können die Werte Ihrer Eigenschaft und des Feature-Flags durch Integration des SDK auswerten.

  1. Installieren Sie das SDK mit einer der folgenden Optionen:

    • Laden Sie das Paket herunter und importieren Sie es in Ihr Android Studio-Projekt.
    • Rufen Sie das Paket über Gradle ab, indem Sie Folgendes hinzufügen:
      • Fügen Sie die Abhängigkeit des App Configuration-Android-Client-SDK zur Datei build.gradle auf Projektebene hinzu.

        repositories {
            mavenCentral()
        }
        
      • Fügen Sie die Abhängigkeit des App Configuration-Android-Client-SDK zur Datei build.gradle auf Modulebene hinzu.

        dependencies {
           implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1"
        }
        
  2. Konfigurieren Sie die Datei AndroidManifest.xml für die Internetberechtigung.

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. Initialisieren Sie das 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)
    

    Dabei gilt:

    • region- Name der Region, in der die Dienstinstanz App Configuration erstellt wird. Eine Liste der unterstützten Standorte finden Sie hier. Zum Beispiel: us-south, au-syd usw.
    • guid - GUID des App Configuration-Service. Rufen Sie dies aus dem Abschnitt für Serviceberechtigungsnachweise des Dashboards auf.
    • apikey - API-Schlüssel des App Configuration-Service. Rufen Sie dies aus dem Abschnitt für Serviceberechtigungsnachweise des Dashboards auf.
    • collectionId -ID der Sammlung, die in der App Configuration Serviceinstanz unter dem Abschnitt "Sammlungen" erstellt wurde.
    • environmentId -Die ID der Umgebung, die in der Serviceinstanz für die App-Konfiguration unter dem Abschnitt "Umgebungen" erstellt wurde.
  4. Listener für Feature-oder Eigenschaftsdatenänderungen festlegen

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

Beispiele für die Verwendung von Eigenschafts- und Funktionsbezogenen APIs für Android-App in Kotlin

  • Einzelnes Feature abrufen

    val feature: Feature? = appConfiguration.getFeature("featureId")
    
  • Alle Features abrufen

    val features: HashMap<String, Feature>? = appConfiguration.getFeatures();
    
  • Feature-Auswertung

    Sie können die Methode feature.getCurrentValue() verwenden, um den Wert des Feature-Flags auszuwerten. Übergeben Sie eine eindeutige entityId als Parameter, um die Feature-Flag-Auswertung durchzuführen. Wenn das Feature-Flag im App Configuration-Service mit Segmenten konfiguriert ist, können Sie die Attributwerte als JSONObject festlegen.

    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)
    }
    
  • Einzeleigenschaft abrufen

    val property: Property? = appConfiguration.getProperty("propertyId")
    
  • Alle Eigenschaften abrufen

    val properties: HashMap<String, Property>? = appConfiguration.getProperties();
    
  • Auswertung der Eigenschaft

    Sie können die Methode property.getCurrentValue() verwenden, um den Wert der Eigenschaft zu bewerten. Übergeben Sie eine eindeutige entityId als Parameter für die Eigenschaftsauswertung. Wenn die Eigenschaft mit Segmenten im App Configuration Service konfiguriert ist, können Sie die Attributwerte als JSONObjectfestlegen.

    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)
    

Unterstützte Datentypen

App Configuration dienst konfiguriert das Merkmalskennzeichen und die Eigenschaften in den folgenden Datentypen: Boolesch, Numerisch, String. Der Datentyp String kann das Format eines Textstrings, JSON oder YAML haben. Dementsprechend verarbeitet das SDK jedes Format wie in Tabelle 1 dargestellt.

Beispiel Outputs
Feature oder Eigenschaftswert Datentyp Datenformat Typ der von getCurrentValue() zurückgegebenen Daten Beispielausgabe
true BOOLEAN nicht zutreffend java.lang.Boolean true
25 NUMERIC nicht zutreffend java.lang.Integer 25
"ein Zeichenfolgetext" 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"`

Feature-Flag

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)

Eigenschaft

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)
  • Erzwingt das Abrufen der Konfigurationen vom Server.

    appConfiguration.fetchConfigurations()
    

Client-SDK für in Java geschriebene Android-App integrieren

Der App Configuration-Service stellt das Android-Client-SDK für die Integration in Ihre Android-Anwendung bereit. Sie können die Werte Ihrer Eigenschaft und des Feature-Flags durch Integration des SDK auswerten.

  1. Installieren Sie das SDK mit einer der folgenden Optionen:

    • Laden Sie das Paket herunter und importieren Sie es in Ihr Android Studio-Projekt.
    • Rufen Sie das Paket über Gradle ab, indem Sie Folgendes hinzufügen:
      • Fügen Sie die Abhängigkeit des App Configuration-Android-Client-SDK zur Datei build.gradle auf Projektebene hinzu.

        repositories {
           mavenCentral()
        }
        
      • Fügen Sie die Abhängigkeit des App Configuration-Android-Client-SDK zur Datei build.gradle auf Modulebene hinzu.

        dependencies {
           implementation "com.ibm.cloud:appconfiguration-android-sdk:0.3.1"
        }
        
  2. Konfigurieren Sie die Datei AndroidManifest.xml für die Internetberechtigung.

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. Integrieren Sie Kotlin in Ihr Java-Projekt mit den folgenden Schritten:

    • Fügen Sie das Kotlin Gradle-Plug-in zur Modulebene hinzu build.gradle

      dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
      }
      
    • Fügen Sie das kotlin-android-Plug-in zu build.gradle auf App-Ebene hinzu.

      plugins {
         id 'com.android.application'
         id 'kotlin-android'
      }
      
  4. Initialisieren Sie das 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");
    

    Dabei gilt:

    • region - Regionsname, unter dem die Serviceinstanz erstellt wird. Verwenden Sie AppConfiguration.REGION_US_SOUTH für Dallas, AppConfiguration.REGION_US_EAST für Washington DC, AppConfiguration.REGION_EU_GB für London und AppConfiguration.REGION_AU_SYD für Sydney.
    • guid - GUID des App Configuration-Service. Rufen Sie dies aus dem Abschnitt für Serviceberechtigungsnachweise des Dashboards auf.
    • apikey - API-Schlüssel des App Configuration-Service. Rufen Sie dies aus dem Abschnitt für Serviceberechtigungsnachweise des Dashboards auf.
    • collectionId -ID der Sammlung, die in der App Configuration Serviceinstanz erstellt wurde.
    • environmentId-ID der Umgebung, die in der App Configuration-Serviceinstanz im Abschnitt „Umgebungen“ erstellt wurde.
  5. Empfangsbereitschaft für Änderungen an Features

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

Beispiele für die Verwendung von eigenschafts- und funktionsbezogenen APIs für Android-Anwendungen, die in Java

Sehen Sie sich die Beispiele zur Verwendung der APIs für Eigenschaften und Funktionen an.

  • Einzelnes Feature abrufen

    Feature feature = appConfiguration.getFeature("featureId");
    
  • Alle Features abrufen

    HashMap<String,Feature> features =  appConfiguration.getFeatures();
    
  • Feature-Auswertung

    Sie können die Methode feature.getCurrentValue() verwenden, um den Wert des Feature-Flags auszuwerten. Übergeben Sie eine eindeutige entityId als Parameter, um die Feature-Flag-Auswertung durchzuführen. Wenn das Feature-Flag im App Configuration-Service mit Segmenten konfiguriert ist, können Sie die Attributwerte als JSONObject festlegen.

    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;
        }
    }
    
  • Einzeleigenschaft abrufen

    Property property = appConfiguration.getProperty("propertyId");
    
  • Alle Eigenschaften abrufen

    HashMap<String,Property> properties =  appConfiguration.getProperties();
    
  • Auswertung der Eigenschaft

    Sie können die Methode property.getCurrentValue() verwenden, um den Wert der Eigenschaft zu bewerten. Übergeben Sie eine eindeutige entityId als Parameter für die Eigenschaftsauswertung. Wenn die Eigenschaft mit Segmenten im App Configuration Service konfiguriert ist, können Sie die Attributwerte als JSONObjectfestlegen.

    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);
    

Unterstützte Datentypen

App Configuration dienst konfiguriert das Merkmalskennzeichen und die Eigenschaften in den folgenden Datentypen: Boolesch, Numerisch, String. Der Zeichenfolgedatentyp kann das Format einer TEXT-Zeichenfolge, JSON oder YAML haben. Das SDK verarbeitet jedes Format entsprechend, wie in Tabelle 1 dargestellt.

Feature Flag

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)

Eigenschaft

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)
  • Erzwingt das Abrufen der Konfigurationen vom Server.

    appConfiguration.fetchConfigurations()