Friday, January 25, 2013

AIR Android Native Extensions: Part 5: Exporting xLarge and xHDPI resources with the AIR application

In my previous post, I explained how to use 3rd party JARs in your app. In this post, I am going to cover how to bundle xLarge resources and xHDPI assets with the AIR app.

On the higher version of Android tablets, you may not be able to get the correct UI by using standard sizes and may need to use xLarge and xHDPI values. However, when you put a xHDPI asset or xLarge resource in your res folder, you'd be able to package the ANE but when you compile the final AIR application, it would throw errors saying that these are not supported.

Reason: AIR, by default uses Android 2.2 SDK and xLarge and xHDPI were introduced in Android SDK 3.2. So, the Android SDK 2.2 doesn't recognize them.

Solution: We'd need to compile the app with an Android SDK on and above 3.2. Question is How?

There are 2 ways to update the Android SDK uses to compile AIR:
  • Using the -platformsdk compiler argument
  • Replacing the Andoid JAR in the AIR SDK

Well, sadly it is not an either/or solution. You may need to follow both the steps for different problems. For packaging xHDPI and xLarge resources, you'd need to follow step 1.

Firstly, make sure you have the Android SDK folder on your machine in the following structure:

  - android_sdk_folder
      - platform
      - platform-tools
      - tools
      - add-ons
      - AVD Manager
      - SDK Manager

Now ensure that inside the android_sdk_folder/platforms folder, you have the correct platform available i.e. if you want to use Android 4.0, there should be a folder named android-15 inside the platforms folder. You can Google for the Android SDK to API version mapping.

So, now that you have the correct SDK, what do you specify in the platformsdk? Suppose the android_sdk_folder, I spoke about is in root of d:/. The path that would go in platformsdk is d:/android_sdk_folder. Read this carefully:, you would not give the path to d:/android_sdk_folder/platform/android-15, but only till the d:/android_sdk_folder. Now, you must be wondering, how AIR would know which Android SDK to use, since there may be multiple SDKs inside the platforms folder. This is where your application's app.xml would come into play.

Open your application's app XML i.e. myproject-app.xml. You'd need to add the following line to this file:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

This implies that the minimum API version that the app supports is "8" i.e. Android 2.2 but you want to use API version 15 i.e. Android 4 to package the build. Even after adding this line, you can be rest assured that your app would work in the older android versions as well.

Your App XML's section would look something like follows:

<android>
<colorDepth>16bit</colorDepth>
<manifestAdditions><![CDATA[
<manifest android:installLocation="auto">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application android:enabled="true"></application>
</manifest>
]]>
</manifestAdditions>
</android>

Now, this is how, Android identifies which version of android SDK to use. So, that's it, now you'd be successfully be able to compile your AIR app with xLarge and xDPI resources.

We did not discuss the usage of Step 2 i.e. Replacing the Andoid JAR in the AIR SDK. I have covered it in the next post of the series: Preventing Android activity from being recreated on orientation change and relaunch.

No comments:

Post a Comment