HarmonyOS App development, real-world testing, and the craters

Author: Han Ru

Company: Procedural Coffee (Beijing) Technology Co., LTD

Procedure: IT professional skills evaluation platform

Website: www.chengxuka.com

Watch the Release of The HarmonyOS on June 2nd, excited and eager. I have a Huawei phone and on June 3rd, I backed up data in the morning, upgraded HarmonyOS in the afternoon, and started working on the app in the afternoon. I had a lot of hiccups and had to vomit blood at one point. Today I will sort these out, record, and share with you.

A, the premise

If you want to run a HarmonyOS app on a real machine, you first need to set up the HarmonyOS development environment, as mentioned in the previous article.

  • Set up HarmonyOS’s development environment
  • A HarmonyOS application is created on the DevEco Studio development tool

Generate key and certificate request files

2.1 Generating a Key

First, on the DevEco Studio development tool, select Build–>Generate Key and CSR.

2, we first generate the Key, which is the Key file. Note that the suffix of the key file must be.p12 and the storage location must be selected.

  • Key Store Path: Select the storage Path of the Key Store file.
  • Password: specifies the Password of the key store. The Password must contain at least eight characters, including uppercase letters, lowercase letters, digits, and special characters. Remember the password, which is required for subsequent signature configuration.
  • Confirm Password: enter the key store Password again.

2.2 Generating a Certificate Request File

1. Follow up and continue filling in the information.

  • Alias: indicates the Alias of the key, which is used to identify the key. Keep this alias in mind for subsequent signature configuration.

  • Password: indicates the Password of the key. It must be the same as the Password of the key store. You do not need to enter it manually.

  • Validity: indicates the Validity period of the certificate. You are advised to set the Validity period to 25 years or longer, which covers the entire application life cycle.

  • Certificate: Enter the basic information of the Certificate, such as the organization, city or region, and country code.

2. Then generate the certificate request file, which is the CSR file. Note that the suffix of the key file must be. CSR. Select a proper storage location.

3. We will then generate two files in the directory we just specified. One is the key file demo1.p12 and the certificate request file demo1.csr.

Apply for certificates and profiles

We need to open the AppGallery Connect website, which requires login.

3.1 Applying for an Application Debugging Certificate

After login, select user and access.

In the navigation tree, choose Certificate Management. On the certificate management page, click Add Certificate.

In the New Certificate dialog box that is displayed, enter the certificate information and click Submit.

Then, on the Certificate Management page, we can see the debugging certificate we just added. The certificate name, certificate type, and expiration date are also shown. We need to download this certificate and go local.

  • Click Download to download the certificate.
  • To revoke the certificate, click “Revoke” and click “Confirm” in the confirmation box.

3.2 Registering and Debugging devices

Again, we just got to the page, so let’s go to Device Management. On the device management page, click Add Device in the upper right corner.

In the pop-up window, fill in the device information and click “Submit”.

After the device is added, the device management page is displayed. To delete a debugging device, click Delete in the Operation column.

Step in a big hole

I used to get the UDID of my huawei Mate30 phone. I just upgraded HarmonyOS 2.0 and according to the official document, I just couldn’t get it. I connected to Mac, I used Windows, I went back and forth, and I had to contact a Huawei engineer to get the UDID.

I’m going to do Mac first, Windows down here.

UDID: indicates the unique identifier of a device. It is a 64-character string consisting of letters and digits. Its UDID can be obtained on the corresponding debug device, which requires that the HarmonyOS system be running.

The principle is to obtain the UDID of the device according to the HDC command in ToolChains.

The first thing you need to know is where your SDK is installed. If you don’t know, go to DevEco Studio and open the Settings page for the HarmonyOS SDK, which we used to configure the environment variables.

In fact, the main use of here toolchains HDC command.

So we need to configure environment variables.

1. Open the terminal under the Mac and run the following command to open the bash_profile file.

sudo vim ~/.bash_profile
Copy the code

2. Enter I to enter vim editing mode:

3. Then we enter the following data:

export PATH=/Users/hanru/Library/Huawei/Sdk/toolchains:$PATH
Copy the code

4. Then press ESC to exit editing mode. Enter :wq and press enter to save the configuration and exit. Run vim. bash_profile to check whether the profile is saved successfully.

5. Enter source ~/.bash_profile for the configuration to take effect.

6. Then enter HDC help. The HDC command is available.

Here we finished configuration, and then your Huawei phone connected to the computer. Select Transfer file in the window that is displayed.

On the pit:

Make sure USB debugging is allowed here. Otherwise the command will not read the device later. error: no devices/emulators found

Then at the terminal we type:

hdc shell
Copy the code

Input:

bm get --udid
Copy the code

You can see the UDID of the phone.

The Windows version:

Toolchains = toolchains = toolchains = toolchains = toolchains = toolchains

Then configure the environment variables. This is easy. The same way you configure Java, Golang, etc. Right click on my computer and select Properties. Then select advanced system Settings — select advanced — Environment variables — System variables –Path– click edit, and then the HDC. Exe toolchains directory, configure to the Path Path.

Then open Windows +R, enter CMD, and press Enter to open a command line window, type:

hdc help
Copy the code

Then type:

hdc shell
Copy the code

Input:

bm get --udid
Copy the code

You can see the UDID of the device.

Remember, as above, to make sure the phone is connected to the computer and USB debugging is allowed. Otherwise, the device cannot be read. error: no devices/emulators found

3.3 access Profile

1. We first create a project.

Then choose my project:

Add a project:

Fill in project name:

2. Create an application under this project.

Fill in the application information:

  • Select the platform: Select the APP (HarmonyOS APP).
  • Supported device: Select the device type to be debugged.
  • Application package name: Must be the same as the value of bundleName in the config.json file.
  • Set the application name, application category, and default language as required.

3. Choose HarmonyOS Application > HAP Provision Profile Management. On the HAP Provision Profile Management page, click Add in the upper right corner.

In the HarmonyAppProvision Information window that pops up, add a debug Profile and fill in the following information:

After the Profile application is successful, the Profile name, Profile type, added certificate, and expiration date are displayed on the Management HAP Provision Profile page. Here, we have to download it.

  • Click “Download” to download the Profile.
  • To delete a Profile, click “Delete” and click “Confirm” in the confirmation box.
  • Click View Device to view the debugging device bound to the Profile.

At this point, we have the application debug certificate: rubyHanmate30.cer, and the Profile: HelloWorlddebug.p7b.

Four, signature

Next, we need to sign. Open Project Structure under File.

To sign:

After signing, you can view the signature information in the build.gradle file:

Five, run,

Then run the program.

We can see that we have an App on our phone.

Click in to see how it works:

Sixth, the principle of

The integrity of the HarmonyOS application is secured through digital certificates (.cer files) and HarmonyAppProvision files (.p7b files). The key files (.p12 files) and certificate request files (.csr files) are generated by DevEco Studio. You can also use command line tools to generate key files and certificate request files.

The basic concept

  • Key: contains the public and private keys used in asymmetric encryption and is stored in the key store file in the.p12 format. The public key is used for content encryption, private key for decryption; In the digital signature process, the private key is used for digital signature and the public key is used for decryption.
  • Certificate Request file: The format is. CSR, and the name is Cerificate Signing Request. It contains the public key and public name of the key pair, organization name, and organization unit, and is used to apply for a digital certificate from AppGallery Connect.
  • Digital certificate: The certificate format is. Cer and is issued by Huawei AppGallery Connect.
  • HarmonyAppProvision file: The.p7B format contains the package name for the HarmonyOS application, digital certificate information, a list describing the certificate permissions that the application is allowed to apply for, and a list of devices that the Application is allowed to debug (empty if the application is Release). Each application package must contain a HarmonyAppProvision file.

So, we used Mr. DevEco Studio to create the key file: DevEco Studio generated

In the second step, we generated the certificate request file: DevEco Studio generated

In the third step, we obtain the digital certificate for application debugging: it is issued by Huawei AppGallery Connect.

Step 4: We obtained the Profile (HarmonyAppProvision file) : generated and downloaded from Huawei AppGallery Connect.

Step five, at the end of the preparation, then go to the program to sign, and then run.

If we want to execute the second application on the phone, we need to retrieve the Profile again. In other wordsAppGallery ConnectAdd a new app to the website, add a new Profile, and download it. Then sign the program and run it.