MuntashirAkon/AppManager · error · IllegalArgumentException

Cannot create template for network type

Error message

Cannot create template for network type ${networkType}'.

What it means

NetworkStatsManagerCompat.createTemplate maps a NetworkCapabilities transport constant to a NetworkTemplate. When the networkType is not one of the handled transports (TRANSPORT_CELLULAR, TRANSPORT_WIFI, etc.), it throws IllegalArgumentException with this message, since no matching template can be built.

Solutions

  1. Only pass TRANSPORT_WIFI or TRANSPORT_CELLULAR (the handled types) to template().
  2. Add a case for the transport you need, or build a fallback template (e.g. buildTemplateWifiWildcard).
  3. Catch IllegalArgumentException and skip stats collection for that transport type.
  4. Clamp/whitelist the networkType int before the call.

Example fix

// before
NetworkTemplate t = NetworkStatsManagerCompat.template(networkType, subId);
// after
NetworkTemplate t;
if (networkType == NetworkCapabilities.TRANSPORT_WIFI
        || networkType == NetworkCapabilities.TRANSPORT_CELLULAR) {
    t = NetworkStatsManagerCompat.template(networkType, subId);
} else {
    t = NetworkTemplate.buildTemplateWifiWildcard();
}
Defensive patterns

Strategy: validation

Validate before calling

if (networkType != NetworkCapabilities.TRANSPORT_WIFI
        && networkType != NetworkCapabilities.TRANSPORT_CELLULAR) {
    throw new IllegalArgumentException("Unsupported transport: " + networkType);
}

Try / catch

try {
    NetworkTemplate t = NetworkStatsManagerCompat.template(networkType, subId);
} catch (IllegalArgumentException e) {
    NetworkTemplate t = NetworkTemplate.buildTemplateWifiWildcard();
}

Prevention

When it happens

Trigger: Calling template(networkType, subscriberId) with an unhandled transport constant (e.g. TRANSPORT_BLUETOOTH, TRANSPORT_VPN, TRANSPORT_LOWPAN) that the switch statement's default branch reaches.

Common situations: Querying stats for VPN or Bluetooth traffic on newer Android versions; constants added in newer SDK levels but not handled by this compat code; passing a raw int from an unexpected source.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/99655bd8a0359d1a. Report an issue: GitHub.

Appendix: source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/compat/NetworkStatsManagerCompat.java:49

        return networkStats;
    }

    @NonNull
    private static NetworkTemplate createTemplate(int networkType, @Nullable String subscriberId) {
        final NetworkTemplate template;
        switch (networkType) {
            case NetworkCapabilities.TRANSPORT_CELLULAR:
                template = subscriberId == null
                        ? NetworkTemplate.buildTemplateMobileWildcard()
                        : NetworkTemplate.buildTemplateMobileAll(subscriberId);
                break;
            case NetworkCapabilities.TRANSPORT_WIFI:
                template = TextUtils.isEmpty(subscriberId)
                        ? NetworkTemplate.buildTemplateWifiWildcard()
                        : new NetworkTemplate(NetworkTemplate.MATCH_WIFI, subscriberId, null);
                break;
            default:
                throw new IllegalArgumentException("Cannot create template for network type " + networkType + "'.");
        }
        return template;
    }
}

View on GitHub (pinned to 0152f468fc)