Showing posts with label http. Show all posts
Showing posts with label http. Show all posts

Cleartext Http Traffic not Permitted Android 9

Android Studio Error Log: java.io.IOException: Cleartext HTTP traffic to my_IP/domain not permitted

2019-09-06 12:20:29.050 22531-22531/com.geekscompete.gate_isro_cs.ugc_net_preparation E/VolleyError:: ::com.android.volley.NoConnectionError: java.io.IOException: Cleartext HTTP traffic to http://geekscompete.com/ not permitted

Reason for this error:
According to Network security configuration - Opt out of cleartext traffic
Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default. Network security configuration also suggest to ensure that all connections to your domain are always done over HTTPS to protect sensitive traffic from hostile networks.

You have to create a new file in your xml folder, file named network_security_config.xml.
res/xml/network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">yourdomain.com</domain>
        <domain includeSubdomains="true">geekscompete.com</domain>
        <domain includeSubdomains="true">geekscompete.blogspot.com</domain>
    </domain-config>
</network-security-config>

This config will now allow clear traffic to your specified domains after you . Make sure that you have set android:networkSecurityConfig="@xml/network_security_config" in the application tag of your AndroidManifest.xml. The content of your config file should contain all URLs which requests clear traffic without encryption. As shown in below code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.yourappname">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".MainApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:allowBackup="false"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme">
    </application>
</manifest>


Related Articles: 


java.lang.NoClassDefFoundError Failed resolution of: Lorg/apache/http/ProtocolVersion Android (9) Pie

 I built App on Android studio targetSdkVersion 28 which targets Android P. The APK can be build ,but when I used it on device with OS Android (9) Pie, it throws below crash and see below detailed crash log for more details.

ANDROID CRASHLYTICS ERROR LOG:
Fatal Exception: java.lang.NoClassDefFoundError
Failed resolution of: Lorg/apache/http/ProtocolVersion;
com.google.android.gms.common.data.zac.b (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.data.zab.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.internal.AccountAccessor.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.ads.internal.util.aq.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):15)
com.google.android.gms.common.api.internal.TaskUtil.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.common.api.internal.TaskUtil.run (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):8)
Caused by java.lang.ClassNotFoundException
Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/00000055/AdsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000055/AdsDynamite.apk!/lib/arm64-v8a, /system/lib64]]
dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:169)
java.lang.ClassLoader.loadClass (ClassLoader.java:379)
com.bumptech.glide.load.engine.EngineKeyFactory.loadClass (:com.google.android.gms.dynamite_dynamiteloader@14799081@14.7.99 (100400-223214910):4)
java.lang.ClassLoader.loadClass (ClassLoader.java:312)
com.google.android.gms.common.data.zac.b (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.data.zab.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.internal.AccountAccessor.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.ads.internal.util.aq.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):15)
com.google.android.gms.common.api.internal.TaskUtil.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.common.api.internal.TaskUtil.run (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):8)

SOLUTION:
This link android-9.0-changes-28-->Apache HTTP client deprecation explains reason for adding the following to your AndroidManifest.xml:
<uses-library android:name="org.apache.http.legacy" android:required="false"/>

Similar errors : Need to specify requirement for Apache HTTP Legacy library
You can also face same error while using play-services-maps library. There are two solutions as below:

Solution 1: If you are working with com.google.android.gms:play-services-maps:16.0.0 or below then update the com.google.android.gms:play-services-maps library to the latest version. This is handled for you if you are using com.google.android.gms:play-services-maps:16.1.0 and is not necessary if your app is targeting a lower API level.
Or
Solution  2:  Include the below declaration within the <application> element of AndroidManifest.xml.
<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
Refer the same included in the official docs: Specify requirement for Apache HTTP Legacy library
It's reported on issuetracker (Android bug tracker) :https://issuetracker.google.com/issues/79478779

More about Apache HTTP client deprecation:
With Android 6.0, we removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default. To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their AndroidManifest.xml:
<uses-library android:name="org.apache.http.legacy" android:required="false">

Things to Note
 The android:required="false" attribute is required for apps that have a minimum SDK of 23 or lower, because on devices with API levels lower than 24, the org.apache.http.legacy library is not available. (On those devices, the Apache HTTP classes are available on the bootclasspath.) As an alternative to using the runtime Apache library, apps can bundle their own version of the org.apache.http library in their APK. If you do this, you must repackage the library (with a utility like Jar Jar) to avoid class compatibility issues with the classes provided in the runtime.

Popular Posts