Function zng_env::android_install_res

source ·
pub fn android_install_res<Asset: Read>(
    open_res: impl FnOnce() -> Option<Asset>,
)
Expand description

Helper function for adapting Android assets to the cross-platform res API.

To implement Android resource extraction, bundle the resources in a tar that is itself bundled in assets/res.tar inside the APK. On startup, call this function, it handles resources extraction and versioning.

§Examples

#[no_mangle]
fn android_main(app: zng::view_process::default::android::AndroidApp) {
    zng::env::init!();
    zng::view_process::default::android::init_android_app(app.clone());
    zng::env::android_install_res(|| app.asset_manager().open(c"res.tar"));
    // zng::view_process::default::run_same_process(..);
}

The open_res closure is only called if this is the first instance of the current app version on the device, or if the user cleared all app data.

The resources are installed in the res directory, if the tar archive has only a root dir named res it is stripped. This function assumes that it is the only app component that writes to this directory.

Note that the tar file is not compressed, because the APK already compresses it. The cargo zng res tool .zr-apk tar resources by default, simply place the resources in /assets/res/.