checkNpuStackInManifest function

Future<NpuStackCheck> checkNpuStackInManifest(
  1. NpuStack vendor, {
  2. required NativeAssetsManifestReader read,
  3. bool cache = false,
  4. String? abi,
  5. String? operatingSystem,
})

Whether this build registered vendor's NPU stack on operatingSystem (npuStackLibs).

The authority is NativeAssetsManifest.json, which Flutter regenerates on every build and the engine itself reads at startup to resolve native assets: it lists exactly the assets this build's hooks registered. File presence is not an answer — on Windows stale DLLs outlive a rebuild, and on Android a Play install keeps the libraries in a config split.

read supplies the manifest's text. With cache (the platform host's read: the manifest of the running build), an answer is kept per reader and vendor for the isolate and reused without reading again. Only a successful read is cached. A failed read is not an answer about the build either, so it is NpuStackCheck.unknown, not "not enabled".

Implementation

Future<NpuStackCheck> checkNpuStackInManifest(
  NpuStack vendor, {
  required NativeAssetsManifestReader read,
  bool cache = false,
  String? abi,
  String? operatingSystem,
}) async {
  final stack = npuStackLibs(
    vendor,
    operatingSystem ?? Platform.operatingSystem,
  );
  if (cache) {
    final hit = _cached[(read, vendor)];
    if (hit != null) return hit;
  }
  final String text;
  try {
    text = await read();
  } on Object catch (e) {
    return NpuStackCheck.unknown(
      'NativeAssetsManifest.json could not be read ($e), so whether this '
      'build bundles the ${vendor.label} NPU stack is unknown',
    );
  }
  final NpuStackCheck result;
  try {
    final assets =
        ((jsonDecode(text) as Map)['native-assets'] as Map?)?[abi ??
                Abi.current().toString()]
            as Map?;
    final missing = [
      for (final name in stack)
        if (!(assets?.containsKey(nativeAssetId(name)) ?? false))
          androidLibFileName(name),
    ];
    result = missing.isEmpty
        ? const NpuStackCheck.bundled()
        : missing.length == stack.length
        ? NpuStackCheck.missing(npuNotBundledReason(vendor))
        : NpuStackCheck.missing(
            'this build bundles only part of the ${vendor.label} NPU stack '
            '(missing ${missing.join(', ')})',
          );
  } on Object catch (e) {
    return NpuStackCheck.unknown(
      'NativeAssetsManifest.json is not in a format this version understands '
      '($e), so whether this build bundles the ${vendor.label} NPU stack is '
      'unknown',
    );
  }
  if (cache) _cached[(read, vendor)] = result;
  return result;
}