Home > database >  How I can apply a network_security_config.xml on specific variants and not in release one?
How I can apply a network_security_config.xml on specific variants and not in release one?

Time:03-22

In my build.gradle I have the follwing buildTypes:

buildTypes {
        release {
            //debuggable true
            minifyEnabled false
            signingConfig signingConfigs.config
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "SERVER_DOMAIN", "\"api.example.com\""
        }

        debug {
            minifyEnabled false
            buildConfigField "String", "SERVER_DOMAIN", "\"api.local\""
        }

        staging {
            initWith debug
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "SERVER_DOMAIN", "\"https://api.staging.example.com\""
        }
}

Also the api.local I use self-signed certificates that I configure them into the network-security-config.xml:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <!-- local -->
    <domain-config>
        <domain includeSubdomains="true">api.local</domain>
        <trust-anchors>
            <certificates src="@raw/api"/>
        </trust-anchors>
    </domain-config>
    <!-- /local -->
</network-security-config>

Also, in app/src/main/res/raw I place a file named api.crt where it is the self-signed certificate.

But once wither I build a release or a staging variant I do not want the certificates to be shipped. Is there a way to do so? Also how I can specify per-variant network-security.xml

One way is to use a CI/CD pipeline and delete the certificates whilst I replace the network-security-config.xml with a default one before I run:

./gradlew assembleRelease

Or

./gradlew assembleStaging

But this approach may not scale if app needs to be built in my (coleagues) local machine at any variant. Is there a way to do so?

CodePudding user response:

You can create a different resource for each build type or variant. When adding a new resource, you can select the appropriate source set. For example, in the attached screenshot I am adding an element for the debug build variant. You can add different configs for different variants, different flavors or combination of variant and flavor. So, just add an empty config for the prod/staging and have the debug variant use the network config you want.

enter image description here

What this does is basically add the resource in the appropriate folder in the structure. When building, Gradle will override the more-generic resources with more specific ones (if you have a resource in the main folder and you specify a resource for a flavor with the same name, the flavor resource will override the main one)

CodePudding user response:

For Do such specific changes you have to create the flavour for release, debug and staging and then you can create a different-different folder for each folder whether it would be for resource or java.

Inside that flavour resource folder, you may put the specific file so that changes will affect those flavours only.

Hope you get my point, due to time urgency I couldn't post the example.

  • Related