3

I develop cordova cross platform app using HTML5 & Javascript. But recently I encountered some problems with android.

Previously I was using Cordova 7 where android version was 6.3 Which supports android API 16 to 26. But as per new google rule, from August 2019, All app must be targeted Android API 28 which is Android 8 & Android 9.

Cordova's latest version is Cordova 9 which supports Android 8. Cordova did not release any version which supports Android 9.

So to develop App targeting API 28, I've to use Android 8. But I'm facing some problems with the app. Previously my app was working fine. When I develop app with Android 8 with Cordova 9, it is working fine with all phones which are Android 8 and below but showing error in Android 9. Specially with AJAX requests.

I'm sending AJAX requests to a server with http (not https). In some blog I see it might cause error. But I don't see any way to fix and currently I've no option to use https.

Another way is to set <preference name="android-targetSdkVersion" value="26" /> which works fine in all android version (6~9) but when I upload with this config in Google Play store, it shows warning to update to target API to 28.

So for now what should I do?

Thanks in advance.

N.B: I found a way to use http but did not understand it at all. If anyone can explain I'll be glad. Link given below: https://developer.android.com/training/articles/security-config

Ajax error in Cordova App after upgrade phone to Android 9 Pie API 28 Revision 6

Razin Abid
  • 349
  • 3
  • 15

3 Answers3

6

All you have to do is include on AndroidManifest.xml the following key-pair-value on the application tag:

android:usesCleartextTraffic="true"

On API 28 this attribute is set false for default(for security reasons), in previous Android API it was true.

Stefano Boeri
  • 61
  • 1
  • 2
4

After a long searching I found out that "By Default", Android 9 do not allow to send request to http. It only allows to send requests to https urls. So all I had to do is to upgrade my server from http to https. No worry, its not that hard. If you use plesk for your server management, its like 2 clicks to install "Lets Encrypt" SSL certificate for FREE forever with auto renewal. It also shows Greenbar in Mozilla Firefox too. However if you use other than Plesk, then go to Lets Encrypt website and generate one and install it to your server. Thats all.

You still can make requests to http urls in Android 9 but still its a bit too complex for me to understand so I say pass. Also upgrading from http to https is giving me more security :)

Razin Abid
  • 349
  • 3
  • 15
1

The responses provided by Razin Abid and Stefano Boeri are both excelent answers, but for someone looking how to enable not secure http coonections in Android 9, plese check this answer: How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?.

Please note that if you are working with Cordova, it is not a good idea to modify the AndroidManifest.xml (Android platform) file directly, but edit config.xml (project configuration file) instead.

You will have 2 tools to achieve it: config-file and edit-config, described with details in the Cordova Docs.

codewario
  • 19,553
  • 20
  • 90
  • 159
ragan
  • 203
  • 2
  • 11