Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target multiple architectures using NDK?

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?

like image 231
android developer Avatar asked Jan 20 '13 00:01

android developer


2 Answers

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.

like image 83
Alex Cohn Avatar answered Oct 09 '22 01:10

Alex Cohn


For the latest version (now r9) you have to specify in "jni/Application.mk"

APP_ABI := all 

or

APP_ABI := armeabi armeabi-v7a x86 mips 

without ./ndk_build will only build 'armeabi'

like image 45
hannes ach Avatar answered Oct 09 '22 01:10

hannes ach