-1

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?

2 Answers 2

0

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)

2
  • So for specific flavours I'll need to create the folders src/debug/res and src/staging/res where inside the raw and xml folders where the network-security-config.xml and the certificates will reside. Right? Mar 22, 2022 at 10:43
  • 1
    Correct. Specify a default one in the main resources, and for every flavor/build type you want to override, add a specific resource folder. Use the IDE to avoid mistakes in the file structure
    – deluxe1
    Mar 22, 2022 at 10:44
0

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.

1
  • Well I'll love an example either as a link OR if you update the post and place it later. Mar 22, 2022 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.