Expert: Doctor Consult using Dependency Injection with Huawei Kits (Account, Crash, Identity and Analytics) in Android App.

Manoj Kumar
4 min readMar 4, 2022


In this article, I will create a Doctor Consult Demo App along with the integration of Huawei Id, Crash, Analytics and Identity Kit. Which provides an easy interface to consult with doctor. Users can choose specific doctors and get the doctor details using Huawei User Address.

By reading this article, you’ll get an overview of HMS Core Identity, Analytics, Crash and Account Kit, including its functions, open capabilities and business value.

HMS Core Identity Service Introduction

Hms Core Identity provides an easy interface to add or edit or delete user details and enables the users to authorize apps to access their addresses through a single tap on the screen. That is, app can obtain user addresses in a more convenient way.


1. Huawei Phone EMUI 3.0 or later.

2. Non-Huawei phones Android 4.4 or later (API level 19 or higher).

3. Android Studio

4. AppGallery Account.

App Gallery Integration process

1. Sign In and Create or Choose a project on AppGallery Connect portal.

2. Navigate to Project settings and download the configuration file.

3. Navigate to General Information, and then provide Data Storage location.

App Development

1. Create A New Project.

2. Configure Project Gradle.

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {url ''}
dependencies {
classpath ""
classpath 'com.huawei.agconnect:agcp:'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

allprojects {
repositories {
maven {url ''}

task clean(type: Delete) {
delete rootProject.buildDir

3. Configure App Gradle.

implementation 'com.huawei.hms:identity:'
implementation 'com.huawei.agconnect:agconnect-auth:'
implementation 'com.huawei.hms:hwid:'

implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
implementation ''
annotationProcessor ''
implementation 'com.huawei.hms:hianalytics:'
implementation 'com.huawei.agconnect:agconnect-crash:'

4. Configure AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

5. Create Activity class with XML UI.


package com.hms.doctorconsultdemo.di;import;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
public class AppModule {
private Application mApplication;
public AppModule(Application mApplication) {
this.mApplication = mApplication;
Application provideApplication() {
return mApplication;


package com.hms.doctorconsultdemo.di;import;import;
import javax.inject.Singleton;import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiModule {
String mBaseUrl; public ApiModule(String mBaseUrl) {
this.mBaseUrl = mBaseUrl;
Cache provideHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
return gsonBuilder.create();
OkHttpClient provideOkhttpClient(Cache cache) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()


package com.hms.doctorconsultdemo.di;
import com.hms.doctorconsultdemo.MainActivity;
import javax.inject.Singleton;
import dagger.Component;
@Component(modules = {AppModule.class, ApiModule.class})
public interface ApiComponent {
void inject(MainActivity activity);


package com.hms.doctorconsultdemo;
import com.hms.doctorconsultdemo.di.ApiComponent;
import com.hms.doctorconsultdemo.di.ApiModule;
import com.hms.doctorconsultdemo.di.AppModule;
import com.hms.doctorconsultdemo.di.DaggerApiComponent;
public class MyApplication extends Application { private ApiComponent mApiComponent; @Override
public void onCreate() {
mApiComponent = DaggerApiComponent.builder()
.appModule(new AppModule(this))
.apiModule(new ApiModule("REST_API_URL"))
public ApiComponent getNetComponent() {
return mApiComponent;


package com.hms.doctorconsultdemo;import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import;import com.huawei.hmf.tasks.Task;
import com.huawei.hms.common.ApiException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final int REQUEST_SIGN_IN_LOGIN = 1002;
private static String TAG = MainActivity.class.getName();
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;
protected void onCreate(Bundle savedInstanceState) {
Button view = findViewById(;
} private void signIn() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
startActivityForResult(mAuthManager.getSignInIntent(), REQUEST_SIGN_IN_LOGIN);
public void onClick(View v) {
switch (v.getId()) {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());
Intent intent = new Intent(this, HomeActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());

App Build Result

Tips and Tricks

Identity Kit displays the HUAWEI ID registration or sign-in page first. The user can use the functions provided by Identity Kit only after signing in using a registered HUAWEI ID.

A maximum of 10 user addresses are allowed.

If HMS Core (APK) is installed on a mobile phone, check the version. If the version is earlier than 4.0.0, upgrade it to 4.0.0 or later. If the version is 4.0.0 or later, you can call the HMS Core Identity SDK to use the capabilities.


In this article, we have learned how to integrate HMS Core Identity in Android application. After completely read this article user can easily implement Huawei User Address APIs by HMS Core Identity, so that User can consult with doctor using Huawei User Address.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.


HMS Identity Docs:

Identity Kit —