DevTools Logo

Android Network Security Config Builder

Android Network Security Config Builder

Generate network_security_config.xml with cleartext rules, trust anchors, pinning and debug-only CAs — plus the manifest snippet and file checklist.

Runs in your browser
Try an example

Security policy

0 domain rules

The policy is saved in this browser only. Internal host names and pin digests never leave your device.

API 28+ blocks cleartext by default; API 27 and lower allow it. The emitter writes the flag explicitly either way.

Base config

Resource name only, lowercase — the certificate goes in res/raw/<name>.pem.

Domain configs
Debug overrides

Generated files

9 lines · 254 chars
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="false">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>
What each element does
base-config
App-wide defaults. Anything a domain-config does not set is inherited from here; when unset, Android uses platform defaults.
domain-config
Applies only to connections whose host matches one of its <domain> entries. The most specific match wins.
domain
A bare host name. includeSubdomains="true" also matches its subdomains; the default is false, so the attribute is always written explicitly.
trust-anchors
The CAs a configuration trusts. Omit it on a domain-config to inherit the anchors from base-config.
certificates
A CA source: system (pre-installed CAs), user (device-installed CAs), or @raw/name (a PEM/DER file in res/raw).
pin-set
Limits the accepted public keys for this domain to the listed SHA-256 SPKI digests. Keep a second backup pin and an expiration date.
debug-overrides
Extra trust anchors used only when android:debuggable is true. Release builds never see these CAs.
API level and lint notes
  • targetSdk 35: cleartext is blocked by default (API 28+); the explicit cleartextTrafficPermitted attributes above override that default.
  • android:usesCleartextTraffic is deprecated at API 28 and ignored when networkSecurityConfig is present — use this file instead.
  • Certificate pins are SHA-256 hashes of the certificate's SubjectPublicKeyInfo (SPKI), base64-encoded — not hashes of the whole certificate.

Examples

HTTP for a legacy staging host

Input
domain: api.staging.example.com · cleartext: Allow · base: Blocked
Output
<base-config cleartextTrafficPermitted="false"> … </base-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="false">api.staging.example.com</domain>
</domain-config>

Only the named host gains cleartext; production domains keep the app-wide block.

Debug proxy CA

Input
debug overrides: user CA + @raw/proxy_ca
Output
<debug-overrides>
    <trust-anchors>
        <certificates src="user" />
        <certificates src="@raw/proxy_ca" />
    </trust-anchors>
</debug-overrides>

The extra anchors exist only in debuggable builds; release installs never see them.

Pinned API domain

Input
domain: api.example.com · 2 pins · expiration 2027-01-01
Output
<pin-set expiration="2027-01-01">
    <pin digest="SHA-256">7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=</pin>
    <pin digest="SHA-256">fwza0LRMXouZHRC8Ei+4PyuldPDcf3UKgO/04cDM1oE=</pin>
</pin-set>

A backup pin prevents lockout during key rotation; the expiration disables pinning if the app stops updating.

About this tool

The Android Network Security Config Builder writes network_security_config.xml from a form: a base policy, per-domain overrides, optional SHA-256 pin sets, and debug-only trust anchors. It also emits the AndroidManifest.xml attribute and a placement checklist for res/xml and res/raw, so the artifact is ready to drop into an Android project.

The schema is easy to get subtly wrong. This emitter writes cleartextTrafficPermitted explicitly (its implicit default flips at API 28), writes includeSubdomains explicitly (its schema default is false), nests certificates inside trust-anchors, and keeps debug-overrides as a direct child of the root where Android expects it. Test CAs are kept out of base config and rejected if you try to place them there.

Validation covers bare ASCII host names, punycode for internationalized domains, base64 SHA-256 pin digests, ISO pin expiration dates, Android resource names, duplicate or overlapping domains, and the lockout risk of a single pin. Everything runs locally in the browser; internal host names and pins are never uploaded.

What this tool does not do

  • Only network_security_config.xml and its manifest attribute are generated; the tool does not build a full AndroidManifest.xml.
  • Domain configs are flat; nested domain-config inheritance is not modelled.
  • Pins must be pasted as pre-computed base64 SHA-256 SPKI digests — pasting a PEM certificate to compute pins is not supported.
  • The tool does not test connectivity, inspect a certificate chain or verify that a pin matches the deployed server.
  • Certificate Transparency and Encrypted Client Hello policy elements are out of scope.

How to use this tool

  1. Pick a preset or start from the base policy

    Use a preset for a staging cleartext exception, a debug proxy CA, a pinned API host, or a fully locked default.

  2. Set targetSdk and base trust anchors

    Enter the app's targetSdk and choose system, user or custom CA resources for the app-wide base-config.

  3. Add domain configs

    Enter bare host names, decide whether subdomains are included, override cleartext and trust anchors, and optionally add pin sets.

  4. Add debug-only CAs

    Enable debug overrides for test proxies. The tool rejects any attempt to put the same test CA in the release trust path.

  5. Copy the files and follow the checklist

    Copy or download the XML, add the manifest attribute, place any CA files in res/raw, rebuild and reinstall.

Use cases

Fix cleartext traffic errors

Allow HTTP for one legacy staging host instead of weakening the whole app's policy after an API 28 upgrade.

Lock down trust anchors

Restrict the app to system CAs or a private CA in res/raw instead of trusting every pre-installed authority.

Debug with a proxy

Trust a local proxy CA in debug builds only, with a hard check that it never appears in the release trust path.

Certificate pinning

Pin an API host to two SHA-256 SPKI digests and set an expiration so old installs do not break on key rotation.

Policy options and what they emit

Form optionResult
Base cleartext: Blocked/AllowedcleartextTrafficPermitted="false|true" on base-config (always explicit)
System / user / custom CA<certificates src="system|user|@raw/name" /> inside trust-anchors
Include subdomainsincludeSubdomains="true|false" on <domain> (default false, always explicit)
Domain cleartext overridecleartextTrafficPermitted on that domain-config, or inherited when unset
Pins + expiration<pin-set expiration="YYYY-MM-DD"> with one <pin digest="SHA-256"> per line
Debug-only CAs<debug-overrides> at the root, active only when android:debuggable is true

Attribute omission is deliberate: cleartext and includeSubdomains are always written so the policy does not change with platform defaults.

Common mistakes

Mistake:Pasting a URL into the domain field.

Fix:Use a bare host such as api.example.com. Schemes, paths, query strings and wildcards are rejected; use Include subdomains instead.

Mistake:Trusting user-added CAs in the base config to make a debug proxy work.

Fix:Put test CAs under debug-overrides. Base trust anchors apply to release builds too.

Mistake:Assuming includeSubdomains defaults to true.

Fix:The schema default is false. This tool always writes the attribute explicitly so the intent survives.

Mistake:Pinning a single key with no backup.

Fix:Add a second pin from a different key or CA and set an expiration date to avoid bricking old installs.

Mistake:Writing @raw/my_ca.pem as the resource name.

Fix:Enter only my_ca; Android appends @raw/ and expects the file at res/raw/my_ca.pem (or .der).

Frequently asked questions

Why is cleartext traffic blocked since Android 9?

Starting with Android 9 (API 28), cleartext HTTP is disabled by default so apps cannot accidentally send traffic unencrypted. Apps targeting API 27 or lower still allow it by default. A network security config with cleartextTrafficPermitted overrides the platform default for the hosts you choose.

How do I allow HTTP for just one domain?

Leave base-config cleartextTrafficPermitted set to false, then add a domain-config with cleartextTrafficPermitted="true" and a <domain> entry for that bare host. Turn on Include subdomains only if subdomains need the same exception. This tool's 'Allow one staging domain' preset does exactly that.

Should user-added CAs be trusted in release builds?

Usually no. Trusting the user CA store in base-config means any CA installed on a device can intercept your app's TLS traffic in production. Use <debug-overrides> for proxy and test CAs instead; those anchors apply only when android:debuggable is true. The builder rejects an attempt to place a debug CA in the base trust anchors.

Where does the XML file go?

Save it as app/src/main/res/xml/network_security_config.xml, then add android:networkSecurityConfig="@xml/network_security_config" to the <application> tag in AndroidManifest.xml. If you reference custom CAs, put the PEM or DER files in app/src/main/res/raw/<name> without an extension in the src attribute. Rebuild and reinstall — the file is read at build time.

How does pinning handle key rotation?

A pin-set accepts a connection if any listed SHA-256 SPKI digest appears in the chain, so include a backup pin from a second key or CA. Set an expiration date to disable pinning after that date, which prevents old installs from being locked out when you cannot ship an update. A single pin without a backup gets a warning in this tool.

Why is my wildcard or Unicode domain rejected?

The Android schema has no wildcard host syntax; use a domain entry with includeSubdomains="true" instead. Host names are matched as ASCII, so internationalized domains must be entered in punycode (xn--...) form. Schemes, paths and query strings are rejected because <domain> takes a bare host only.

Does includeSubdomains default to true?

No — the schema default is false. This builder always writes includeSubdomains="true" or "false" explicitly, so checking the box cannot silently stop matching subdomains. It also writes cleartextTrafficPermitted explicitly because its implicit default changes at API 28.

References & standards