rust-mobile / android-rs-glue

Glue between Rust and Android
Apache License 2.0
905 stars 111 forks source link

Failing to load assets #241

Closed derezzedex closed 5 years ago

derezzedex commented 5 years ago

By modifying the examples/use_assets to use a folder test inside assets results in:

09-02 15:02:22.786 25082 25098 D RustAndroidGlueStdouterr: thread '<unnamed>' panicked at 'Can`t load asset 'test/test_asset'', src\fs.rs:24:19

Files: fs.rs (not modified)

use std::path::{Path};
use std::io::{Cursor};

#[cfg(not(target_os = "android"))]
pub fn load<P: AsRef<Path>>(path: P) -> Cursor<Vec<u8>> {
    use std::fs::{File};
    use std::io::{Read};

    let mut buf = Vec::new();
    let fullpath = &Path::new("assets").join(&path);
    let mut file = File::open(&fullpath).unwrap();
    file.read_to_end(&mut buf).unwrap();
    Cursor::new(buf)
}

#[cfg(target_os = "android")]
pub fn load<P: AsRef<Path>>(path: P) -> Cursor<Vec<u8>> {
    use android_glue;

    let filename = path.as_ref().to_str()
        .expect("Can`t convert Path to &str");
    match android_glue::load_asset(filename) {
        Ok(buf) => Cursor::new(buf),
        Err(_) => panic!("Can`t load asset '{}'", filename),
    }
}

main.rs

mod fs;

use std::io::BufRead;
use std::path::Path;

fn main() {
    let path = Path::new("test").join("test_asset");
    let f = fs::load(path);
    for line in f.lines() {
        println!("{:?}", line);
    }
    loop {}
}

Assets folder

> tree assets /f
<...>\ANDROID-RS-GLUE\EXAMPLES\USE_ASSETS\ASSETS
└───test
        test_asset
philip-alldredge commented 5 years ago

This appears to be caused by https://issuetracker.google.com/issues/120436372

I just created a pull request which uses aapt to create the apk which will fixes the issue. Previously, we currently use a mixture of aapt and aapt2.