The level is limited, if there are problems welcome to visit my blog criticism.

Ionic apps are compatible with Android9 experience

Android P is Android 9.

Suddenly, the App developed for users cannot be used normally on Android9 and Android10, or even cannot be installed on Android10, so we have to pull the code from a long time ago for troubleshooting.

The first thing THAT comes to my mind is that the API above is changed in Android9. If the permissions are easy to handle, if the API is related to third-party cordova plug-ins, it will be a bit of trouble. We can’t wait for the plugin author to update it, we have to rely on the source code Modification. Just like I was looking for a cordova plugin that supports Chinese TTS, almost all of it is in English. Finally, you can only package your own (TTS plug-in), so if you know the principle of Cordova and some Java knowledge, the second point is not difficult.

Fortunately, our problem this time is in their own body, is the kind of easy to solve, we now add android platform source code inside the change.

Problem solving

The Apache HTTP Client class is lost

When you upgrade compileSdkVersion to 28(Android9), if you use Apache HTTP Client related classes in your project, you will throw an error that they cannot be found. If you still need to use Apache HTTP Client, you can add it to the androidmanifest.xml file:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>
Copy the code

Go to the application node for details.

The network is disconnected.

CLEARTEXT communication to life.115.com not permitted by network security policy

Cause: Android P will restrict the plaintext traffic network requests, non-encrypted traffic requests will be blocked by the system

Plan a

Since we are not allowed to use plaintext, we can change HTTP to HTTPS to solve the problem, and this is the future trend, the general trend.

Scheme 2

Given that some requests are too cumbersome to deal with customers, there are other solutions.

Create a new file network_security_config. XML (which also exists) and add or modify the existing content:

All let go

<? xml version="1.0" encoding="utf-8"? > <network-security-config> <base-config cleartextTrafficPermitted="true" />
</network-security-config>
Copy the code

Or permitted parts:

<? xml version="1.0" encoding="utf-8"? > <network-security-config> <! --<base-config cleartextTrafficPermitted="true" />-->
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">sample.domain.com</domain>
    </domain-config>
</network-security-config>
Copy the code

Then add the following to the androidmanifest.xml application node attribute:

<application
	...
    android:networkSecurityConfig="@xml/network_security_config">
Copy the code

Other schemes:

  1. Don’t use too high targetversion, 26 and older versions will still work.

  2. In the addition of AndroidMainifest android: usesCleartextTraffic

<application
    android:usesCleartextTraffic="true"/>
Copy the code

Android 9 uses the foreground service to report an exception

** Cause :** After Android P, FOREGROUND_SERVICE must be granted permission to use foreground services, otherwise an exception will be thrown.

Add permissions to androidmanifest.xml manifest:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Copy the code

At this point, my problems with Android P have been solved, and I’ll come back to Android 10 later, but I’ll list some of the other problems that people have raised online during the process.

Bangs screen Adaptation (native code)

For example, when we need full screen display, Google has already done so in API28. Such as the following code:

requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Android P fringe screen adaptationif(build.version.sdk_int >= build.version_codes.p) { 28 the following complains WindowManager. LayoutParams lp = getWindow (). The getAttributes (); lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; getWindow().setAttributes(lp); }Copy the code

LayoutInDisplayCutoutMode values:

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT: By default, the holed area is not used for full-screen Windows, but can be used for non-full-screen Windows.
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS: The window declaration uses the hole area
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES: The window declaration uses the hole area
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER: Window declaration does not use the hole area

Limits the reception of static broadcasts

After upgrading Android P, implicit broadcasting will be completely disabled, and receivers registered in AndroidManifest will not work if you have one of the following listeners in your manifest file:

<receiver android:name="com.yanghaoyi.receiver.UpdateReceiver">
        <intent-filter>
            <action android:name="com.yanghaoyi.action.ACTION_UPDATE" />
        </intent-filter>
</receiver>
Copy the code

You need to remove the above code and dynamically register it in your app, for example:

private void registerReceiver(){
    myReceiver = new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TOAST_ACTION);
    registerReceiver(myReceiver, intentFilter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(myReceiver);
}
Copy the code

Non-full-screen transparent Activity Disable setting orientation

java.lang.IllegalStateException: Only fullscreen activities can request orientation

You cannot set the direction of a page that is not full-screen transparent. Otherwise, an exception will be thrown.

An Activity is not "fullscreen" if its Style meets any of the following three criteria: "windowIsTranslucent" istrue; "WindowIsTranslucent"falseBut for "windowSwipeToDismiss"true; "For" windowIsFloatingtrue;Copy the code

Solution: android: windowIsTranslucent set to false.

Ionic compatibility optimization

Ionic’s config.xml comes in handy when I add XML to open plaintext access, foreground permissions, and Apache HTTP Client class loss. I have to change code every time I add a platform.

Place the network_security_config. XML file mentioned above in the resources/android/ XML directory.

And add attributes to the widget tag in config.xml:

<widget ... xmlns:android="http://schemas.android.com/apk/res/android">... </widget>Copy the code

Add the following to the Config. XML android configuration:

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
        <config-file parent="/manifest" target="app/src/main/AndroidManifest.xml">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
        </config-file>
        <config-file parent="/manifest/application" target="app/src/main/AndroidManifest.xml">
            <uses-library android:name="org.apache.http.legacy" android:required="false" />
        </config-file>
 </platform>
Copy the code

Then you don’t have to worry about adding and rewriting your code every time you remove a platform.

Some problems with Android 10

In the Android 10 on some phones, such as huawei, don’t know the reason why is because the phone setting their own or android upgrade, not allowed to install an app, so we send the beta (debug) version of the apk installation will fail, because sometimes it’s not that we are the whole development of the market, customers may need to experience or ahead of schedule See the effect. The closing Settings of each mobile phone are different, and huawei’s is not found anyway, so we should officially sign our app when we release it, so as to prevent customer installation failure.

And the signature is also a necessary process for the official release of our APP.

Please refer to my Android Signature article for details about the signature process.

INSTALL_FAILED_NO_MATCHING_ABIS(VIRTUAL machine problem)

Installation failed with message failed to Finalize session: Installation failed with Message failed to Finalize session: INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.

Build. Gradle file in Android Studio (model: app) : Add the following to Android:

android{
    ...
    splits {
        abi {
            enable true
            reset()
            include 'x86'.'armeabi-v7a'.'x86_64'
            universalApk true}}}Copy the code