Network Security Config in androidTest gets ignored
Asked Answered
U

1

11

My app has free and paid flavours. Right now we are adding tests the to paid flavour that run in a mock server, we need to communicate with it in cleartext so are trying to add a network security config only in androidTestPaid (we don't have one in the main source root). Unfortunately the config seems to be completely ignored.

androidTestPaid/AndroidManifest.xml

<manifest ...>
    <application
        tools:node="merge"
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"
        ... />
    </application>
    <instrumentation android:name="...CustomTestRunner" ... />
</manifest>

androidTestPaid/res/xml/network_security_config.xml

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

In our gradle files we do not do anything with source sets for flavour-specific android test, but we do for the base android test. Could that lead to problems?

build.gradle

android {
    sourceSets {
         androidTest {
            java.srcDirs = ['src/androidTest/java', 'src/testShared/java']
            testOptions {
                animationsDisabled = true
            }
         }
         ... 
     }
     ...
}

What would stop this configuration from being picked up in our tests?

Upkeep answered 15/6, 2020 at 10:24 Comment(0)
A
1

Directory androidTest belongs to to the test-app, which means it will end up in the wrong package. Adding them eg. to the debug source-set would cause them to end up in the actual app package under test.

One can define which build-type to build for testing in the AndroidManifest.xml.
The documentation suggests build-type staging; it should be debuggable true.

android.defaultConfig.testBuildType "debug"

And this would be the resulting paths, where network_security_config.xml would be picked up:

debug/res/xml/network_security_config.xml
debugFree/res/xml/network_security_config.xml
debugPaid/res/xml/network_security_config.xml

While one cannot use the local loopback 127.0.0.1, because the emulator has it's own sub-net. I can only hint for, that this is quite pointless, while I'm sure this had already been answered before.

Always answered 25/6 at 14:17 Comment(3)
AndroidManifest and extra XMLs being ignored when merging manifests is what got me. Placing it inside debug as you suggested works, although non-intuitive IMO.Murder
127.0.0.1 can't be used as loopback indeed. The correct one would be 10.0.0.2 as described in developer.android.com/studio/run/emulator-networkingMurder
@Murder You could as well define source-set testing, which inherits from debug. It's just important that it's debuggable true and is not being called test, which causes it to be be packaged into the test-app. This all is quite flexible and even scriptable.Always

© 2022 - 2024 — McMap. All rights reserved.