I would like to trust certificate only in one build-variant. How to make it?
- Do you have a different network security configuration that you want to use in other build variants? Or do you only want network security configuration at all in this one variant?CommonsWare– CommonsWare2017-09-19 13:44:49 +00:00Commented Sep 19, 2017 at 13:44
- I only want security configuration at in this one variant.Mladen Rakonjac– Mladen Rakonjac2017-09-19 18:04:36 +00:00Commented Sep 19, 2017 at 18:04
3 Answers
Create a source for the variant. Create a res/xml/ directory in that source set, and put your network security configuration XML in there. Create other stuff in that source set as needed (e.g., res/raw/ and your certificate). In that source set, create an AndroidManifest.xml file, with an <application> element that just has the android:networkSecurityConfig attribute and nothing else.
And you should be done.
Builds for this variant will include your source set, merge in the android:networkSecurityConfig attribute, and leverage the resources tied to it. Builds for other variants will ignore this source set, leaving you without a network security configuration.
5 Comments
AndroidManifest.xml file is not actually required for each build variant. If your network-security-config file is in the appropriate folder structure as suggested (for example: /app/src/debug/res/xml/network_security_config.xml), then the app will automatically use and apply it.package attribute or not (since that will be supplied by the merger process).main, but instead have it be for a particular build variant.Just create 'n' number of customized network security XML for your 'n' number of build variant, and then you can inject exact network security XML file to your Android Manifest XML depend on the Environment. see here for examples https://developer.android.com/studio/build/manifest-build-variables
