ZOFTINO.COM android and web dev tutorials

Android App Development Tips for Beginners

No matter which programming language you use and type of application you are planning to develop, there are certain programming aspects like exception handling, logging and debugging, need to be taken care of in order to create your application fast and produce quality software.

In this post, I am going to talk about how exception handling, logging and testing can be done in android applications and provide some android studio tips for easy and fast coding.

Testing android apps & virtual device acceleration setup

You can test android apps developed in Android studio with mobile devices connected to computer via USB or using android emulator. Android emulator tool lets you create virtual device. You can find the tool under Tools > Android > AVD Manager. Creating virtual device is easy; just follow prompted steps to select configuration for your target virtual device.

One setup related to hardware acceleration is required to use virtual device to improve the performance of virtual device. Android studio suggests you to install required software if your computer supports virtual machine acceleration. For windows, the required installation for virtual machine acceleration is Intel HAXM. Just follow the link suggested by Android studio to install this. If you don’t install this you will get error “x86 emulation currently requires hardware acceleration” when you try to run virtual device.

After installation, if Intel virtualization technology is not enabled on your computer, you’ll get error “Intel virtualization technology (vt,vt-x) is not enabled” when you try to start virtual device in Android studio. You can enable Intel virtualization technology in your computer BIOS.

Exception handling

Android application exceptions can be handled like other Java based applications. While developing apps, you can check console output from logcat monitor in IDE to find out exceptions and take appropriate action like fixing code, showing a message to user or starting different activity etc. For example, if your app interacts with server side service for data, you need to handle the exception which gets thrown when the service is down and show message to user saying the selected feature is temporarily unavailable.

There could be unhandled exceptions in your app which may get thrown when your app runs on user device causing it to crash. To analyze and fix these issues, you need crash report from the device on which your app is crashed.

You can add global exception handler to each component to catch unhandled exceptions, save exception and other required information on device and send it your server or to you via email for analysis. There are frameworks or libraries to handle exceptions and send reports to you, like ACRA library.

Logging

You can use Log class to log messages which you can view in logcat monitor. There are methods in the class to log different level of information, Log.e for error, Log.i for information, Log.d for debug, Log.v for verbose etc. It is a good practice to use debug and verbose while developing apps and make sure that these statements are not compiled into app except for development.

Android studio tips

Android studio highlights classes in red which are not imported. To import classes, you need to point your mouse to one of the unresolved classes and press Alt + Enter. To remove unused imports, you can use Ctrl + Alt + Enter. There is an global level setting “optimize auto imports”, which can be found under file > setting > editor > general > auto import, to auto import classes. To search for classes, you can use Ctrl + N.

Android studio offers several refactoring options which can be used to make code changes like class signature, method signature changes, parameter renaming etc. Refactoring tracks all the affected code references and corrects automatically.