Keyring offers the Credentials Management API for storing user credentials locally on Android phones and tablets and sharing them between different apps and different platform versions of an app.
In this codelab, you will call the APIs of Keyring in the demo project and experience how to:
In this codelab, you will learn how to:
To integrate Keyring, you must complete the following preparations:
Now, you have successfully enabled Keyring for your app.
If you are using Android Studio, you can integrate the HMS Core SDK via the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project.
If you have enabled certain services in AppGallery Connect, add the agconnect-services.json file to your app.
Step 1 - Sign in to AppGallery Connect and click My projects.
Step 2 - Find your app project and click the app that needs to integrate the HMS Core SDK.
Step 3 - Go to Project settings > General information. In the App information area, download the agconnect-services.json file.
Step 4 - Copy the agconnect-services.json file to your app's root directory.
—-End
The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.
|
Gradle plugin earlier than 7.0 |
Gradle plugin 7.0 |
Gradle plugin 7.1 or later |
Gradle plugin earlier than 7.0
buildscript {
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
// Add the AppGallery Connect plugin configuration. Please refer to AppGallery Connect Plugin Dependency to select a proper plugin version.
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
}
}
allprojects {
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
}
Gradle plugin 7.0
buildscript {
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
// Add the Android Gradle plugin configuration. You need to replace {version} with the actual Gradle plugin version, for example, 7.0.1.
classpath 'com.android.tools.build:gradle:{version}'
// Add the AppGallery Connect plugin configuration. Please refer to AppGallery Connect Plugin Dependency to select a proper plugin version.
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
}
}
dependencyResolutionManagement {
...
repositories {
google()
jcenter()
// Configure the Maven repository address for the HMS Core SDK.
maven {url 'https://developer.huawei.com/repo/'}
}
}
Gradle plugin 7.1 or later
buildscript {
dependencies {
...
// Add the Android Gradle plugin configuration. You need to replace {version} with the actual Gradle plugin version, for example, 7.1.1.
classpath 'com.android.tools.build:gradle:{version}'
// Add the AppGallery Connect plugin configuration. Please refer to AppGallery Connect Plugin Dependency to select a proper plugin version.
classpath 'com.huawei.agconnect:agcp:1.6.0.300'
}
}
plugins {
...
}
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
// Configure the Maven repository address for the HMS Core SDK.
maven { url 'https://developer.huawei.com/repo/' }
}
}
dependencyResolutionManagement {
...
repositories {
google()
mavenCentral()
// Configure the Maven repository address for the HMS Core SDK.
maven { url 'https://developer.huawei.com/repo/' }
}
}
Step 1 - Open the app-level build.gradle file of your project.
Step 2 - Add the AppGallery Connect plugin configuration in either of the following methods:
apply plugin: 'com.huawei.agconnect'
plugins {
id 'com.android.application'
// Add the following configuration:
id 'com.huawei.agconnect'
}
Step 3 - Add a dependency in the dependencies block.
dependencies {
implementation 'com.huawei.hms:keyring-credential:{version}'
}
—-End
android {
defaultConfig {
...
resConfigs "en", "zh-rCN", "Other languages supported by your app"
}
}
For details about the languages supported by the HMS Core SDK, please refer to Languages Supported by the HMS Core SDK.
Click Sync Now to synchronize your project.
CredentialClient credentialClient = CredentialManager.getCredentialClient(this);
Kotlin:
val credentialClient = CredentialManager.getCredentialClient(this)
List<AppIdentity> trustedAppList = new ArrayList<>();
trustedAppList.add(new AndroidAppIdentity("yourAppName", "yourAppPackageName", "yourAppCodeSigningCertHash"));
trustedAppList.add(new WebAppIdentity("youWebSiteName", "www.yourdomain.com"));
trustedAppList.add(new WebAppIdentity("youWebSiteName", "login.yourdomain.com"));
SharedCredentialFilter sharedCredentialFilter = SharedCredentialFilter.acceptTrustedApps(trustedAppList);
credentialClient.findCredential(sharedCredentialFilter, new CredentialCallback<List<Credential>>() {
@Override
public void onSuccess(List<Credential> credentials) {
if (credentials.isEmpty()) {
Toast.makeText(MainActivity.this, R.string.no_available_credential, Toast.LENGTH_SHORT).show();
} else {
for (Credential credential : credentials) {
}
}
}
@Override
public void onFailure(long errorCode, CharSequence description) {
Toast.makeText(MainActivity.this, R.string.query_credential_failed, Toast.LENGTH_SHORT).show();
}
});
Kotlin:
val credentialClient = CredentialManager.getCredentialClient(this)
val trustedAppList: MutableList<AppIdentity> = ArrayList()
trustedAppList.add(AndroidAppIdentity("yourAppName", "yourAppPackageName", "yourAppCodeSigningCertHash"))
trustedAppList.add(WebAppIdentity("youWebSiteName", "www.yourdomain.com"))
trustedAppList.add(WebAppIdentity("youWebSiteName", "login.yourdomain.com"))
val sharedCredentialFilter = SharedCredentialFilter.acceptTrustedApps(trustedAppList);
credentialClient.findCredential(sharedCredentialFilter, object : CredentialCallback<List<Credential>> {
override fun onSuccess(credentials: List<Credential>) {
if (credentials.isEmpty()) {
Toast.makeText(this@MainActivity, R.string.no_available_credential, Toast.LENGTH_SHORT).show()
} else {
for (credential in credentials) {
}
}
}
override fun onFailure(errorCode: Long, description: CharSequence) {
Toast.makeText(this@MainActivity, R.string.query_credential_failed, Toast.LENGTH_SHORT).show()
}
})
private Credential mCredential; // Obtained credentials.
mCredential.getContent(new CredentialCallback<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
String hint = String.format(getResources().getString(R.string.get_password_ok),
new String(bytes));
Toast.makeText(MainActivity.this, hint, Toast.LENGTH_SHORT).show();
mResult.setText(new String(bytes));
}
@Override
public void onFailure(long l, CharSequence charSequence) {
Toast.makeText(MainActivity.this, R.string.get_password_failed,
Toast.LENGTH_SHORT).show();
mResult.setText(R.string.get_password_failed);
}
});
Kotlin:
credential!!.getContent(object : CredentialCallback<ByteArray?> {
override fun onSuccess(content: ByteArray?) {
// Processing if the credentials are obtained successfully.
//var password = String(content!!)
val hint: String = kotlin.String.format(getResources().getString(R.string.get_password_ok),
String(content!!))
Toast.makeText(this@MainActivity, hint, Toast.LENGTH_SHORT).show()
mResult.setText(String(content!!))
}
override fun onFailure(l: Long, charSequence: CharSequence?) {
// Processing if the credentials fail to be obtained.
Toast.makeText(this@MainActivity, R.string.get_password_failed,
Toast.LENGTH_SHORT).show()
mResult.setText(R.string.get_password_failed)
}
})
AndroidAppIdentity app2 = new AndroidAppIdentity(sharedToAppName,
sharedToAppPackage, sharedToAppCertHash);
List<AppIdentity> sharedAppList = new ArrayList<>();
sharedAppList.add(app2);
Credential credential = new Credential(username, CredentialType.PASSWORD, userAuth,
password.getBytes());
credential.setDisplayName("user_niceday");
credential.setSharedWith(sharedAppList);
credential.setSyncable(true);
credentialClient.saveCredential(credential, new CredentialCallback<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(MainActivity.this,
R.string.save_credential_ok,
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(long errorCode, CharSequence description) {
Toast.makeText(MainActivity.this,
R.string.save_credential_failed + " " + errorCode + ":" + description,
Toast.LENGTH_SHORT).show();
}
});
Kotlin:
val app2 = AndroidAppIdentity(sharedToAppName, sharedToAppPackage, sharedToAppCertHash)
val sharedAppList: MutableList<AppIdentity> = ArrayList()
sharedAppList.add(app2)
val credential = Credential(username, CredentialType.PASSWORD, userAuth, password.toByteArray())
credential.setDisplayName("user_niceday")
credential.setSharedWith(sharedAppList)
credential.syncable = true
credentialClient.saveCredential(credential, object : CredentialCallback<Void?> {
override fun onSuccess(unused: Void?) {
Toast.makeText(this@MainActivity,
"save credential OK",
Toast.LENGTH_SHORT).show()
}
override fun onFailure(errorCode: Long, description: CharSequence) {
Toast.makeText(this@MainActivity,
"save credential failed. $errorCode:$description",
Toast.LENGTH_SHORT).show()
}
})
CredentialClient credentialClient = CredentialManager.getCredentialClient(this);
Kotlin:
val credentialClient = CredentialManager.getCredentialClient(this)
List<AppIdentity> trustedAppList = new ArrayList<>();
trustedAppList.add(new AndroidAppIdentity("yourAppName", "yourAppPackageName", "yourAppCodeSigningCertHash"));
trustedAppList.add(new WebAppIdentity("youWebSiteName", "www.yourdomain.com"));
trustedAppList.add(new WebAppIdentity("youWebSiteName", "login.yourdomain.com"));
SharedCredentialFilter sharedCredentialFilter = SharedCredentialFilter.acceptTrustedApps(trustedAppList);
credentialClient.findCredential(sharedCredentialFilter, new CredentialCallback<List<Credential>>() {
@Override
public void onSuccess(List<Credential> credentials) {
if (credentials.isEmpty()) {
Toast.makeText(MainActivity.this, R.string.no_available_credential, Toast.LENGTH_SHORT).show();
} else {
for (Credential credential : credentials) {
// Further process the available credentials, including obtaining the credential information and content and deleting the credentials.
}
}
}
@Override
public void onFailure(long errorCode, CharSequence description) {
Toast.makeText(MainActivity.this, R.string.query_credential_failed, Toast.LENGTH_SHORT).show();
}
});
Kotlin:
val credentialClient = CredentialManager.getCredentialClient(this)
val trustedAppList: MutableList<AppIdentity> = ArrayList()
trustedAppList.add(AndroidAppIdentity("yourAppName", "yourAppPackageName", "yourAppCodeSigningCertHash"))
trustedAppList.add(WebAppIdentity("youWebSiteName", "www.yourdomain.com"))
trustedAppList.add(WebAppIdentity("youWebSiteName", "login.yourdomain.com"))
val sharedCredentialFilter = SharedCredentialFilter.acceptTrustedApps(trustedAppList);
credentialClient.findCredential(sharedCredentialFilter, object : CredentialCallback<List<Credential>> {
override fun onSuccess(credentials: List<Credential>) {
if (credentials.isEmpty()) {
Toast.makeText(this@MainActivity, R.string.no_available_credential, Toast.LENGTH_SHORT).show()
} else {
for (credential in credentials) {
// Further process the available credentials, including obtaining the credential information and content and deleting the credentials.
}
}
}
override fun onFailure(errorCode: Long, description: CharSequence) {
Toast.makeText(this@MainActivity, R.string.query_credential_failed, Toast.LENGTH_SHORT).show()
}
})
credentialClient.deleteCredential(credential, new CredentialCallback<Void>() {
@Override
public void onSuccess(Void unused) {
String hint = String.format(getResources().getString(R.string.delete_ok),
credential.getUsername());
Toast.makeText(MainActivity.this, hint, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(long errorCode, CharSequence description) {
String hint = String.format(getResources().getString(R.string.delete_failed),
description);
Toast.makeText(MainActivity.this, hint, Toast.LENGTH_SHORT).show();
}
});
Kotlin:
credentialClient.deleteCredential(credential, object : CredentialCallback<Void?> {
override fun onSuccess(unused: Void?) {
val hint = String.format(resources.getString(R.string.delete_ok),
credential.getUsername())
Toast.makeText(this@MainActivity, hint, Toast.LENGTH_SHORT).show()
}
override fun onFailure(errorCode: Long, description: CharSequence) {
val hint = String.format(resources.getString(R.string.delete_failed),
description)
Toast.makeText(this@MainActivity, hint, Toast.LENGTH_SHORT).show()
}
})
Well done. You have successfully completed this codelab and learned how to:
For more information, please click the following link:
Related documents
To download the sample code, please click the button below: