Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network Security Config in androidTest gets ignored

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?

like image 729
Nick Cardoso Avatar asked Apr 11 '26 17:04

Nick Cardoso


1 Answers

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.

like image 62
Martin Zeitler Avatar answered Apr 13 '26 06:04

Martin Zeitler