Hasan A Yousef
Hasan A Yousef

Reputation: 24948

Cross compile to native android

How can I cross compile from my MacOS to Android native binary.

I read this, that I've to do:

export NDK_ROOT=~/dev/ndk-toolchain
./android-ndk-r10c/build/tools/make-standalone-toolchain.sh --platform=android-16 --install-dir=$NDK_ROOT

export NDK_CC=~/dev/ndk-toolchain/bin/arm-linux-androideabi-gcc
CC_FOR_TARGET=$NDK_CC GOOS=android GOARCH=arm GOARM=7 ./make.bash

export NDK_TOOLCHAIN=~/dev/ndk-toolchain
export CC=$NDK_TOOLCHAIN/bin/arm-linux-androideabi-gcc
export GOROOT=~/dev/go
export GOPATH=`pwd`
export GOOS=android
export GOARCH=arm
export GOARM=7
export CGO_ENABLED=1

GO="$GOROOT/bin/go"

$GO build -x main.go

I installed NDK from Android Studio, but could not find the mentioned path /dev/ndk-toolchain/bin/arm-linux-androideabi-gcc

In my device I've:

$ cd /Users/hajsf/Library/Android/sdk/ndk/22.1.7171670/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
$ ls
arm-linux-androideabi   bin         lib         share
$ cd bin
$ ls
arm-linux-androideabi-addr2line arm-linux-androideabi-elfedit   arm-linux-androideabi-nm    arm-linux-androideabi-size
arm-linux-androideabi-ar    arm-linux-androideabi-gprof arm-linux-androideabi-objcopy   arm-linux-androideabi-strings
arm-linux-androideabi-as    arm-linux-androideabi-ld    arm-linux-androideabi-objdump   arm-linux-androideabi-strip
arm-linux-androideabi-c++filt   arm-linux-androideabi-ld.bfd    arm-linux-androideabi-ranlib
arm-linux-androideabi-dwp   arm-linux-androideabi-ld.gold   arm-linux-androideabi-readelf
$ 

Upvotes: 1

Views: 2016

Answers (2)

Hasan A Yousef
Hasan A Yousef

Reputation: 24948

I downloaded NDK, extracted it, and found the correct path as:

/Users/hajsf/Downloads/android-ndk-r21e/toolchains/llvm/prebuilt/darwin-x86_64/bin

And was able to compile as:

$ export NDK=~/Downloads/android-ndk-r21e
$ CGO_ENABLED=1 \
GOOS=android \
GOARCH=arm64 \
CC_FOR_TARGET=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang

$ GO build -x main.go

enter image description here

In this, there is this line, but it gave me an error, and witout it, I was able to compile correctly

./all.bash

Upvotes: 1

Hasan A Yousef
Hasan A Yousef

Reputation: 24948

Not sure if this is correct or no, I tried the below and it worked with me the way I want. In my mac:

$ GOOS=android GOARCH=arm64 GO build -x main.go

Then I saved the file at GDrive, then from my mobile, I downloaded it, then shared it with the Tramux and got it saved at folder ~/Downloads I gone there and made it executable as:

$ chmod +x server

Then run it as:

$ ./server

And got it working with me!

Upvotes: 1

Related Questions