android/ndk

M1 support in ndk-build script

JancyWeng opened this issue · 13 comments

在Mac M1上使用NDK

export NDK_HOME=/Users/xxx/Library/Android/ndk/android-ndk-r22
export PATH=$NDK_HOME:$PATH

然后运行 ndk-build 的时候报错 ERROR: Unknown host CPU architecture: arm64

是否是不支持Mac M1?

hhb commented

M1 native support is not ready yet. See #1299. In the mean time you can try to run ndk-build with rosetta2 like: arch -x86_64 /bin/bash -c ndk-build

error when building with ndkBuild using /Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/Android.mk: Build command failed.
Error while executing process /Users/mac/Library/Android/sdk/ndk/21.4.7075529/ndk-build with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/Android.mk APP_ABI=x86 NDK_ALL_ABIS=x86 NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/ndkBuild/debug/lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
ERROR: Unknown host CPU architecture: arm64
Affected Modules: jnidictionaryv2

@hhb how to resolve this issue.?

Did you try using Rosetta?

no rosetta for android studio.

Rosetta is something you install yourself. It's part of macOS, not Studio.

You will still need rosetta for ndk-build for a little while longer as many of the components listed in #1299 still need fat binaries, but that fix will resolve an ndk-build issue with rosetta. Plan is to have everything in the NDK be fixed to not require rosetta by the time r24 reaches stable (and a lot will be backported to r23).

You will still need rosetta for ndk-build for a little while longer as many of the components listed in #1299 still need fat binaries, but that fix will resolve an ndk-build issue with rosetta. Plan is to have everything in the NDK be fixed to not require rosetta by the time r24 reaches stable (and a lot will be backported to r23).

For the upcoming r24, we currently have these components as universal binaries: LLVM, make, python3, and shader-tools. The NDK package still needs yasm and simpleperf migrated. CMake and Ninja are shipped independently of the NDK, and they're in progress. (I have them building as universal binaries already, but we need to upgrade the CMake version in the SDK.)

qpqg commented

iam Using NDKr23
but i don't know why it redirects aarch64 host into linux-x86 host

su -c $NDK_BUILD NDK_PROJECT_PATH=. \APP_PLATFORM=android-16 APP_BUILD_SCRIPT=./Application.mk 

/data/data/com.termux/files/home/androidtools/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++[1]: syntax error: unexpected '(' /data/data/com.termux/files/home/androidtools/android-ndk-r23/prebuilt/linux-x86_64/bin/python[23]: syntax error: unexpected ')'
/data/data/com.termux/files/home/androidtools/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++[1]: syntax error: unexpected '(' /data/data/com.termux/files/home/androidtools/android-ndk-r23/prebuilt/linux-x86_64/bin/python[23]: syntax error: unexpected ')'
/data/data/com.termux/files/home/androidtools/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++[1]: syntax error: unexpected '(' /data/data/com.termux/files/home/androidtools/android-ndk-r23/prebuilt/linux-x86_64/bin/python[23]: syntax error: unexpected ')'
/data/data/com.termux/files/home/androidtools/android-ndk-r23/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++[1]: syntax error: unexpected '('
/data/data/com.termux/files/home/androidtools/android-ndk-r23/prebuilt/linux-x86_64/bin/python[23]: syntax error: unexpected ')'
/data/data/com.termux/files/home/androidtools/android-ndk-r23/build/core/build-all.mk:94: Android NDK: WARNING: There are no modules to build in this project!

IMG_20220216_195026

Because r23 doesn't have the fix yet. You can figure out what releases are still waiting for fixes by looking at the "projects" section of the bug.

Because r23 doesn't have the fix yet. You can figure out what releases are still waiting for fixes by looking at the "projects" section of the bug.

QiubyZ appears to be running the NDK on an arm64 Android device using Termux, and ndk-build is selecting linux-x86_64 rather than linux-arm64 (which the NDK doesn't officially support). This code in ndk/build/core/init.mk simply assumes an x86 host:

HOST_ARCH := $(strip $(HOST_ARCH))
HOST_ARCH64 :=
ifndef HOST_ARCH
    ifeq ($(HOST_OS_BASE),windows)
        # In the case that we're a 32-bit make (shouldn't be the case, but maybe
        # the user is using their own make binary rather than the NDK's), on a
        # 64-bit OS, PROCESSOR_ARCHITECTURE will be x86 but
        # PROCESSOR_ARCHITEW6432 will be AMD64. If PROCESSOR_ARCHITECTURE is x86
        # and PROCESSOR_ARCHITEW6432 is empty, this is a 32-bit OS.
        # https://blogs.msdn.microsoft.com/david.wang/2006/03/27/howto-detect-process-bitness/
        ifeq ($(PROCESSOR_ARCHITECTURE)$(PROCESSOR_ARCHITEW6432),x86)
            $(call __ndk_error,32-bit Windows is supported.)
        endif
    endif
    HOST_ARCH := x86
    HOST_ARCH64 := x86_64
else
    $(call ndk_log,Host CPU from environment: $(HOST_ARCH))
endif

(Maybe we can remove some more of the 32-bit Windows stuff.)

This GitHub issue is for the M1 macOS hosts, not Android. On macOS, we continue to use binaries from darwin-x86_64, but the arch part of the tag is vestigial because the binaries are actually universal arm64+x86_64 binaries.

Should be fixed in r23 build 8486889.