Skip to content

Using Straas Android SDK

Ray edited this page Aug 24, 2021 · 3 revisions

Add Straas Android SDK

First, you need to add Straas SDK to your application.

1. Add repositories

Add repositories in the build.gradle file in the root of your project:

repositories {
    google()
    maven { url "https://raw.githubusercontent.com/Straas/Straas-android-sdk-releases/main" }
}

2. Add Straas module dependencies

Then, add the dependencies in the build.gradle file of your application module:

X.X.X is the your preferred version. For the version information, see CHANGELOG

  • Media browser & playback:
implementation 'io.straas.android.sdk:straas-media-core:X.X.X'
  • ChatRoom:
implementation 'io.straas.android.sdk:straas-messaging:X.X.X'
  • Streaming:
implementation 'io.straas.android.sdk:straas-streaming:X.X.X'

Registering your application

You need to register your application, including your digitally signed .apk file's public certificate in the Straas CMS, to be able to use all the Straas Android SDK modules. The following steps explain how to register your application and certificate as well as how to set up your application's authorization credentials.

Get Client ID

After you apply the official account from Straas.io, you could generate your Client ID by providing the SHA-1 certificate signature and package name (starting from v0.7.0) for Straas CMS.

  1. Go to the Straas CMS and click Application tab.
  2. Click Create Application.
  3. Fill the form as below:
  1. Click Save and you will get a Client ID in this form.
  2. Set this Client ID in your project and sign this certificate to your app, you will be able to use Straas Android SDK.

Generate SHA-1 certificate signature

  1. Read the Certificates and keystores.
  2. Prepare a keystore of your own APK. You could generate a keystore by yourself or use the provided keystore debug.keystore.
  • Store password: android
  • Key alias: androiddebugkey
  • Key password: android
  1. Create SHA-1 certificate signature by your signing certificate in the keystore. Please note that the keystore must be the same as the one that your android app is signed with.
    You could generate SHA-1 certificate signature by the following command:
keytool -exportcert -alias <alias_name> -keystore <keystore_file> | openssl sha1 -hex
  1. You will get a string like the following one:

(stdin)= 6d2eb151363ed0a10c53df11b7631e7b2e213b30

The string 6d2eb151363ed0a10c53df11b7631e7b2e213b30 (which does not includes "(stdin)= ") is your SHA-1 certificate signature.

Set Straas Client ID

Open your build.gradle of your application Module (which apply the plugin with com.android.application).

Version 0.6.0 or later

Set straas_client_id key-value pair with manifestPlaceholders property in your android > buildTypes > debug/release section:

android {
    buildTypes {
        debug {
            manifestPlaceholders = [straas_client_id: "$your_debug_client_id"]
            // ... other debug setting
        }
        release {
            manifestPlaceholders = [straas_client_id: "$your_release_client_id"]
            // ... other release setting
        }
    }
}

The value $your_debug/release_client_id could be copied from client_id

Note: Because manifestPlaceholders is a Map, if you have multiple key-value pairs in your application, make sure append them like this:

manifestPlaceholders = [straas_client_id: "$your_client_id", keyA:"valueA", keyB:"valueB", ...]

Version 0.3.0 ~ 0.5.x

Copy client_id into gradle.properties at project root:

your_debug_client_id=xxxxx
your_release_client_id=xxxxx

These value will finally be read in resValue by build.gralde in sample.

If you don't want to set this value by gradle.properties, you could set resValue client_id immediately as well.

Before Version 0.3.0

  1. Open your strings.xml file. Example path: /app/src/main/res/values/strings.xml.
  2. Add a new string with the name client_id and value as your Straas <client_id>.
  3. Add a meta-data element to the application element:
<application android:label="@string/app_name" ...>
    ...
    <meta-data android:name="io.straas.sdk.ApplicationId" android:value="@string/client_id"/>
    ...
</application>

Validation

After validation, validated Apps can start using functions of Straas Android SDK.

(WIP) For media-core-kt and messaging-kt(Version 1.0.0 or later)

Use kotlin:

lifecycleScope.launch {
     val context = getContext()
     val straasCore = StraasCore.getInstance(context)
     straasCore.validate(context)
}

After validation, you can use this straasCore to do create Straas modules, e.g. val chatroomManager = straasCore.createChatroomManager(StraasMember.GUEST).

StraasCore is a singleton object, it means you only need to validate once if you pass.

For other cases

The validation is in methods of SDKs like ChatroomManager.initialize(), developers don't have to take care of this.

Troubleshooting

If you meet CredentialFailException.WrongInformationException after starting your app like this:

E/Straas Credential: io.straas.android.sdk.authentication.credential.CredentialFailException$WrongInformationException: Please check your client ID, keystore hashes and applicationId.
                     https://github.com/Straas/Straas-android-sdk-sample/wiki/SDK-Credential

It may be related to provide the wrong client id, package name, or key hashes. You could manually modify the sample code to use the right key hash. For example in StraasDemoActivity class make a temporary change to the onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // Add code to print out the key hash and package name
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("Straas", "applicationId: " + getPackageName() +
                    "\nSHA-1 certificate signature: " + ByteString.of(md.digest()).hex());
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
    
    ...

Save your changes and re-run the sample. Check your logcat output for a message similar to this:

18:28:49.696 24594-24594/io.straas.android.sdk.demo D/Straas: applicationId: io.straas.android.sdk.demo
                                                              SHA-1 certificate signature: c1e14cba9320b0ccbad09a2zeie7d00ff7b0fe35

Then please check your client ID is binding with the same package name and SHA-1 certificate signature.

Clone this wiki locally