【Integration Guide】 HUAWEI Account Kit Demo

Search This thread


Senior Member
Nov 30, 2020
In the previous post, we learned the advantages of Account Kit: one-click sign-in authorization in any scenario, secure and reliable services, and convenient integration, as well as giving apps access to the global HUAWEI ID base of potential users. A QR code is also provided at the end of the post for you to download the demo app to experience HUAWEI ID sign-in authorization. The demo has integrated three Account Kit APIs (four in total), and is easy to develop. In this article, we'll show you the demo development to get you familiar with Account Kit.

Before you start, use a browser to scan the QR code below to try the demo app.


(Note: The app may collect relevant information for user statistics.)

Preparing for Demo Development​

l Install Android Studio 3.5 or later.

l Install JDK 1.8 or later.

l Use SDK Platform 19 or later.

l Use Gradle 4.6 or later.

l If you haven't already, register as a developer on HUAWEI Developers.

l Download the sample code of the demo from GitHub.

Configuring the Running Environment (2 minutes)​

1. Use Android Studio to open the demo project.

2. Go to File > Settings > Plugins > Marketplace, enter HMS Toolkit in the search box, and click Install. Note: HMS Toolkit must be version or later.

For details, please refer to Installing HMS Toolkit.


3. Create a package and rename it to something like com.hxb.account in the project. Then, copy the code in the com.huawei.hms.accountsample package to your package, and change the value of package and applicationId to your package name. (Do not use the existing package name in the demo project because it is already registered on HUAWEI AppGallery.)

First, create a package. Move the MainActivity class to your package so that you can search for related files easily during the build.


Change package in the AndroidManifest.xml file to com.hxb.account.


Change applicationId in the build.gradle file to com.hxb.account.


4. Go to HMS > Configuration Wizard to check the environment configuration. If you have not signed in with a HUAWEI ID, Toolkit will first prompt you to do so.


On the Configuration Wizard page displayed, you are prompted that no app corresponding to the package name was detected under the signed-in HUAWEI ID.


Click Link to go to AppGallery Connect and create an app manually:

(a) Click Release.


(b) Click Add project.


(c) Create a project.


(d) Click Add app.


(e) Add the project.


After creating, go back to the Configuration Wizard page and click Retry. This time, the check will be successful.


5. Choose Account Kit.

On the Configuration Wizard page, click Add Kits and choose Account Kit.


The following page will be displayed.


6. Choose a certificate. You can choose Use Android debug certificate and click Generate to generate a certificate fingerprint, as shown in the following figure.


7. Click Next to automatically complete other configurations including toggling on the Account Kit switch in AppGallery Connect, configuring the signing certificate fingerprint, downloading the agconnect-services.json file to the project directory, configuring obfuscation scripts, and adding build dependencies and APK fingerprint required for integrating the Account SDK to the build.gradle file. Once these configurations are completed, the following page will be displayed. If you run into errors, please refer to the corresponding guide on the page.


Packaging and Testing the Demo on Remote Real Device Provided by Toolkit​

After configuration, go to HMS > Cloud Debugging to package and test the demo app.


Select the desired device model.



Click Run to test the demo app.

Key Code for Demo Development​

1. UI Design

Account Kit provides four main APIs for signing in, silently signing in, revoking authorization, and signing out. The demo app integrates the first three.

In the above figure, the Sign in with HUAWEI ID button is created with an encapsulated standard control. Please follow HUAWEI ID Sign-In Button Usage Rules when using the icon elements provided by Huawei.

<p style="line-height: 1.5em;"><com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton

1. Key Code for APIs

(1) Sign-in

Use case: Account Kit, compliant with OAuth 2.0 and OpenID Connect, supports sign-in in two modes: authorization code (for apps with their own servers only) and ID token modes. Select the mode you need.

private void signIn() {
    mAuthParam = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
    mAuthManager = AccountAuthManager.getService(AccountActivity.this, mAuthParam);
    startActivityForResult(mAuthManager.getSignInIntent(), Constant.REQUEST_SIGN_IN_LOGIN);

As shown, setIdToken() means use the ID token mode for authorization. The other mode will show setAuthorizationCode() instead. Their difference will be described later. getSignInIntent() is the API for ID sign-in authorization.

Process the result after authorization.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constant.REQUEST_SIGN_IN_LOGIN) {
// Successful sign-in.
// Obtain user information through parseAuthResultFromIntent.
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
if (authAccountTask.isSuccessful()) {
AuthAccount authAccount = authAccountTask.getResult();
Log.i(TAG, authAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken:\n" + authAccount.getAccessToken());
Log.i(TAG, "OpenId:\n" + authAccount.getOpenId());
Log.i(TAG, "Email:\n" + authAccount.getEmail());
Log.i(TAG, "UnionId:\n" + authAccount.getUnionId());

         // Download avatar by using AsyncTask.
NetService = new NetService(new URLPostHandler() {
public void PostHandler(Bitmap bitmap) {
} else {
Log.i(TAG, "signIn failed: " + ((ApiException)


(2) Silent sign-in

Use case: Authorization is required only on first sign-in to your app using a HUAWEI ID. Subsequent sign-ins using the same HUAWEI ID do not require any authorization.

private void silentSignIn() {
    Task<AuthAccount> task = mAuthManager.silentSignIn();
    task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
       public void onSuccess(AuthAccount authAccount) {
          Log.i(TAG, "silentSignIn success");
    task.addOnFailureListener(new OnFailureListener() {
       public void onFailure(Exception e) {
          // If failed, use getSignInIntent.
          if (e instanceof ApiException) {
             ApiException apiException = (ApiException) e;

Call silentSignIn() to implement silent sign-in.

(3) Authorization revoking

Use case: To improve privacy security, users can unauthorize your app.

private void cancelAuthorization() {
Task<Void> task = mAuthManager.cancelAuthorization();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
public void onSuccess(Void aVoid) {

Log.i(TAG, "cancelAuthorization success");
task.addOnFailureListener(new OnFailureListener() {
public void onFailure(Exception e) {
Log.i(TAG, "cancelAuthorization failure:" + e.getClass().getSimpleName());

Call cancelAuthorization() to revoke authorization.

For more information about Account Kit, please visit:

l Development guide

l Codelab

l Video course (and the course for HMS Core 4.0.)

For more details, you can go to:

Our official website

Demo of Analytics Kit

Android SDK integration documentation

iOS SDK integration documentation

Web SDK integration documentation

Quick app SDK integration documentation

Checkout in forum
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone