4 October 2011
Familiarity with native extensions in Adobe AIR on Android, Java, and Android Licensing Verification Library (LVL)
Required third-party products
Intermediate
Note: Adobe recommends using the next version of Flash Builder for developing native extensions for Adobe AIR. Sign up now to get access to the prerelease of Flash Builder 4.6.
This tutorial shows you how to use and package the native extension for Android licensing, including tips for securing your application.
The Android Licensing native extension provides AIR application developers access to the Android Market Licensing service. This service helps you enforce licensing policies for paid applications that you publish through Android Market. When using this service, your application can query Android Market at runtime to obtain the licensing status for the current user and then permit or prohibit further use.
Before you begin: verify that your copy of Flash Builder has the Java Development Tools (JDT) installed. You need the JDT to edit Java code in Flash Builder. For details on how to install JDT, follow the steps in Jason's Flex blog post on Java development in Flash Builder.
To use the native extension for Android Licensing, you need to create:
To create the Java project follow these steps:
Note: Using the latest Android API level directory is recommended.
You now need to copy the LVL available with the Google Android SDK to your Java src project.
Go to AIR SDK, and copy the com folder from <air_sdk_dir>\frameworks\projects\air\Licensing\Android\AndroidLicensingJavaProject\src inside the src folder of your Java project.
At this point your Java project should resemble Figure 4.
After setting up your Java project for the native extension, you need to create an ActionScript library that enables the communication between Java code and your AIR application on runtime.
Later, you will be referencing this ActionScript library in your AIR application.
You also need to set your developer public key in your Flex Library Project which is required for communication between your AIR application and the Android Market server.
Sign in your Google Android Market Portal, copy the public key and set it to the BASE64_PUBLIC_KEY variable in the LicenseChecker.as file:
// Add the Public key obtained from Android Market here
private static const BASE64_PUBLIC_KEY:String = "";
Next you create the AIR app that will use Android Licensing via the native extension that you've just created.
Enter a project name type; I used AndroidLicensingUsage (see Figure 7).
Be sure not to use the same project name as this ID is already used in Google Android Market.
var lc:LicenseChecker = new LicenseChecker();
lc.addEventListener(ErrorEvent.ERROR,errorHandler);
lc.addEventListener(LicenseStatusEvent.STATUS,licenseResult);
lc.checkLicense();
import com.adobe.air.sampleextensions.android.licensing.*;
checkLicense method call. Add the following function:protected function licenseResult(event:LicenseStatusEvent):void{
lbl.text = "status: " + event.status + " statusReason: +" + event.statusReason;
if (event.status == LicenseStatus.LICENSED) {
//Application is licensed, allow the user to proceed.
} else if (event.status == LicenseStatus.NOT_LICENSED) {
//Application is not licensed, don't allow to user to proceed.
switch (event.statusReason) {
case LicenseStatusReason.CHECK_IN_PROGRESS:
// There is already a license check in progress.
break ;
case LicenseStatusReason.INVALID_PACKAGE_NAME:
// Package Name of the application is not valid.
break ;
case LicenseStatusReason.INVALID_PUBLIC_KEY:
// Public key specified is incorrect.
break ;
case LicenseStatusReason.MISSING_PERMISSION:
// License Check permission in App descriptor is missing.
break ;
case LicenseStatusReason.NON_MATCHING_UID:
// UID of the application is not matching.
break ;
case LicenseStatusReason.NOT_MARKET_MANAGED:
// The application is not market managed.
break ;
default:
// Application is not licensed.
}
}
}
The following changes are required in the application descriptor when writing an AIR application that uses a native extension for Android Licensing.
Add an Extension ID tag following the application tag:
<extensions>
<extensionID>com.adobe.air.sampleextensions.android.licensing</extensionID>
</extensions>
An Android permission is needed for all applications that perform a license check; you need to add it below the <manifest> tag:
<uses-permission android:name="com.android.vending.CHECK_LICENSE"/>
Also be sure the application descriptor namespace is set to 3.0:
<application xmlns="http://ns.adobe.com/air/application/3.0">
Note: The SWF generated here is a debug SWF. One of the ways to do this in Flash Builder (regardless of AIR or web project, or whether the export is available) is to add an argument to the compiler options. To do this go to: Project > Properties > Flex Compiler and then add -debug=false" to Additional compiler arguments.
To summarize, this application will perform a license check when the button is clicked, and print the response in the label text.
For your own projects, be sure to also read through the next section which has recommendations on securing your application.
To package your app you need following files:
Create a workfolder called PackageAL, copy all these files into this folder, and then use the ADT tool (available with the AIR SDK) to package the application.
On Windows
<Full Path to adt.bat> -package -storetype PKCS12 -keystore <path to Certificate for Native Extension> -storepass <password> -target ane com.adobe.air.sampleextensions.android.licensing.ane extension.xml -swc AndroidLicensingLib.swc -platform Android-ARM -C .\Android-ARM\ .
On Mac OS X
<Full Path to adt> -package -storetype PKCS12 -keystore <path to Certificate for Native Extension> -storepass <password> -target ane com.adobe.air.sampleextensions.android.licensing.ane extension.xml -swc AndroidLicensingLib.swc -platform Android-ARM -C ./Android-ARM/ .
When you have the ANE file, use the following command to package the APK file:
On Windows
<Full Path to adt.bat> -package -target apk -storetype PKCS12 -keystore <path to Certificate for AIR application> -storepass <password> AndroidLicensingUsage.apk AndroidLicensingUsage-app.xml AndroidLicensingUsage.swf –extdir .
On Mac OS X
<Full Path to adt> -package -target apk -storetype PKCS12 -keystore <path to Certificate for AIR application> -storepass <password> AndroidLicensingUsage.apk AndroidLicensingUsage-app.xml AndroidLicensingUsage.swf –extdir .
Note: If you want to see traces in device logcat, please change the target to apk-debug.
Users of your application will receive the response "Licensed" when you've published your application to market and they've purchased the application from Google Android Market.
Refer to the follow in tips to make your application more secure:
var retValue:int = extContext.call( "checkLicenseNative", BASE64_PUBLIC_KEY ) as int;
functionMap.put("checkLicenseNative", new AndroidLicensing() );
java –jar <Path to ProGuard.jar> @licensing.pro
Rename the SampleAndroidLicensing_o.jar to SampleAndroidLicensing.jar and use it in packaging ANE.
The following table covers some possible error messages and tips on how to resolve them:
| Error message | How to resolve |
|---|---|
Invalid Key Specification Error (from LVL in logcat) |
Be sure you've provided the exactly same key that is available in your Google Android Market portal. |
missingPermission (statusReason) |
Check if you've provided the Android Permission for Check License in the application descriptor. |
notMarketManaged (statusReason) |
The ID is not recognized by the Google Android Market application, because an application with this ID has never been uploaded to Google Android Market. Upload the proper application. |
Error contacting licensing server (from LVL in logcat) |
Be sure you've logged in once in the market application of your Android phone. Also make sure your device has a working internet connection. |
In this example, you have learned how to use the AIR native extension for Licensing your Android applications. To learn more about ANEs, go to the Native extensions for Adobe AIR page in the AIR Developer Center.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.