Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make android:usesCleartextTraffic="true" only for instrumentation tests?

I'm using RESTMock for my instrumentation tests, but it only works if I set usesCleartextTraffic to true in my manifest. I only want that to be true for instrumentation tests, though. Is there a way to do that?

I tried creating a new manifest file in the androidTest folder. The tests run but they fail like usesCleartextTraffic is still false.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package">

    <application android:usesCleartextTraffic="true" />

</manifest>

I know that RESTMock supports https starting from version 0.3.2, but I'd rather not have to deal with it. I actually followed their guide and ended up with this error from OkHttp3:

java.lang.AssertionError: java.security.NoSuchAlgorithmException: The BC provider no longer provides an implementation for KeyPairGenerator.RSA. Please see https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html for more details.

Any ideas?


EDIT:

I followed this answer and moved this manifest I created to the debug source folder and then it worked. Now the android:usesCleartextTraffic="true" option is only applied to my debug build, which is used by the instrumentation tests, so it works, but it still doesn't feel like the proper solution.

like image 934
Fred Porciúncula Avatar asked Dec 16 '18 16:12

Fred Porciúncula


People also ask

What is the use of usesClearTextTraffic in android?

Similar to the App Transport Security (ATS) feature in iOS (see also best practice 6.5 Implement App Transport Security), Android 6.0 and later makes it easier to prevent an app from using cleartext network traffic (e.g., HTTP and FTP without TLS) by adding the android:usesCleartextTraffic attribute to the Android ...

What is android Debuggable true?

By putting android:debuggable="true" in your manifest file, application will go in debug mode, that means android will manage all logs file regarding your application. But make sure put it again false (or remove this tag) if application will going to live or for release mode.

When should Usecleartexttraffic be used?

On Android 9.0 (API level 28) or higher, cleartext support is disabled by default and apps targeting Android 9.0 or higher will need to add the android:usesClearTextTraffic="true" flag in the AndroidManifest. xml file. If you are only working with HTTPS files, this flag is not required.

What is android supportsRtl?

android:supportsRtl="true" enables support for right-to-left languages. Without this, layout will always be left-to-right, However by itself it does not change the layout to be from right to left. It simply enables other attributes - one of those new attributes controls whether is left-to-right or right-to-left.


1 Answers

For me the solution is to add a simple AndroidManifest.xml in androidTest/AndroidManifest.xml. This is also mentioned in the answer you reference, but in that case it didn't work because old tooling didn't merge this AndroidManifest.xml.

So, inside androidTest directory, and next to java directory, I have the following:

~/source/my-library/src/androidTest develop*
❯ ls
AndroidManifest.xml java

With this AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.mypackage.mylibrary">

    <application
        android:usesCleartextTraffic="true" />

</manifest>
like image 182
Xavier Rubio Jansana Avatar answered Sep 25 '22 08:09

Xavier Rubio Jansana