'Unknown host CPU architecture: arm64 , Android NDK SiliconM1 Apple MacBook Pro

I've got a project that is working fine in windows os but when I switched my laptop and opened an existing project in MacBook Pro M1. I'm unable to run an existing android project in MacBook pro M1. first I was getting

Execution failed for task ':app:kaptDevDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution > java.lang.reflect.InvocationTargetException (no error message)

this error was due to the Room database I applied a fix that was adding below library before Room database and also changed my JDK location from file structure from JRE to JDK.

kapt "org.xerial:sqlite-jdbc:3.34.0"

   //Room components
    kapt "org.xerial:sqlite-jdbc:3.34.0"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

after that now I'm getting an issue which is Unknown host CPU architecture: arm64

there is an SDK in my project that is using this below line.

android {
    externalNativeBuild {
        ndkBuild {
           path 'Android.mk'
        }
    }
    ndkVersion '21.4.7075529'


}

App Gradle

 externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.18.1"
            //version "3.10.2"
        }
    }

[CXX1405] 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=arm64-v8a NDK_ALL_ABIS=arm64-v8a NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/cxx/Debug/4k4s2lc6/obj NDK_LIBS_OUT=/Users/mac/Desktop/Consumer-Android/ime/dictionaries/jnidictionaryv2/build/intermediates/cxx/Debug/4k4s2lc6/lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n} ERROR: Unknown host CPU architecture: arm64

which is causing this issue and whenever I comment on this line

path 'Android.mk'

it starts working fine, is there any way around which will help me run this project with this piece of code without getting this NDK issue?

Update - It seems that Room got fixed in the latest updates, Therefore you may consider updating Room to the latest version (2.3.0-alpha01 / 2.4.0-alpha03 or above)

use ndkVersion "24.0.8215888" update ndk to this version and no need to edit any script :)

GitHub Issue Tracker



Solution 1:[1]

solved this issue.

Finder -> Go To Folder(/Users/mac/Library/Android/sdk/ndk/21.4.7075529) -> now edit ndk-build open it in text editor and paste below code script and re-run your project.

from

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

to

#!/bin/sh
DIR="$(cd "$(dirname "$0")" && pwd)"
arch -x86_64 /bin/bash $DIR/build/ndk-build "$@"

Reference Link

Solution 2:[2]

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'

or

echo "y" | sudo ${ANDROID_HOME}/sdk/cmdline-tools/latest/bin/sdkmanager --install 'ndk;24.0.8215888'

Depending what where sdkmanager is located enter image description here

B

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 /bin/bash $DIR/build/ndk-build "$@"

enter image description here

C

convert your ndk-build into a cmake build

Solution 3:[3]

There is a new CMake 3.22.1 package in the SDK Manager's canary channel that has M1 support for macOS.

With this release, and with the latest platform-tools and build-tools packages, it's now possible to build and debug an Android app with a CMake project, on an M1 Mac without Rosetta (but with beta/canary packages). These versions of components are needed:

Android Studio 2021.1.1
Android Gradle Plugin 7.1.0 (the plugin includes an aapt2 binary)
Android SDK Build-Tools 32.1-rc1 (SDK's stable channel)
Android SDK Platform Tools 32.0.0 (SDK's stable channel)
Android NDK 24.0.7956693 (beta 2, from the SDK's beta channel)
CMake 3.22.1 (SDK's canary channel)

Android Studio can be briefly configured to use the canary channel to install just CMake, then switched back to the stable channel, to avoid updating everything else.

The NDK and CMake versions need to be configured in build.gradle, e.g.:

android {
    ndkVersion "24.0.7956693-beta2"
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
}

In NDK r24 beta 2, yasm and simpleperf are still x86_64-only.

Using ndk-build on a non-Rosetta M1 Mac needs a newer NDK r24 build, which has a fix to the makefiles to use a universal Python3 binary. The NDK r24 final release should have this fix.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Libin Mathew