12

I would like to trust certificate only in one build-variant. How to make it?

2
  • 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? Commented Sep 19, 2017 at 13:44
  • I only want security configuration at in this one variant. Commented Sep 19, 2017 at 18:04

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

5 Comments

To add on to this, the 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.
@CommonsWare - Should the AndroidManifest.xml just contain <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" package="com.xxx"> <application android:networkSecurityConfig="@xml/network_security_config"> </application> </manifest>
@Prashanth That should be sufficient. I do not recall if you need the package attribute or not (since that will be supplied by the merger process).
@CommonsWare - Thank you!! I can just have network_security_config.xml in my main/res/src/xml and my main main AndroidManifest.xml need not contain android:networkSecurityConfig in application tag ? Just let me know if this is right. Thanks
@Prashanth: The question asked here is specifically for not putting these things in main, but instead have it be for a particular build variant.
-1

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

Comments

-1

You can create build_variant folder inside src and your network_config file inside that

below is some sample for different icon for different build_variant. same way we can do for network_config file

/src/build_variant/res/xml/network_security_config.xml

Exmaple

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.