Expert: Courier App MVVM Jetpack (HMS CloudDB Kit) in Android using Kotlin- Part-3
Published: 2022-04-15 12:19:10

**

## Overview
**
In this article, I will create a Courier android application using Kotlin in which I will integrate HMS Core kits such as HMS Account, AuthService, Push and Cloud DB Kit.
We have integrated HMS Account and AuthService Kit in part-1 and Client Push Notification Using HMS Push Kit in Part-2 of this series. Kindly go through the link below-
Part-1 https://forums.developer.huawei.com/forumPortal/en/topic/0202841957497640128

Part-2 https://forums.developer.huawei.com/forumPortal/en/topic/0201847982965230092
App will make use of android MVVM clean architecture using Jetpack components such as DataBinding, AndroidViewModel, Observer, LiveData and much more.
In this article, we are going to implement DataBinding using Observable pattern.
**

## Huawei Cloud DB Kit Introduction
**
Huawei Cloud DB is a device-cloud synergy database product that enables seamless data synchronization between the device and cloud and between devices, and supports offline application operations, helping you quickly develop device-cloud and multi-device synergy applications.
**

## Prerequisite
**
1.Huawei Phone EMUI 3.0 or later.
2.Non-Huawei phones Android 4.4 or later (API level 19 or higher).
3.HMS Core APK 4.0.0.300 or later
4.Android Studio
5.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.
4.Navigate to My Projects > Project Settings > Build > Cloud DB, and click Enable now.
5.Click Add
6.Click Export, select Java, android and Package Name, and click OK.
**

## App Development
**
Add Required Dependencies:
•Launch Android studio and create a new project. Once the project is ready.
•Navigate to the Gradle scripts folder and open build.gradle (module: app).Add following dependency for HMS Cloud DB Kits.
```
implementation 'com.huawei.agconnect:agconnect-cloud-database:1.5.0.300'
    implementation "com.huawei.agconnect:agconnect-auth-huawei:1.6.0.300"
    implementation 'com.huawei.agconnect:agconnect-auth:1.5.0.300'
```

•Navigate to the Gradle scripts folder and open build.gradle (project: app).
```
 ext.kotlin_version = "1.4.21"
    repositories {
        google()
        jcenter()
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.huawei.agconnect:agcp:1.4.2.300'
```
 
**

## Code Implementation
** 
•Created following package model, clouddb, viewmodel.
Model: In your primary folder, create a new package and name it model. 
CloudInfo.java:
```
package com.hms.corrierapp.clouddb;

import com.huawei.agconnect.cloud.database.CloudDBZoneObject;
import com.huawei.agconnect.cloud.database.annotations.DefaultValue;
import com.huawei.agconnect.cloud.database.annotations.EntireEncrypted;
import com.huawei.agconnect.cloud.database.annotations.Indexes;
import com.huawei.agconnect.cloud.database.annotations.NotNull;
import com.huawei.agconnect.cloud.database.annotations.PrimaryKeys;

@PrimaryKeys({"CourierID"})
@Indexes({"ID:CourierID"})
public final class CourierInfo extends CloudDBZoneObject {
    private Integer CourierID;

    @NotNull
    @DefaultValue(stringValue = "Courier Name")
    private String CourierName;

    @DefaultValue(stringValue = "Desc")
    private String Desc;

    @EntireEncrypted(isEncrypted = true)
    private String FromAdress;

    @EntireEncrypted(isEncrypted = true)
    private String ToAddress;

    public CourierInfo() {
        super(CourierInfo.class);
        this.CourierName = "Courier Name";
        this.Desc = "Desc";
    }

    public Integer getCourierID() {
        return CourierID;
    }

    public void setCourierID(Integer CourierID) {
        this.CourierID = CourierID;
    }

    public String getCourierName() {
        return CourierName;
    }

    public void setCourierName(String CourierName) {
        this.CourierName = CourierName;
    }

    public String getDesc() {
        return Desc;
    }

    public void setDesc(String Desc) {
        this.Desc = Desc;
    }

    public String getFromAdress() {
        return FromAdress;
    }

    public void setFromAdress(String FromAdress) {
        this.FromAdress = FromAdress;
    }

    public String getToAddress() {
        return ToAddress;
    }

    public void setToAddress(String ToAddress) {
        this.ToAddress = ToAddress;
    }

}


```

ObjectTypeInfoHelper.java:
```
package com.hms.corrierapp.clouddb;

import com.huawei.agconnect.cloud.database.CloudDBZoneObject;
import com.huawei.agconnect.cloud.database.ObjectTypeInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class ObjectTypeInfoHelper {
    private static final int FORMAT_VERSION = 2;
    private static final int OBJECT_TYPE_VERSION = 1;

    public static ObjectTypeInfo getObjectTypeInfo() {
        ObjectTypeInfo objectTypeInfo = new ObjectTypeInfo();
        objectTypeInfo.setFormatVersion(FORMAT_VERSION);
        objectTypeInfo.setObjectTypeVersion(OBJECT_TYPE_VERSION);
        List<Class<? extends CloudDBZoneObject>> objectTypeList = new ArrayList<>();
        Collections.addAll(objectTypeList, CourierInfo.class);
        objectTypeInfo.setObjectTypes(objectTypeList);
        return objectTypeInfo;
    }
}


```

**

## App Build Result
**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ui4rkb5o1y1b6zyws0i1.gif)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89y65c0ynagxhqc62ly8.gif)
  
**

## 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.
Make sure you have added SHA-256 fingerprint without fail.
Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
**

## Conclusion
**
In this article, we have learned how to integrate Huawei ID, Push and Cloud DB Kit in Android application. After completely read this article user can easily implement Huawei ID and Client Side Push Notification and submit courier information on Cloud DB in the Courier android application using Kotlin.
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.
**

## References
**
HMS Docs: 
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050048870
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/service-introduction-0000001050040060
https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-clouddb-introduction-0000001054212760
Cloud DB Kit Training Video:
https://developer.huawei.com/consumer/en/training/course/video/101628259491513909