10

I'm trying to build a simple HelloWorld application and run it on Genymotion. My building system is Ubuntu 14.04 64bit. I have Android SDK(r22.6.2) and NDK(r9d) installed.

First Attempt: Build using the prebuilt NDK ARM toolchain

TOOLCHAIN = $NDK/toolchains/arm-linux-androideabi-4.8
$TOOLCHAIN/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc --sysroot=$PLATFORM hello.c -o hello
adb push hello /sdcard/
./hello

I get an error

/system/bin/sh: ./Hello: not executable: magic 7F45

After doing some research, I quickly realize Genymotion has x86 arch instead of ARM

Second Attempt: Build using x86 Standalone ToolChain

  1. Make x86 standlone toolchain first

    cd $NDK/build/tools
    ./make-standalone-toolchain.sh --arch=x86 --platform=android-19 --install-dir=/tmp/android-toolchain --ndk-dir=../../. --system=linux-x86_64
    
  2. Build Hello using the toolchain

    cd tmp/android-toolchain/bin
    ./i686-linux-android-gcc -o Hello -c hello.c
    
  3. Run Hello on Genymotion

    adb push hello /data/local/tmp
    adb shell
    cd /data/local/tmp
    ./hello
    /system/bin/sh: ./Hello: not executable: magic 7F45
    

Ran uname -a on the genymotion machine. Android System Name Ran file Hello on the executable Hello File info

So it should be a match.

Still getting the same error. Would really appreciate it if someone could shed some light.

Edit 1: Tried running the executable in /data/local/tmp instead of /sdcard/. Still gives the same error.

Edit 2: Ran uname on genymotion system and file on the executable to see if they match.

HBZ
  • 499
  • 4
  • 11
  • I believe there is a confusion: you push `hello` to `/sdcard`, but run `Hello` from `/` (root directory). At any rate, Android mounts the /sdcard and other external storage as non-executable, for security reasons. Therefore, if you want to push and run your executable, use some other directory. `/data/local/tmp` is usually a good choice. – Alex Cohn Oct 23 '15 at 15:20
  • I just tried running the executable in /data/local/tmp. Still gives me the same error – HBZ Oct 23 '15 at 15:37
  • Did you run `chmod 700 /data/local/tmp/hello`? – Alex Cohn Oct 23 '15 at 16:47
  • Yup, already gave the file permission to be executable. – HBZ Oct 23 '15 at 20:51
  • Have you tried using [agcc](http://plausible.org/andy/agcc) script? – t0mm13b Oct 23 '15 at 20:58
  • I think the agcc script is targeted towards ARM architecture, but not x86? – HBZ Oct 24 '15 at 05:15
  • Have you tried plain GCC? – Shark Nov 02 '15 at 14:12
  • when your module is buid call the 'file' command on it, you shoud see it is x86 otherwise it means that you build has something wrong.... – OznOg Nov 02 '15 at 19:36
  • See my edit 2. I checked the file and it seems that the executable matches the system. – HBZ Nov 03 '15 at 19:09
  • And yes i tried plain GCC. It will build a x86_64 version since my system is linux 64 bit – HBZ Nov 03 '15 at 19:10

1 Answers1

1

If plain GCC doesn't get your job done, you'll have to take the harder route.

You need a cross-toolchain; I suggest generating a Canadian cross toolchain using Crosstool~ng; you can use NDK's standalone toolchain as a starting point.

Shark
  • 6,513
  • 3
  • 28
  • 50