Search code examples
androidnativeandroid-8.0-oreoarm64

Can ARM64 run ARM assembly in compatibility mode?


It's a 3rd party library and I do not have access to it's source code - asking them to build for ARM64 had no effect so far, despite they claim Android 8.0 (which is ARM64 only) would be "supported".

The following Java code runs on armv7l, but fails on aarch64:

static {
    switch(System.getProperty("os.arch")){
        case "aarch64":
        case "armv7l":
            try {
               System.loadLibrary("somelibrary");
            } catch(UnsatisfiedLinkError e) {
                Log.e(LOG_TAG, e.getMessage());
            }
            break;
    }
}

dlopen failed: "/data/app/ ... /base.apk!/lib/arm64-v8a/somelibrary.so" is 32-bit instead of 64-bit.

It seems, as if the AArch32 execution would be "optional" (and possibly not even available) - because one would (most likely) have to switch the CPU from AArch64 to AArch32 execution. eg. this slide-show covers the topic: Linux on AArch64 ARM 64-bit Architecture.

Q: Is there a way to use a 32bit library on a 64bit Android device?


As it turned out, this is not accepted by Google Play - providing ARM64 native assembly is required.


Solution

  • Any arm64 CPU you find in a smartphone should also be able to run arm32 code.

    There are, however, at least three requirements that need to be fulfilled:

    1. Your process cannot mix 64-bit and 32-bit code -- it needs to be one or the other.
    2. Your operating system (or you) must provide 32-bit versions of all the dynamic libraries you link to.
    3. Your OS kernel must have support for 32-bit processes; it will be responsible for switching the execution mode while your process is using the CPU.

    Number 3 is fine; Android uses a Linux-like kernel, which supports arm32 processes on arm64.

    Number 2 is probably fine; I don't know where you heard that Android 8.0 is 64-bit only, but it's not true. It is possible for a device manufacturer to choose to exclude 32-bit support, but I don't know any that do (because it would prevent 32-bit-only legacy apps and games from running on their devices).

    Of course, shipping both 32-bit and 64-bit libraries on phones means that the OS takes up more storage space, so it's definitely possible that 64-bit-only devices will be a thing in the future. No 32-bit-only games or apps will work on those devices.

    Number 1 is your problem: your app is running as a 64-bit process, and is trying to load a 32-bit library. This will not work.

    The solution would be to make your app 32-bit-only. If you can avoid it all, though, I would recommend against it (i.e. don't use that library). Your app would not work on any 64-bit-only devices that might be released in the future.

    2023 update

    It is now the future!

    Google's Pixel 7 series come without OS support for 32-bit applications.

    Also, starting this or next year, new ARM CPUs will not have 32-bit support at all.

    we are announcing that all Arm Cortex-A CPU mobile cores will be 64-bit only from 2023.