“Zefengengchen” wechat technology account is now open, in order to get the first-hand technical articles push, welcome to search attention!

preface

The official release of HarmonyOS 2.0 has caused quite a stir among Huawei users and tech enthusiasts alike. How HarmonyOS is compatible with Android apps is one of the most frequently discussed topics, leading to a heated debate on whether or not HarmonyOS is an Android shell.

In the meantime, some developers, sensing the wind, are already working out how to collect apps for compatibility with HarmonyOS. This is not, the author’s company arranged me to carry out relevant technical research. One of the most important things is to determine if the current app is installed on HarmonyOS, so that the number of users upgrading HarmonyOS can be used as a basis for future changes in the company’s strategic direction.

OpenHarmony and HarmonyOS

First of all, let’s make a clear conclusion to the previous argument, namely:

HarmonyOS is not a shell Android.

HarmonyOS, as we usually refer to it, is divided into two parts: OpenHarmony and closed source applications and services, including HMS.

OpenHarmony is an open source project incubated and run by the Open Atoms Open Source Foundation, which is similar in nature and positioning to the Android Open Source Project (AOSP).

HarmonyOS 2.0 is a commercial version of Huawei’s OpenHarmony 2.0 for a variety of full-scene smart devices. HarmonyOS 2.0 complies with AOSP’s open source license, which enables existing Android ecosystem applications to work on some devices running HarmonyOS 2.0.

There are currently two types of applications available for HarmonyOS 2.0, namely

  • Harmonyos-only apps (apps that are developed for HarmonyOS, cannot be installed on other Android phones, are small in size, and have few features)
  • Apps that support HarmonyOS features (Apps developed on Android, but using HarmonyOS features, exported through the tool with APK)

HarmonyOS detection method

Going back to our original requirement, how do we know if an app developed for Android is compatible with HarmonyOS?

My first solution to this problem is:

By reading the Android system configuration, iterate over the properties that can be used as a judgment

This section describes the Android system configuration

In order for all running processes to share the various property values required by the system to run, the Android system creates a property storage area and provides an API to access this area. Attributes consist of a key and a value, both of string type.

Properties are heavily used in the Android system to record system Settings or exchange information between processes. Properties are globally visible throughout the system. Each process can get/set attributes.

We can access property values in the system configuration in two ways:

Adb command line tool

  • Adb Shell GetProp allows you to view all property values on your phone.
  • Abb shell getProp XXX displays the value of a specified property.

SystemProperties class

The Systemproperties class is in the android.os directory, but this class is marked as hidden, and is not directly available for upper-level application development, but we can call it reflectively. Properties can be created/modified using the systemProperty.set (property,value) method, and obtained using the systemProperty.get (property) method.

For example, we can get Settings – about the version number displayed in the phone – this way:

*/ fun getHarmonyOsVersion(): String { return if (isHuaweiBrand() && isHarmonyOs()) { try { val cls = Class.forName("android.os.SystemProperties") val  method = cls.getMethod("get", String::class.java) method.invoke(cls, "ro.huawei.build.display.id") as String } catch (e: Exception) { "-1" } } else { "-1" } }Copy the code

Of course, this version number can be obtained in a more direct way:

*/ fun getHarmonyOsVersion(): String { return if (isHuaweiBrand() && isHarmonyOs()) { Build.DISPLAY } else { "-1" } }Copy the code

Get (property) is implemented by calling systemProperty.get (property). Jni calls native methods to realize property access:

    public static final String DISPLAY = getString("ro.build.display.id");
    
    private static String getString(String property) {
        return SystemProperties.get(property, UNKNOWN);
    }
    
    public static String get(@NonNull String key, @Nullable String def) {
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        return native_get(key, def);
    }
Copy the code

Unfortunately, after iterating through all the values, the os-related values are still related to the Android system. The only one that appears with Harmony is the default ringtone value:

So, want to plan as the basis, we need to read through the default phone ring attribute values and determine whether ends in Harmony, but because at present I have only an upgrade to the HarmonyOS 2.0 mobile phones, in other type of cell phone can’t ensure that the attribute values are consistent, therefore can only keep this plan as a solution.

Another solution:

This is determined by whether calls to the HarmonyOS Java API are supported.

As for the method of calling, as you might guess, it’s still in the form of reflection, to see if you can get the Class object of the Class associated with the API at runtime.

As you can see on the HarmonyOS developer website, the latest HarmonyOS SDK Release is version 2.1.1.21 and API version 5 (as of 2021.6.22).

In the HarmonyOS Java API documentation, however, Since is used to indicate which Version of a package, class, or interface was first supported. For example, “Since: 1” indicates that support began with API Version 1.

So we chose a class with the latest API version 5 as the test object:

The code is simple. We use the forName static method of the Class Class to retrieve the Class object with the full Class name argument. If the Class object is not empty, we can prove that the current application is installed on HarmonyOS:

/** * If Harmony JAVA API can be called */ fun isHarmonyOs(): Boolean { return try { val cls = Class.forName("ohos.utils.system.SystemCapability") cls ! = null } catch (e: Exception) { false } }Copy the code

A little confusion

Why should I choose a class with API version 5? This is because the idea of determining whether an app is running on a HarmonyOS device by whether or not it supports calls to the HarmonyOS Java API was something I originally thought about, but I initially chose the Ability related class that was first introduced on the HarmonyOS developer website. The API version for this class is 1.

Tests have found that Ability Class objects are available on both upgraded HarmonyOS and unupgraded phones.

It wasn’t until I looked up the history notes for HarmonyOS that I noticed this:

Further confirmation was made on the official HarmonyOS developer account, which received the following response:

So it turns out that the idea of determining whether an application is running on a HarmonyOS device by supporting calls to the HarmonyOS Java API is correct, but only if the API version 5 class is used.

Yet another question arises: why can a phone that hasn’t upgraded HarmonyOS call a HarmonyOS Java API with an API version below 5? Are some of the HarmonyOS featurely-related libraries already deployed on the EMUI of existing Huawei phones? There is no reasonable explanation for this problem at present.

conclusion

In order to reduce migration costs for users and protect their existing data, HarmonyOS, for the time being, has chosen to accommodate Android ecosystem applications under an open source AOSP license.

This, along with the fact that huawei’s developer website doesn’t have a clear API, makes it a bit convoluted to determine whether or not an Android-based app should be installed on HarmonyOS.

Currently, the most reliable way to determine this is by supporting calls to the HarmonyOS Java API, and you must select classes with API version 5.

If you have a better idea, let me know in the comments below. Thanks

“Zefengengchen” wechat technology account is now open, in order to get the first-hand technical articles push, welcome to search attention!

reference

What’s the difference between OpenHarmony and HarmonyOS? Developer.huawei.com/consumer/cn…

HarmonyOS Java API reference developer.harmonyos.com/cn/docs/doc…

The Android system properties blog.csdn.net/yangwen123/ SystemProperty analysis…