Search code examples
androidandroid-ndkapple-m1apple-silicon

Android NDK Apple SiliconM1 run into: Unknown host CPU architecture arm64


There are two kinds to build Android with NDK

cmake

externalNativeBuild {
    cmake {
        path "../sharedCode/CMakeLists.txt"
    }
}

This works fine with Apple Silicon M1 👍

ndk-build

externalNativeBuild {
    ndkBuild {
        path "src/main/jni/Android.mk"
    }
}

On a Apple Silicon M1 I run into

Unknown host CPU architecture arm64

The question is to solve this ?


Solution

  • To solve this on a Apple Silicon M1 I found three options

    A

    Use NDK 24

    android {
        ndkVersion "24.0.8215888"
        ...
    }
    

    You can install it with

    echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install 'ndk;24.0.8215888' 1>/dev/null
    

    B

    convert your ndk-build into a cmake build

    C

    Change your ndk-build to use Rosetta x86. Search for your installed ndk with

    find ~ -name ndk-build 2>/dev/null
    

    eg

    vi ~/Library/Android/sdk/ndk/22.1.7171670/ndk-build
    

    and change

    DIR="$(cd "$(dirname "$0")" && pwd)"
    $DIR/build/ndk-build "$@"
    

    to

    DIR="$(cd "$(dirname "$0")" && pwd)"
    arch -x86_64 $DIR/build/ndk-build "$@"
    

    D

    Use a x86 Android Studio version. But this is slow

    enter image description here