Friday, January 25, 2013

AIR Android Native Extensions: Part 3: Speeding up access of resources in Native extensions

In my previous post of this series, I explained how to access resources inside an native extension for Android. In this post, I am going to explain ways to speed up access of resources. In most scenarios, you may not need it at all, but if you are using really heavy Android UI components in your extension, like the Calendar component, you are at the right place.

I had an issue where I used a calendar component in one of my native extensions. When I ran that activity natively, it took 1 second to show up, but running the same thing in native took around 9 seconds. That's huge!!!

After a lot of research, I figured out that the issue was with Context.getResourceId calls. AIR was internally running a long loop, which used to take time accessing each resource. Calendar component had 31 cells (item renderers) and each item renderer had 9 calls to Context.getResourceId. AIR doesn't maintain a dictionary of the resources once fetched, which causes it to run the loop again and again.

As a quick fix, I maintained a dictionary of resources once fetched and it brought the load time of the activity down to 3 seconds. On loading the activity again, the time was down to 1 second only.

It was still not the ideal situation. This is where a colleague of mine, a Java bond, came to rescue. The next few lines of code are sheer magic and bring the activity load time exactly the same as native, even when launched first time. Checkout the magic:

public static int getResourceId(String resourceString)
{
    packageName = getActivity().getPackageName()+".R$";
    String[] arr = new String[2];
    arr = resourceString.split("\\.");
    Class someObject = Class.forName(packageName+arr[0]);
    Field someField = someObject.getField(arr[1]);
    return someField.getInt(new Integer(0));
}

That's it done. Magic, ain't it? Just call this function to get resource id instead of using extensionContext.

Someone please pass this code on to Adobe guys, I'd give you the name of my colleague for the credits section ;)

Hope this helps and saves your life at a time where you feel completely helpless. The next one in the series, that I have posted is: Using Third party libraries (JARs) in your android code.

P Android Native Extensions Part 2: Accessing R file variables from native extensions

In the previous post, I covered how to bundle the resources with the ANE. In this post I would cover how to access them from the native code, when it runs with the native extension.

Let us understand the issue first. When creating Android native applications or libraries, we use several resources such as images, layout XMLs, ids, strings. At the time of compilation Android generates an R.java file which provides the native code access to these resources. Examples:

R.id.myLayout
R.layout.mywebviewlayout
R.strings.close_button

This works perfectly fine when you run the app natively. However, when we run this code as an extension from the AIR application, the app crashes. A look at the DDMS log would tell you that a null pointer exception occurred at the place you were accessing the R file.

So, what is happening here? Let me explain.

When you compile an Android application, Android runs through all the resources used by the app and assigns them a unique integer id. If you open the R.java file from the gen folder of your app, you'd find something like this:
public static final class drawable
{
    public static final int ic_action_search=0x7f020000;
    public static final int ic_launcher=0x7f020001;
}
public static final class id
{
    public static final int menu_settings=0x7f070001;
    public static final int openwebview=0x7f070000;
}
public static final class layout
{
    public static final int activity_main=0x7f030000;
    public static final int rla_activity_main=0x7f030001;
}

Now, when you exported this code as a JAR, everywhere in the code where the R class was used, the references were replaced by their values.

Take a look at the following code:

    setContentView(R.layout.rla_activity_main);

In your JAR, the code would have compiled like this:

    setContentView(2130903041);

So, R.layout.rla_activity_main has been replaced with it's int value 2130903041

Now, what does AIR do that we don't find this resource, even if it is bundled with the application? Let us see.

Your AIR application has it's own resources plus it would have resources from your native extension and any other native extensions that you use. So, when compiling the final APK, AIR generates it's own R file. The Android SDK would once again loop through all the resources collectively and assign unique integer ID to each of them. Let's have a look at the same values defined in AIR's R.java file:

public static final class drawable
{
    public static final int ic_action_search=2130968576;
    public static final int op_action_some=213137980;
    public static final int ic_launcher=20979292;
}
public static final class id
{
    public static final int menu_settings=2131296275;
    public static final int openwebview=2131296260;
}
public static final class layout
{
    public static final int activity_main=2130837582;
    public static final int rla_activity_main=2130837585;
    public static final int someother_activity_two=2476689229;
}

As you can see, the IDs assigned to each item have changed. And this is where the issue is. Your native code is :
  setContentView(2130903041);

So, it is looking for rla_activity_main with the id 2130903041, however, AIR has assigned it a different id i.e. 2130837585. So, when you run the extension within the AIR app, it tries to find for the resource with if 2130837585, which is not present, hence the null exception.

Solution: AIR to rescue. The FREContext object that we use in our extensions exposes a function named getResourceId("resource_name"). This function would always give you the correct IDs mapped with the AIR's R file.

So, instead of using:
    setContentView(R.layout.rla_activity_main);

use:
     setContentView(extensionContext.getResourceId("layout.rla_activity_main"));

You can follow the same approach for all the resources,

For colors, use extensionContext.getResourceId("color.colorName") For id, use extensionContext.getResourceId("id.my_resource_id")

Also, do note that the extensionContext object is the extension's FREContext and not Android application context. Although, this works without fail, but at times accessing resources via extensionContext can slow down the application majorly. I would cover ways to speed up access to Android resources through the extension contxt. Stay tuned.

Hope this would save someone a few hours of googling atleast. Thanks and do check out the next article in the series. AIR Android Native Extensions: Part 3: Speeding up access of resources in Native extensions

Thursday, January 24, 2013

AIR Android Native Extensions - Issues and Solutions

In the last few months, I have worked on a lot of native extensions for AIR applications, both Android and iOS. I faced a lot of issues and there was little help available on the web and even if it was available, it was all scattered and not easy to understand.

In this post, I would focus primarily on the issues that I faced when creating Android native extensions. The issues range from trying to bundle resources into a native extension to accessing resources and so on. Some of the issues would have been real show stoppers if they were not resolved. The idea is to share my experience to help others save time.

I assume that the readers are familiar with basics of native extensions and are versed with compiling ANEs.

Let me first list down the issues I am going to talk about in this series of blogs. Series, not to make a single post too lengthy.
  1. Exporting assets / resources with the extension
  2. Accessing R file variables from an extension
  3. Speeding up access of resources in Native extensions
  4. Using third party JARs in your native code
  5. Exporting xLarge assets with the APK
  6. Preventing Android activity from being recreated on orientation change and relaunch
  7. Launching AIR's main activity from other Android activities and passing information


Exporting assets / resources with the extension and common errors
When we create a native extension for Android which contains a view, we generally use layout XMLs to design the layout and some images for buttons and icons and these files are placed in the res folder. By default, you'd tend to export these files along with the JAR and create the ANE. However, the final APK doesn't have any of these assets and your application would crash

To bundle the files along with the APK, we do not need to export the res folder along with the JAR. However, to bundle it with the ANE, we need to copy it separately alongside the JAR and make some change in the way you compile the ANE.

I generally follow the folder structure as follows:
[build]
  extension.xml
  extension.swc
  compile.bat
  [platform]
    [android-arm]
      res
      library.swf
      extension.jar

As you'd notice above, I have created an android-arm folder which has the res folder. The res folder contains the resources for the native extensions. We can name the folder anything, but it's important to keep the three items i.e. res folder, library.swf and extension.jar.
Once we've prepared the folder structure, you can execute a command line script as follows to compile the extension:

adt -package -target ane extension.ane extension.xml -swc extension.swc -platform Android-ARM -C ./platform/Android-ARM/ .

Note: Please note the space and dot (" ." ). It is supposed to be like this only. You can add iPhone platform in the similar way. However, the XIBs in iPhone need to be compiled first.

Doing the above would ensure the resources are bundled with the app. However, please ensure that the you keep unique names for your resource files so that they don't conflict with resources from other extensions. This is essential, otherwise AIR SDK would give errors when compiling when the final app. for example, by default when you create an eclipse project, it creates a file "strings.xml" in res/values folder. Rename the file to "yourproject-strings.xml" or anything unique you can think of. Similarly, the variable names inside the resource files must also be unique. So, if extension 1 defines are resource with id "webview", extension 2 should not use the same name, else you won't be able to compile the final APK. I hope this turns out to be useful for some. Do check out Part 2:Accessing R file variables from an extension.


Monday, June 18, 2012

HTML5, Flex or Native?


We are in a world which is moving at a very rapid pace and it's important that we move along.

The world is going mobile and most of the companies want to extend their offerings to mobile platform. For any developer or organization, the first question that comes to mind is which technology to adopt. When I started, I had heard of three HTML5, Flash and Native (Java for Android and Objective C for iOS), of course there are others like Corona and Titanium.

When I started, I also faced this difficult choice. However, the decision was made easier by the fact that Flash / Flex was not supported on iOS and I had not worked on Objective C at all so HTML5 was the easier way out, more so, considering the fact that our product was going to be a cross platform app.

HTML5
HTML5 is cool and it's easy to develop things once you get the hang of it. With amazing libraries like Backbone JS, Sencha, Jquery and others, it's fun to develop with HTML5. For the APIs like Camera, FileSystem etc. that HTML doesn't have direct access to, PhoneGap comes to rescue. The most commonly used APIs are provided by PhoneGap and it also provides the ability to write your own plugins to extend the reach of your app. The best part of using a cross platform technology like HTML is that your core application code needs to be written just once and it's only the plugins that need to separately developed for each supported platform. So, overall the combination of HTML5 and PhoneGap is amazing. This is the reason that a lot of companies are adopting HTML5 as their preferred technology.

However, there are some limitations too. As HTML5 is dependent on the browser component, the behavior is not always consistent across devices. The issue is more prominent on Android as each device manufacturer comes up with their own browser which interprets things differently. Such issues make developer's life miserable as it is impossible to always depict the correct behavior.

Flash
Coming to the other cross platform technology i.e. Adobe Flash. Despite all the predictions of Flash being on verge of death and the PR blunder by Adobe, Flash is still going strong. It is still the most preferred platform for games development. Like HTML5 Flash is also a cross platform tool that allows you to code once and deploy it to multiple platforms. One major advantage Flash enjoys over HTML5 is that it is not dependent on the browsers so the behavior is generally unique across devices. Like PhoneGap plugins, Flash has a concept of ANE. AIR Runtime Extensions allow the developers to write native plugins to expand their app's reach. Stage3D and StageVideo enable developers to improve the performance by getting rid of the display list. Another great advantage of using Flash is that you can make use of the huge amount of eLearning assets made in Flash over the years.
As with HTML5, there are certain limitations with Flash too:
  1. AIR run time gets included with all the mobile apps which adds up around 2 MB. If you use Flex SDK, add 3-4 MB more
  2. Build process for iOS is slow and it's very painstaking to debug iOS apps built with AIR
  3. For any issues or bugs in the AIR run time, one needs to wait for the new releases by Adobe.
Flex has been donated to Apache by Adobe and they are pretty close to rolling the first version of Apache Flex out. I see a lot of promise here. So, despite what people say, Flash is still live and kicking.

Native Development
By native development, I mean Java for Android and Objective C for iOS.Developing in native language would assure you of one thing for sure and that's that the performance of your would be much better when compared the cross platform technologies, be in HTML5 or Flex.

When developing native, you would invariably be forced to follow the workflow designed for the particular platform. This is one aspect, which generally gets ignored when developing cross platform and hampers the user experience. An iPhone user is used to seeing the apps behave in a way and Android users in other so it's important to take care of these while development. Native development helps in faster development too. As all the APIs are available for disposal, there is no need to write extensions to be able to use them and this saves a lot of development effort. The only shortcoming with developing native is that you'd need to learn multiple technologies and each app would need to be rewritten for all the platforms it supports.

So, at the end of it all, do we have a clear answer on which technology to use? No, I believe that it's horses for courses. Carefully analyze the job in hand and then decide on the technology. Love the times we are in, so many tools available to do the job, pick and choose any one.

Tuesday, November 30, 2010

Unit Test Cases - Mocking and Stubbing

When it comes to unit testing, what's the biggest issue you face? I generally get stuck with the dependencies among different classes, stubbing the actual environment. I am a Flash - Flex programmer but feel that the issue applies to any language you code in.

I am not too deeply familiar with the jargon of Unit Testing but I always try and keep implementing new things that help write better code. So, if you are a master of Unit Testing, the post is not for you. It is more for the people like me getting started with this. Do not expect it to be a tutorial on Unit Testing, but I would share my first experience with mocking. I also do hope that audience have a basic understanding of writing test cases with Flex Unit 4.

I wanted to test out the function below:

public function handleTaskComplete(evt:TaskEvent):void
{
   this.data = evt.target.data.toString().toLowerCase();
}

The problem is that the TaskEvent class does not allow me to specify the target property so I cannot simply create an instance of TaskEvent and pass it on. Similarly the Object returned by TaskEvent does not allow to set the data property. Mocking comes in handy here.

I can create a mock class for TaskEvent and also mock it's target method to return what I desire. There are loads of free Flex-AS3 mocking libraries but I found mockolate easiest to comprehend ;) hence I used it. Open source library, available at "http://mockolate.org/".

It took me a litte time to get all the pieces together. Basically, the mockolate source uses these libraries as well:
  • asx.swc
  • flexunit-core-flex-4.1.0-beta3.x-sdky.y.y.y.swc
  • FLoxy.swc
  • hamcrest-as3-1.1.0.swc
I create a new library project and combined all of these to one SWC. If you need it, give me a shout.

Once I had the SWC ready and included in my project, I proceeded to code.


[Test(async, timeout=30000)]
public function testMenuBarLoad():void
{
   Async.handleEvent(this,
   prepare(TaskEvent, XmlLoaderTask),
   Event.COMPLETE, startActualTest, 10000);
}

As you'd notice, the function above is my test function. I've made it an asynchronous function with some timeout. Before, being able to mock or stub any classes, we have to prepare them. Once the classes are prepared, "complete" event is dispatched. I can specify any number of classes to be prepared at once.

Once my classes were ready for mocking, startActualTest event was dispatched.

private function startActualTest (event:Event, data:Object):void
{
   var data:XML = getTestData();
   var taskEvent:TaskEvent = nice(TaskEvent, null, ["A"]);
   var xmlTask:XmlLoaderTask = nice(XmlLoaderTask, null, ["A"]);

   mock(taskEvent).getter("target").returns(xmlTask);
   mock(xmlTask).getter("data").returns(data);

   myComponent.handleTaskComplete(taskEvent);
   assetEquals(data, myComponent.data)
}

Let me go through the code in the function above.

Firstly, I create a nice mock object for the TaskEvent and XMLLoaderTask with the constructor arguments in the array. Mockolate allows 2 types of mock, nice and strict. Read documentation for details.

Secondly, we mock the getter "target" in taskEvent mock to return our mock object of xmlTask.

Thirdly, we mock the getter of xmlTask to return the desired data. Finally, we invoke component's handleTaskComplete method with the mocked taskEvent . In the last line, we add an assert to test whether data has been successfully set.

Mockolate provides us with provisions to test methods. It even allows us to define that if a method is called with a particular argument or a particular argument type, what should be returned. For example:

stub(flavour).method("combine").args(otherFlavour).returns(combinedFlavour);
We can also verify whether the functions mocked by us have been invoked or nor. This is just one of many cool things you can do with mockolate. I found mocking using mockolate to be of great help in creating a stub environment for unit testing.

Hope this post be of some help to you. Write to me at pulkit.gupta@gmail.com for any feedback.

I faced some issues getting a build ready for unit testing using ANT, so I would probably add that in my next post.