Accessing string resources inside network-security-config
Asked Answered
W

1

22

Is it possible to use string resources inside android:networkSecurityConfig defined in AndroidManifest.xml?

I have a config file like this:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config>
    <domain includeSubdomains="true">mydomain.com</domain>
    <pin-set>
      <pin digest="SHA-256">@string/pin</pin>
      <pin digest="SHA-256">@string/pin_fallback</pin>
    </pin-set>
    <trustkit-config enforcePinning="true" disableDefaultReportUri="true">
    </trustkit-config>
  </domain-config>
</network-security-config>

but my app doesn't seem to detect resources I define in res/values/strings.xml. Hardcoding these strings works just fine though.

Wages answered 2/7, 2018 at 15:40 Comment(0)
J
0

if you are using Gradle Plugin 7.0.0 or later then you can use string interpolation using ${} like this. the ${dynamicVariable} syntax includes the dynamic variable in the PIN value using string interpolation.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config>
    <domain includeSubdomains="true">mydomain.com</domain>
    <pin-set>
      <pin digest="SHA-256">@{getString(R.string.pin)}${dynamicVariable}</pin>
      <pin digest="SHA-256">@{getString(R.string.pin_fallback)}${dynamicVariable}</pin>
    </pin-set>
    <trustkit-config enforcePinning="true" disableDefaultReportUri="true">
    </trustkit-config>
  </domain-config>
</network-security-config>

Note: if you are using Gradle Plugin having less then 7.0.0 then it is not possible to use dynamic accessing string resources

Jett answered 13/5, 2023 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.