2

I run Rust (toolchain stable-x86_64-unknown-linux-gnu) on Linux local machine. My library builds and run fine with my Android app. But I can't run any unit tests if I specify i686-linux-android target.

I can reproduce it on the empty project.

I created the new rust project

cargo new android_test --lib

Set Android NDK ar and linker via /android_test/.cargo/config

[target.i686-linux-android]
ar = "ndk/x86/bin/i686-linux-android-ar"
linker = "ndk/x86/bin/i686-linux-android-clang"

Simple test will success.

cargo test

But it will fail if I set the target triple.

cargo test --target i686-linux-android

Running target/i686-linux-android/debug/deps/android_test-a71bf7c418798cd7 error: could not execute process /home/zellius/Projects/android_test/target/i686-linux-android/debug/deps/android_test-a71bf7c418798cd7 (never executed) Caused by: No such file or directory (os error 2)

Unit tests will run and pass if I push android_test-a71bf7c418798cd7 file to my Android emulator manually via adb.

I tried to create custom runner. But result is the same.

android_runner.sh

#!/bin/sh

set -e
adb push "$1" "/data/local/tmp/$1"
adb shell "/data/local/tmp/$1"

/android_test/.cargo/config

[target.i686-linux-android]
ar = "ndk/x86/bin/i686-linux-android-ar"
linker = "ndk/x86/bin/i686-linux-android-clang"
runner = ["android_runner.sh"]

Am I missing something? Can I use cargo test to run tests on the emulator? Or should I just use a script for this purpose?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Zellius
  • 256
  • 1
  • 9
  • Does is work with x86_64-linux-android? For me it's not possible to run i686-linux-android on x86_64 host natively neither. – Tim Diekmann Dec 17 '18 at 11:52
  • Well, I'm not sure about that. But after I found this [answer](https://stackoverflow.com/a/44953448/1013428) I started thinking that it is possible to use cargo test for that. Especially for CI use cases. Typical cargo build works fine and output *.so library works with my Android app on x86 emulator. – Zellius Dec 17 '18 at 12:19
  • Using an emulator for a x86 target requires hardware support. At least on travis-ci it's not working. – Tim Diekmann Dec 17 '18 at 12:23
  • Even libc [does not test](https://github.com/rust-lang/libc/blob/ed8309bc03ba32c9f407d1800ad6d51ade19cdb0/.travis.yml#L34) *i686-linux-android* because of this. See [#826](https://github.com/rust-lang/libc/issues/826) and [#825](https://github.com/rust-lang/libc/pull/825) for more info on that. Esp. [this linked comment](https://github.com/rust-lang/libc/pull/825#issuecomment-340078268) in the former. – Tim Diekmann Dec 17 '18 at 12:25
  • I didn't know about that. Thanks! But for now I'm trying to run test on my local x86 emulator. And I found my silly mistake :-/ – Zellius Dec 17 '18 at 13:53

1 Answers1

0

As usually mistake was small and silly.

In my case I should fix path to the runner script.

My project structure:

  • /.cargo

    • config
  • ndk

  • android_runner.sh

  • other files and dirs

/android_test/.cargo/config

[target.i686-linux-android]
ar = "./ndk/x86/bin/i686-linux-android-ar"
linker = "./ndk/x86/bin/i686-linux-android-clang"
# change android_runner.sh to ./android_runner.sh
runner = ["./android_runner.sh"]

Also I fix the script. Now I extract binary file name from the path (e.g. /home/zellius/Projects/android_test/target/i686-linux-android/debug/deps/android_test-a71bf7c418798cd7) and push it somewhere on the device. Without that adb will try to push it to the much longer directory.

/data/local/tmp//home/zellius/Projects/android_test/target/i686-linux-android/debug/deps/android_test-a71bf7c418798cd7*

/android_test/android_runner.sh

#!/bin/sh

test_file_name=$(basename -- "$1")
adb push "$1" "/data/local/tmp/$test_file_name"
adb shell "/data/local/tmp/$test_file_name"

Now I can run test on my local x86 emulator via cargo test --target i686-linux-android.

Maybe there is a better way to do this but for now it works for my case.

Zellius
  • 256
  • 1
  • 9