I'm trying to add Rust code to an android NDK sample (native-activity); Whenever I link Rust code (compiled as a .a) into the .so , it fails to run.
I went on information from here to get an android aware rust compiler and 'standalone toolchain' https://github.com/mozilla/rust/wiki/Doc-building-for-android
Someone was suggesting on the Rust IRC channel that I need to mention 'thumb' somewhere; is there an option to pass to rustc, or did I have to build it all differently in the first place?
I've certainly been able to call rust & c from eachother on desktop builds.
Is there a sample where someone has rust code working in a .apk; it would have to be simple.. its very hard to decipher makefiles for complex projects. (I think it would be very hard to figure this out from reading Servo source)
I've verified this process generates a useable package without adding rust; it only fails to run if I link unstripped rust code - even if it isn't reached (even though the linking doesn't tell me anything is wrong). If i remove the call to 'rusty_android()', it works. If i move the call to rusty_android to a point that isn't reached, it still doesn't work. I can verify with 'nm' that 'rusty_android' is in the .so ... unmangled.
This is my current build process: its taken from the android native-activity sample; i've added a rust source file with a single extern functin returning a value, and tried to debug print that from the samples' C main
ANDROID_CXX = /opt/ndk_standalone/bin/arm-linux-androideabi-gcc
android:
$(ANDROID_CXX) -I/home/ME/android-ndk-r9b/sources/android/native_app_glue -c jni/main.c -o obj/local/armeabi/objs/native-activity/main.o
rustc --target=arm-linux-androideabi hello_android.rs -C android-cross-path=/opt/ndk_standalone --crate-type=staticlib -o rusty_android.a
$(ANDROID_CXX) -shared -o libs/armeabi/libnative-activity.so obj/local/armeabi/objs/native-activity/main.o obj/local/armeabi/libandroid_native_app_glue.a another.o rusty_android.a -llog -landroid -lEGL -lGLESv1_CM
ant debug
adb install -r bin/NativeActivity-debug.apk
adb shell am start -n com.example.native_activity/android.app.NativeActivity
hello_android.rs:-
use std::libc;
#[no_mangle]
pub extern fn rusty_android()->libc::c_int {
99 as libc::c_int
}
in main.c, inserted in android_main(..):-
{ extern int rusty_android(); LOGI("****rust says %d****", rusty_android()); }
The suspicion that something to specificy 'thumb' might be needed arose from comparing LLVM IR and asm sources, but it seems strange that it would be an additional option required over and above 'arm-linux-androideabi'
NativeActivity
, but I've got a working Android Studio NDK project that builds and links against a pre-built rust shared library and uses a C++ shim for interfacing between Java and Rust. I've just now updated the Rust Wiki with these changes, and wouldn't mind sharing the project if you're still in need... – Renovate