Friday, February 6, 2015

anil

Optimize Your Android Apps (NDK) in Two Minutes on Intel® Architecture


When you want to develop natively for Android, the NDK is compiling your C++ code into a library that can be integrated in the Java project thanks to the JNI interface. In my engine, the Java code is kept to a bare bones minimum (mostly the communication with the system) and never changes from one game to the other. It’s now possible to create an Android application using only C++ (no Java), but I don’t recommend it because sometimes you’ll want to use third parties Java APIs (to display banner ads for example).
Nowadays, you will find different CPU powering Android devices (ARM, Intel x86…). This does not matter for applications developed in Java because it’s an interpreted language. But when you are developing in C++ like myself, you have to compile your code for specific architectures. The majority of Android devices are powered by ARM chips. Like many C++ developers, I was only targeting ARM architectures (armv5 and armv7). Thanks to the NDK, you can compile your code for different architectures - armv5 for old Android devices and armv7 for newer ones. It’s automatically managed by the system - your APK is containing both versions and when your users launch your application, the most appropriate code will be used. Devices powered with x86 architecture can’t run ARM code obviously (as they are different architectures). By consequence, these devices must interpret ARM code via a virtual machine. Even if the x86 devices are doing an amazing job at interpreting ARM code, it’s a shame to not use their full capacity. If you compile your code for x86, your application will run faster on Intel devices and will consume less battery. Overall, it will be a better experience for your users. It’s very easy to support as you simply need to include a new compilation target. You need to open the jni/Application.mk in an editor and update the APP_ABI variable (it’s the one defining the different targets). As I was only targeting armv5 and armv7 before, my variable was set like this:
APP_ABI := armeabi armeabi-v7a
To add support to Intel architecture, you simply need to add x86 to the list.
 APP_ABI := armeabi armeabi-v7a x86
Now, every time you’ll compile your application, you’ll get an extra target compatible with x86 architecture that will be added to your APK. When the application is installed on an Intel based device, it’s this new compatible code that will be executed. All my games are now fully optimized for x86 devices. If like meyou are developing your applications in C++ with the NDK, you should also target for x86 architecture. It’s so simple, it would be a shame not to take advantage of it.
Personally, I did not face any technical problems supporting this new target as my engine is using standard C++. In fact, I’ve recompile and updated all my games the same day.
You need to know that the APK will be a tiny bit bigger (to store the x86 code) which can be annoying if you are close from the 50MB limit on Google Play.
If you are using  third party APIs only compiled for ARM, you won’t be able to use them anymore. I try to use only external libraries with the source code provided because it’s more cross-platform friendly.
If you need to change something in your make a file depending on the current architecture being compiled, you can edit your Android.mk and add conditions:
if eq ($(TARGET_ARCH_ABI), x86)
‘This code will be visible only when compiling the x86 target.’
I really think that for the vast majority of developers using the NDK like myself (with their own engine or Cocos2D for example), adding the word x86 in the APP_ABI variable will be enough to support Intel based devices. You will agree that this is largely possible within two minutes. You should do it.



About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.