Friday, January 17, 2014

A Few Tips for Beginning Android Development

 1. Choice of IDE

Most of the Android development is centered around Eclipse, although I coded a few applications using IntelliJ IDEA's Community Edition without any problem. Personally, I don't care much about Eclipse's UI drag-and-drop interface. which is anything but impressive. Way more work needs to be done here.

2. Develop for the masses

All of us would just love to use the latest Android version with all the cool new stuff. Unfortunately, the Android smartphones on the market don't exactly follow the speed of official version releases. So before diving into coding, do a preliminary check. An application written for the older versions works with the newer ones, but the reverse is of course not true. At this time, version 2.3 (Gingerbread) is still the dominant one. so you might want to wait a while before biting into an ice-cream sandwich.

3. Forget the Emulator, get the Real Deal

Forget the emulator. It's painfully slow, and you can only test a limited number of things on it. I use it only to test how my apps look like on bigger screen sizes, and that's about it. So, if you want to save time, use a real device connected via USB. I'd say you actually need two devices, since odds are, you'll have to test things like SMS, Bluetooth, and anything else requiring device-to-device communication. Even better if your second device has a different screen size. The more different the devices, the better you can test your app.

4. Don't like XML? Too bad

All of Android's UI is based on XML, which caused most of my initial headaches when I started. Although there are some tools that can assist you with some aspects of the UI, like Action Bar generators (such as the Sherlock, or the one from Johannilsson on Github), I am not aware of a complete GUI builder at this time, that would do all the low-level work and not be a Leaky abstraction . All the visual editing tools I tested were buggy or incomplete. If you happen to know a decent one, please let us XML-wary folks know.

5. Use modular UI structure

The amount of XML  needed to build a decent UI can become quickly...daunting. Therefore, build components for reuse between different layouts as much as you can, using includes and fragments. Fragments were introduced in version 3.0, but you can download support libraries if you are developing for earlier versions.

6. You need a Designer

Or you need to become one yourself. There are a few online facilities  for adding common GUI elements to your app, like the Android Asset Studio, but if that's not enough, you'll need to design your own, adapting for different screen intensities. Be sure to also read the official Android Design guidelines.

7. Use Android-specific solutions where applicable

The Android-specific solution is usually simpler and/or yields better performance. Taking multi-threading & asynchronous processing as an example, consider using an IntentService or AsyncTask before looking at the java.util.concurrent package, although in some cases, the latter might ultimately be the way to go.

8. Google is still your friend

Although the amount of documentation on the Android developer official site is extensive, it is often faster to just google what you're looking for. As a concrete example, here's a result search on deleting SMS messages on arrival at stackoverflow that gives more insights than the official docs.

9. Beware of Miracle Tools

Without mentioning a specific one, be wary in general of products that pretend to ease smartphone development across the board (Android, iOS and Blackberry RIM, all at the same time). I am not saying they are all crock, but be sure to test them first by developing a simple yet significant app and deploy it on all of them. Each display should look like a native app on that OS. Of course if you are only developing a Web app for mobile devices, you need not be overly concerned by this.

10. All good software practices still apply

Test extensively (using Monkey, for example), design for i18N and localization, use Design Patterns etc.
At a very bare minimum, you should make an effort to separate UI code from your business logic, even for the smallest applications. Not doing so is just asking for trouble down the road.

Developing on Android is an exciting experience, in spite of all the frustrations caused by imperfect  or non-existent tools. But that is sort of  logical in a relatively new environment. The bright side is, things should get better overtime, and fast.
Thank you 

Thursday, January 2, 2014

Program for Android in C/C++ with the Native Development Kit

Not a big fan of Java? Well, get over it, because that's the primary and recommended way to write applications for Android devices. It's portable and... what's that? Android's Dalvik Java VM not fast enough for you? Granted, it's an interpreted engine and as of version 1.5 there's no Just-In-Time compiler. But Dan promises... oh, can't wait for the JIT to come out? Ok, ok, we'll let you program in C if you're really sure.
Introducing the Android Native Development Kit (NDK). With it, you can implement *parts* of your application using native-code languages such as C and C++. You're familiar with the Java Native Interface (JNI), right? JNI lets you load a shared library and call C code from within Java. The NDK lets you compile and build those libraries for the ARM CPU chip used in all *current* Android devices.
The NDK provides:
  • A set of tools and build files used to generate native code libraries from C and C++ sources
  • A way to embed the corresponding native libraries into application packages files (.apks) that can be deployed on Android devices
  • A set of native system headers and libraries that will be supported in all future releases of the Android platform, starting from Android 1.5
  • Documentation, samples, and tutorials
Users downloading your program from the Market will not be able to tell whether or not you used native code. In fact, some apps already on the Market use it.
The AIDE app does not itself include the NDK. Instead it will install a mobile version of the NDK on your device once it is needed. You can either start the installation manually in the settings. Alternatively, AIDE will also ask to install if you create a new NDK based project or open an existing one.

There are two ways to use the NDK: Either write your application in Java/Xml using the Android SDK and use JNI to access the APIs implemented in C/C++ using the Android NDK. Or write a native activity in C/C++, which allows you to implement the lifecycle callbacks in native code. AIDE supports both scenarios and comes with sample apps for both. Create a new app project as described in the tutorial about Building your first App but choose one of the "C/Java/Xml" projects. You will see that some C files are part of these projects. You can use AIDE to work on the C code as well now. You will see errors in C/C++ code once you run the app and the code is compiled.

Running an NDK app project works as normal by selecting "Run" from the menu. AIDE will use the installed NDK to compile the C/C++ code of your app and run the app afterwards.

Keep in mind that using the NDK will not be relevant for all Android applications. As a developer, you will need to balance its benefits against its drawbacks, which are numerous! Your application will be more complicated, have reduced compatibility, have no access to framework APIs, and be harder to debug. That said, some applications that have self-contained, CPU-intensive operations that don't allocate much memory may still benefit from increased performance and the ability to reuse existing code. Some examples are signal processing, intensive physics simulations, and some kinds of data processing.