214

I'm starting to work on an app on Android, so I don't have much. All I have is just a WebView so far. I created the project in Android Studio, and my project got set as an Android InstantApp. I'm not sure why/how, but my guess is that I overlooked an option for it when creating the project.

I was getting an error from the WebView saying net::ERR_CLEARTEXT_NOT_PERMITTED. When I googled the error, I saw that when an app is an InstantApp, WebViews can only load sites that are HTTPS, and cannot load an HTTP site.

The purpose of this app is to be an extremely simple Flash player for only one site. This is to have better performance running a game that requires Flash. This game is at darkorbit.com, which is HTTPS.

MainActivity.java:

package com.tylerr147.darkorbit;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = findViewById(R.id.webView1);
        wv.loadUrl("https://darkorbit.com/");
        wv.setWebViewClient(new CustomWebViewClient());
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);

    }
}

and CustomWebViewClient.java

package com.tylerr147.darkorbit;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

My question: How can I disable my app as an InstantApp, or how can I get this WebView to display the site?

I feel like it's important I mention a few other details too: In the app, where it is showing the WebView, it also says "The webpage at http://darkorbit.com/" could not be loaded because: net::ERR_CLEARTEXT_NOT_PERMITTED

Notice that is says "... site at http://darkorbit.com/ ...", and not "... site at https://darkorbit.com/ ..." even though the string for the URL is hardcoded, and says "https://darkorbit.com/". Also, I am testing the app on an emulator set up as a Google Pixel 2 running Android 9.

Any help would be appreciated. Thank you.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
tylerr147
  • 2,581
  • 2
  • 11
  • 16

2 Answers2

525

Solution:

Add the below line in your application tag:

android:usesCleartextTraffic="true"

As shown below:

<application
    ....
    android:usesCleartextTraffic="true"
    ....>

UPDATE: If you have network security config such as: android:networkSecurityConfig="@xml/network_security_config"

No Need to set clear text traffic to true as shown above, instead use the below code:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        ....
        ....
    </domain-config>

    <base-config cleartextTrafficPermitted="false"/>
</network-security-config>  

Set the cleartextTrafficPermitted to true

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • 2
    I also developed a Webview template and got same error. And, now it is solved. thanks – Shani Deshwal Feb 17 '19 at 08:50
  • 15
    this is already there and it's still showing the same error!!!1 – Abraham Mathew Mar 14 '19 at 07:31
  • @ABr can you post you Manifest please? – Ümañg ßürmån Mar 14 '19 at 08:30
  • Why does the issue occur though? Is it supposed to happen for non-secure websites? But I've actually reached one that is supposed to be secure (example: "one.co.il", which has "https://" in the beginning). Even the normal web browser on Android shows me the lock icon. How can I know if the website is secure or not? – android developer Jun 05 '19 at 10:19
  • 7
    Not solve still showing error in android pie – kishan verma Jun 07 '19 at 06:43
  • @Ümañgßürmån I am using phonegap and therefore I don't have AndroidManifest.xml. Where can I add this tag please? – Zuzu JH Jun 21 '19 at 12:37
  • 9
    it does not work with android:networkSecurityConfig set – Maciej Beimcik Jul 15 '19 at 08:31
  • I have made a webview connecting to an https site, and I am getting that error, do you know why? I thought that was only for http requests – Miguel Aug 16 '19 at 10:10
  • @taurelas check this answer to set the `android:networkSecurityConfig` configuration to allow your url: https://stackoverflow.com/a/50834600/7850685 – Luzian Aug 21 '19 at 18:50
  • 4
    @Miguel check if you have a `networkSecurityConfig` in your `AndroidManifest.xml` under ``. If you do, you may either try removing it (if not needed), or change it to allow your domain. This answer may help you too: https://stackoverflow.com/a/50834600/7850685 – Luzian Aug 21 '19 at 18:53
  • 1
    Worked for me as well in a Cordova based project. Changed value in `cordova/platforms/android/app/src/main/AndroidManifest.xml`. My env: Cordova 9.0.0, Platform: Android 8.1.0 – Amil Waduwawara Oct 01 '19 at 03:27
  • 1
    Thanks! Worked for me :) (Android webview with react-native) – Jacob.B Feb 26 '20 at 14:43
  • 1
    Thank you man! android webview with react native handle above solution :) – Michał Feb 27 '20 at 22:08
  • @AbrahamMathew: Did you add it to `application` tag? – testing Feb 12 '21 at 08:58
28

When you call "https://darkorbit.com/" your server figures that it's missing "www" so it redirects the call to "http://www.darkorbit.com/" and then to "https://www.darkorbit.com/", your WebView call is blocked at the first redirection as it's a "http" call. You can call "https://www.darkorbit.com/" instead and it will solve the issue.

Gordon
  • 321
  • 3
  • 4