How to target multiple architectures using NDK?

android developer picture android developer · Jan 20, 2013 · Viewed 18.9k times · Source

Background

I've recently started to develop some code using the NDK, and I've thought of a possible portability problem that could occur while developing using NDK.

The problem

Since NDK uses native code, it needs to be compiled per CPU architecture. This is a problem since the user needs to run the app no matter what CPU the device has.

Possible solutions I've found so far

I've noticed I can modify the file "jni/Application.mk" and use:

APP_ABI := armeabi armeabi-v7a x86

however, I don't know what I should do from this step on. Will the app contain all of the compiled code for each of the CPU architectures and automatically choose the correct one when running itself?

Also, what if there will be another CPU architecture that is unknown?

What will happen if I try to run the app on Google TV, which according to what I remember doesn't support the NDK at all?

Another solution I've found is the multi-apk support. However, I'm not sure I understand it. Does it all mean that you create the same APK, each time with a different configuration? No special automation tool from ADT to help with that?

Answer

Alex Cohn picture Alex Cohn · Jan 20, 2013

If you don't set APP_ABI at all, or use

APP_ABI := all

in your Application.mk, then ndk-build will build for all architectures supported by your version of NDK. The latest one, to date r8d, will in addition to armeabi armeabi-v7a x86 build also for mips. When another architecture will be released, you will hopefully automatically get the APK built to support it.

When you application is installed on an Android device, the system will automatically choose the matching ABI version and install the correct shared libraries from the APK.

One drawback of this approach is that if the native libraries are big, your "monolithic" APK file may become huge. You can use the multi-APK approach to make user downloads smaller. The official site recommends: You should generally use multiple APKs to support different device configurations only when your APK is too large (greater than 50MB). You should carefully follow the version code guildlines if you choose this route.

Unfortunately, there are no trustworthy prophecies regarding NDK support on Google TV, but there seem to be no technical justification for its current unavailability. If and when it arrives, your ndk-build will take care of it automatically.

UPDATE Here is a simple process to maintain split APK. And by the way, the new Android TV does support NDK.