MobDev.TfliteDownloader (mob_dev v0.7.33)

Copy Markdown View Source

Downloads and caches pre-built TensorFlow Lite native libraries for Android and iOS so Mob apps can ship the TFLite Nx backend without building TFLite from source (Bazel).

Two upstream sources:

  • Android — tensorflow-lite-2.16.1.aar from Maven Central (the last release with a packed .aar; 2.17.0 ships .jar only). Extracted to jni/arm64-v8a/libtensorflowlite_jni.so + headers/tensorflow/lite/c/c_api.h etc.
  • iOS — TensorFlowLiteC-2.17.0.tar.gz from dl.google.com (CocoaPods upstream). Contains three xcframeworks (Core, CoreML, Metal) each with ios-arm64 device + ios-arm64_x86_64-simulator slices.

The C API is binary-compatible between these versions — TFLite's c_api.h surface has been stable since 2.13. Different version pins per platform reflect upstream packaging differences, not API drift.

Mirrors the MobDev.MLXDownloader pattern: pinned URL + cached extraction at ~/.mob/cache/tflite-<version>-<target>/, validated against the expected layout. Reused across projects. The AAR, the iOS tarball and the two headers patched into the Android tree are checked against their SHA-256 pins in MobDev.Download.Pins (the downloaded AAR/tarball again on every reuse), and an extracted cache is reused only if it came from the pinned archive.

Used by MobDev.NativeBuild when the project enables TFLite via mix mob.enable tflite. The build template sources TFLITE_DIR from dir/1 and links the tflite_nif.c NIF against the headers and framework / .so it provides.

Local-build override

Set MOB_TFLITE_LOCAL_TARBALL_DIR=/path/to/dir to bypass the upstream download and use tensorflow-lite-<android_version>.aar / TensorFlowLiteC-<ios_version>.tar.gz from that directory. Useful when iterating offline or against a custom TFLite build. A local archive is your own choice, so it is not held to the pin; its hash is recorded instead, and the cache it produced is replaced once the override is unset.

Summary

Types

Target slice this downloader supports.

Functions

Android TFLite version pin.

Cached TFLite root directory for target. May not exist if ensure/1 hasn't been called.

Every URL this downloader can request.

Ensure the TFLite bundle for target is cached and extracted.

iOS TFLite version pin.

Returns true if the cache directory has the expected layout for the given target. Public for tests and NativeBuild probing.

Types

target()

@type target() :: :android_arm64 | :android_arm32 | :ios_device | :ios_sim

Target slice this downloader supports.

Functions

android_version()

@spec android_version() :: String.t()

Android TFLite version pin.

dir(target)

@spec dir(target()) :: String.t()

Cached TFLite root directory for target. May not exist if ensure/1 hasn't been called.

download_urls()

@spec download_urls() :: [String.t()]

Every URL this downloader can request.

ensure(target)

@spec ensure(target()) :: {:ok, String.t()} | {:error, term()}

Ensure the TFLite bundle for target is cached and extracted.

Returns {:ok, path} where path is the unpacked root containing the expected layout (see valid_dir?/2).

ios_version()

@spec ios_version() :: String.t()

iOS TFLite version pin.

valid_dir?(target, dir)

@spec valid_dir?(target(), String.t()) :: boolean()

Returns true if the cache directory has the expected layout for the given target. Public for tests and NativeBuild probing.