Remote Configuration for Android using Alibaba Cloud Table Store

By Sai Sarath Chandra Alibaba Cloud Tech Share Author and Alibaba Cloud MVP

In this tutorial, you will learn how to use Table Store to create a simple user information database with a form accessible on mobile devices.

Prerequisites:

• You need an Alibaba Cloud Account. If you need you can get one with $300 by signing up here.
• Need Android Studio 3.0.0
• Basic knowledge of Android/Java.

Tutorial:

1.Activate Table Store Service:

a) Please login to Alibaba Cloud Console and Activate the Table Store service, if you are using this for the first time.

b) If you haven’t used before you will see a “Activate Now” button. Upon which the service will be activated.
c) Go ahead to the Table Store console and “Create Instance” in the region you are interested in.
d) Fill in the relevant details here.
e) And your Instance should be created. Please click on the Instance and copy the endpoint.
f) This is the Internet endpoint is what we are interested in. Please copy it and keep it for future reference.
g) You can follow the below Illustrative guide for the obtaining access keys.
https://www.alibabacloud.com/help/doc-detail/51665.htm?spm=a3c0i.o51500en.b99.9.733cc1c22ZHdyM

2.Create an Android Project with the name you want

a) Select next and I have selected the minimum target Android version as Android Kitkat. Please find the screenshot below.

b) Click next and select empty activity

c) Click next and leave the defaults as is and select “Finish” button

d) Let the Gradle build finish and then follow ahead

3.Downloading dependencies :

a) We need to download 2 jar files which are the core for using the Alibaba Cloud. Please use the following link and download “tablestore-4.2.0-jar-with-dependencies.jar” & “slf4j-api-1.7.7.jar”.
http://central.maven.org/maven2/com/aliyun/openservices/tablestore/4.2.0/
http://central.maven.org/maven2/org/slf4j/slf4j-api/1.7.7/
b) Add the jars into the libs folder in the projectview and add the jars to the dependencies

c) Make sure you right click the jar and the select “Add as library”.

4.Adding dependencies to app level gradle.

The following should be your dependencies in gradle.

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'joda-time:joda-time:2.9.9'
compile 'com.android.support:design:26.1.0'
implementation files('libs/tablestore-4.2.0-jar-with-dependencies.jar')
implementation files('libs/slf4j-api-1.7.7.jar')
}

Code level Guide:

Project Files:

1.We have three layouts

a) activity_main.xml
b) activity_config.xml
c) activity_welcome.xml

2.we have three activities

a) WelcomeActivity.java
b) RemoteConfig.java
c) MainActivity.java

3.One Strings.xml file, which handles the externalization of strings in the project.

4.One Styles.xml, which externalizes the styles in the project

Deep Dive:

1.First of all, we will talk about creating the layouts of the application. For reference, the following screenshot shows how the final layout will look like:

To achieve this create activity_welcome.xml in res > layout & copy the following code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

This is a relative layout which consists of a Image View and Two Button. It is pretty simple layout

2.Create activity_config.xml and copy the following code in the same way what we have did earlier.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

3.The final layout of the file will look something like this:

4.Create activity_main.xml and copy the following code. The final layout will look something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sample.alibabacloud.remoteconfig.MainActivity">

5.Since we have created all the layouts, we will now work on creating the Activities. Create MainActivity.java file by right clicking on the package > New > Java Class and copy the following code

package sample.alibabacloud.remoteconfig;

6.Create RemoteConfig.java file and copy the following code

package sample.alibabacloud.remoteconfig;

7.Create WelcomeActivity.java and copy the following code

package sample.alibabacloud.remoteconfig;

8.Final configuration files:

• Open your “AndroidManifest.xml”and copy the following code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.alibabacloud.remoteconfig">

• Open your styles.xml under the values folder and copy the following one.

<resources>

• Update the strings.xml with the values you store earlier

<resources>
<string name="app_name">RemoteConfig</string>

Detailed Code Explanation:

  1. In this Remote Configuration project, we are storing some app attributes like the maximum number of characters that the person can enter for his name, minimum age and the maximum age allowed, background image. We can see the user app analytics, review comments in appstore and sometimes we think we need to change some parameters for the code. We tend to launch and update which may not be efficient as we can achieve the same through remote configuration.
  2. When the Code loads the activity_welcome.xml is loaded and the respective WelcomeActivity is initialized. This happens through the WeclomeActivity declaration in the Android manifest.
  3. The welcome activity is responsible for checking the app is installed in the device for the first time or is it installed already. We use this scenario to setup the local secured
    SharedPreferences file.
  4. If the app is opening for the first time we will create a table called “Remote Configuration”& Create a primary key named “Version”. And we will update the values to be default. This will be onetime activity along side we will also create the sharedpreferences file and update the values to be default.
  5. We will also copy the same logic onto the onPause() method so whenever the user navigates to the home screen the app can fetch the latest values from the server.
  6. The UserForm screen is responsible for checking the latest values from the TableStore and updating the same in the form.
  7. The RemoteConfig is responsible for updating the values in the TableStore.
  8. You can see all the methods are self-explanatory.
  9. If everything is done correctly all your compilation issues will go away and the application starts installing in by clicking the small play(run) button in the status bar of the android studio.

I strongly recommend to clone the repo you will eliminate lot of manual errors and get this app running in minutes.

Please take a look at this repo for the final code repo and let me know if you face any issues or raise any pull requests for improvements.

Reference:

https://www.alibabacloud.com/blog/Remote-Configuration-for-Android-using-Alibaba-Cloud-Table-Store_p313872?spm=a2c41.11214695.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store