-
Notifications
You must be signed in to change notification settings - Fork 8
Using Straas Android SDK
First, you need to add Straas SDK to your application.
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" }
}
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'
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.
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.
- Go to the Straas CMS and click Application tab.
- Click Create Application.
- Fill the form as below:
- application name: for yourself to recognize
- application type: Mobile
- system type: Android
- SHA-1 certificate signature
- Package name (starting from v0.7.0)
- Click Save and you will get a Client ID in this form.
- Set this Client ID in your project and sign this certificate to your app, you will be able to use Straas Android SDK.
- Read the Certificates and keystores.
- 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
- 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
- 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.
Open your build.gradle
of your application Module (which apply the plugin with com.android.application
).
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", ...]
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.
- Open your
strings.xml
file. Example path: /app/src/main/res/values/strings.xml. - Add a new string with the name client_id and value as your Straas <client_id>.
- 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>
After validation, validated Apps can start using functions of Straas Android SDK.
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.
The validation is in methods of SDKs like ChatroomManager.initialize(), developers don't have to take care of this.
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.