It has been mentioned in the article “Youku Hongmeng Development Practice | Hongmeng Card Development” that in order to realize “sliding up on Youku host ICON and exhaling Youku Hongmeng card”, it is necessary to mix and package the card’s implementation code with Youku Host and guest. The following sections briefly describe how to implement the Android/ Hybrid packaging process.

Currently, it is unrealistic to rewrite all large Android applications (figure 1 below) using the Hongmeng API, so Huawei has designed the above evolution path. It is hoped that functions in the App will be gradually replaced by Android modules into Hongmeng FA/PA, mixed and packaged together for distribution (Figure 2 below), and finally reach the final form of 100% Pure Hongmeng (Figure 3 below).

At present, we mix Youku Android main-guest and Hongmenghap into a product, which is the intermediate state of “Smooth evolution and interoperation of Android App” in the picture.

As mentioned earlier, the current Youku Hongmun edition includes the Android APK body, as well as the desktop Widget HAP, the multi-screen interactive HAP.

Therefore, the Hongmeng Version of Youku not only has all the features of the Android version of Youku, but also has some special features that the Android version does not have.

Youku Hongmeng is one of the pioneers to cooperate with Huawei to develop Hongmeng App in the early stage, which solved a large number of practical engineering problems, and solved a large number of development environment and runtime bugs with Huawei, and finally successfully put Youku Hongmeng hybrid package on huawei App Store.

Package solution Battle

The subcontract plan

Put the original Android Apk app and Harmony Hap app on the market separately.

  • Advantages: Apk and Hap can be different package names, separate control version shelves
  • Disadvantages: Users need to download and install it twice at the same time to experience the full function

Mixed package scheme

Mix the original Android Apk and Harmony Hap into an App for the App market.

  • Advantages: Users can download and install the complete function once, and do not have to worry about the differences between two application versions
  • Disadvantages: Packaging production is relatively cumbersome, apK and HAP have the same package name restrictions

In contrast, the subcontracting scheme needs to be installed twice to complete the overall functional experience, which is obviously a hard injury. Although the combined package scheme is troublesome in development and production, it can bring better experience to users, so the mixed package scheme is selected.

Mixed bag

What is a mixed bag?

Android’s product is Apk, Harmony’s product is Hap, and Apk and Hap are mixed into a total product package called hybrid package (APP).

Mixed package usage scenarios

Scenario 1: When the Harmony app is not a complete feature, but merely a complement and enhancement to the existing Android app, apK and HAP need to be packaged as a whole app.

Scenario 2: Android applications cannot all be migrated to Harmony in a short period of time. This solution can be used to mix and package as a running application for Harmony system during the migration process.

Mixed package packaging process

Noun explanation:

  • LegacyApk: ApK product compiled in the original normal Android project and mixed package plug-in, this APK can not be installed and run independently, only used to produce intermediate products of Hongmeng final APP. As shown above, the mixed packaging process is actually the process of packaging legacyApk (a processed product of the original Android Apk) and Hap (a product of the Hongmun App) into an App package (ZIP).

Mixed package requirements and restrictions

Noun explanation:

  • Hongmeng Entry: Application corresponding to Android;
  • Hongmeng feature: corresponds to the Android module or bundle.

Limitations:

  • Hongmeng Process Entry will be the parent container of APK, so it cannot contain any codes and resources. All codes and resources need to be moved to feature.
  • The config.json of Hongmeng Entry and features must be the same as the versionCode versionName of Android.
  • Android applications (legacyApk) must be 64-bit packages and cannot contain 32-bit SO.

Requirements:

  • Hongmeng application must have the same package name as the original Android application;
  • Mixed packaging requires HAP (Android-like AGP) version >= 2.4.2.2, apiVersion (Android-like targetSdkVersion) >=5;
  • Hongmeng application is actually a separate application when it is started. In order to keep consistent with the original Android technology brand, it is necessary to keep the label and icon in entry config.json consistent with the original Android application.

Generate steps

Step 1: Modify the original Android Apk side

In order to intervene the original Apk startup process and increase the startup entrance of Hongmeng application, it is necessary to intervene the Android application. Hongmeng side provides a package-compiled plugin on the engineering side, but because Youku is a component-based development mode and Application exists as a separate AAR module, the plugin for mixed compilation is not applicable.

Solution: By understanding the mixed compiler plugin responsibilities, directly in the application module manually modify the superclass for AceHarmonyApplication (ohos. Ace. Ability. AceHarmonyApplication, This class is provided in abilityshell.jar) and manually added in the manifest for Compatibility properties as follows:

<! Android :name="zidane.software.ability" Android: Required ="false" /> <meta-data android:name="permZA" android:value="true" /> <meta-data android:name="multiFrameworkBundle" android:value="true" />Copy the code

The application module is integrated into the Android project, and the product legacyApk is obtained after compilation.

Step 2: Hongmeng Engineering side modification

1. Set project parameters

Build. gradle: Change the compileSdkVersion and compatibleSdkVersion of build.gradle in entry and all features to 5 (the minimum version supported by mixed packages is 5).

The config. Json:

  • Change compatible and target in the apiVersion of entry and all features in config.json to 5;
  • Add an originalName attribute to entry and all features and keep it consistent with bundleName.
  • Add version to entry and all features and keep the sub-attribute code equal to versionCode in Apk and the sub-attribute equal to versionName in Apk.
  • The entry must be consistent with the vendor in all features. The code and name in config.json have requirements1000000+b1000 + c. For example, Name is 1.001.003 and Code is 1001003.
{" app ":{"bundleName" : "bundleName ", // This is the hongmengapplication package name, The mixed package must be the same as the Apk package name. "Vendor ":" XXX ", and the entry and feature must be the same as "version":{"code": XXX. // The corresponding Android version code must be higher than the Apk version. ApiVersion :{"compatible":5,//5 only supports mixed package "target":5, "releaseType":"Release" } } }Copy the code

2. Add legacyApkOptions to the Entry module

apply plugin: 'com.huawei.ohos.hap' ohos { ... legacyApkOptions { legacyApk".. \.. \legacy_entry. Apk "// Point to the legecyApk file, It must end with Entry. apk. LegacyVersionCode "499" // the same as android:versionCode configured in Androidmanifest.xml of legacyApk LegacyVersionName "9.15.2" // Same as Android :versionName configured in legacyApk's Androidmanifest.xml}}Copy the code

3. Entry modification

Move all the code and resources in entry to feature (Hongmeng Project allows one entry and multiple features); Add an empty Ability page and modify the icon and label to match the original Android app.

Step 3: Sign

Signature is the most troublesome step in mixed packaging, which is also the most special step for Hongmun development. You need to take the original signature file (JKS or P12) and generate CSR with DevEco Studio, and then apply for the signed certificate (CER) file and Profile (P7B) file in Huawei Application Market. More details refer to huawei also help documentation (developer.huawei.com/consumer/cn…

Because the mixed package requires the signature information of APP to be consistent with the signature information of the original Android, the original Android signature file (JKS) is ok under normal circumstances. However, Hongmeng improves the security requirements of signature for security:

  1. The EC algorithm must be used
  2. The key password must contain at least two characters: uppercase letters, digits, and special characters. The length of the password must be greater than 8 characters

If the signature file is created early and the two requirements do not meet, Huawei provides the following solutions:

1. You can use the original Android signature file to configure shell Apk signature separately

apply plugin: 'com.huawei.ohos.hap' ohos { ... legacyApkOptions { ... signConfig { storeFile file(".. \ legacyapkjks.jks ") // Separately configured shell APK signature material}... }}Copy the code

2. Generate p12 files and CSR files using keytool command line, but the alias and key should be the same as the original Android signature, and use EC algorithm

P12 keytool -genkey -alias [keyname] -keystore [p12.file] -storetype pkcs12 -keyalg EC -keypass [password] -storepass [password] // Generate CSR keytool -certreq -alias [keyname] -keystore [p12.file] -storetype pkcs12 -file [csr.file]Copy the code

Apply for down the cer and p7b file (requires a separate application for debugging and issue a certificate), can configure signature file in development tools (read more configuration details also sign configuration documentation developer.harmonyos.com/cn/docs/doc…

Then build -> Build Apps in DevEco Studio to compile the product APP.

Engineering Structure Overview:

Product analysis of mixed package

After the above series of processes, you can generate a hybrid package for Hongmeng to run.

As it is signed by the release certificate, the mixed package cannot be manually installed on Harmony OS. It also needs to be signed by Huawei platform side (contact the interface person of Huawei side) and provide the converted installation package for youku to test.

After the test, the hybrid pack can be uploaded directly to Huawei’s Hongmeng App market.

Summary of experience of hongmeng version

  • Hongmeng Hybrid package is only 64 bit package;
  • In Hongmeng Market, the release rhythm and version number of Hongmeng mixed package should be consistent with the original Android release rhythm and version number, otherwise, special consideration should be given to the data selection of Hongmeng version;
  • Apk and HAP are actually physical differences in app. Apk is not recompiled when the app is packaged, so if apK and HAP have classes with the same name (because they are both Java classes), there will be problems at runtime due to the parent delegate mechanism.
  • If the original Android application contains the address book and call rights, you must select apply for restricted rights when applying for p7B files on Huawei platform, as shown in the following figure:

Finally, next week, in the third technical article of “Youku Hongmeng Development Practice” series, we will take the technical reserve of Youku Playback center as the starting point, combined with the mirror and flow characteristics of Hongmeng system, and introduce in detail the practice of ordinary flow, free perspective and Zoom and other core capabilities in Hongmeng.

Thanks for your attention to [Alibaba Mobile Technology]. See you in the next technical practice.

Follow us every week for 3 mobile technology practices & dry goods for you to think about!