Introduction
You have read the article on Introduction to Android. You are tempted to jump in and start writing code as quickly as possible, right? Wrong! not so fast. You cannot make something out of nothing. Your have not yet had the essential tools and setup necessary for performing development work in Android. These tools and setup are collectively called the Development Environment. A development environment is the location where there exists tools that allow you to write, compile, test, and debug your projects before uploading it to the staging environment and then to the production environment. The most common location for your development environment is your own computer. This article will walk you through setting up an Android development environment on your computer.
Before the journey begins, I have the following good news to tell you :)
-
Android development environment can be set up on any operating systems, be it Windows, Mac, or Linux.
-
All the essential tools to developing Android applications are freely available and downloadable from the Web.
Let the journey begin...
Getting to Know the Tools
Open the toolbox, you will find the following tools that are necessary for setting up an Android development environment.
-
Java SE Development Kit (JDK)
-
Android Software Development Kit (SDK)
-
Integrated Development Environment (IDE)
-
Android Studio; or
- Eclipse + Android Developer Tools (ADT)
Let's have a quick overview of these tools.
Java SE Development Kit (JDK)
The JDK is a set of Java libraries for building applications, applets, and components using the Java programming language. The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform. Since the natural language of Android programming is Java, the JDK is a necessary tool to have in your Anrdoid development environment. Among other things, it consists of the following essential tools:
-
A Java compiler javac that compiles your .java files into byte code
-
A Java Runtime Environment (JRE) that executes the byte code
-
A jar tool that helps build jar files
Android Software Development Kit (SDK)
The Android SDK is a set of API libraries and developement tools necessary to build, test, and debug applications for Android. Among other things, It consists of the following main components:
-
Android-specific libraries for building Android applications.
-
A command-line tool for managing Android projects in window terminal.
-
Dalvik Virtual Machine for compiling Android applications.
-
Android Emulator for test running Android applications without a physical device.
- Dalvik Debug Monitor Server (DDMS) for debugging Android applications.
Integrated Development Environment (IDE)
To write a computer program, At the very least, you will need another computer program to act as a text editor where you can type the code in plain text. You may use Notepad for PC, TextEdit for Mac, or vim for Linux. Using a text editor is the most simplistic way to write your computer program (or so it seems). Once the code is written it can be compiled and run by using some command-line tool in a terminal window. However, if you program refuses to work or does not work correctly (which happens most of the time, not mean to frighten you), you will have to open it back in the text editor to debug it, and then re-compile and run it again using the command-line tools in the terminal window.The drudgery of the debugging process and the hassle of switching back and forth between the text editor and the terminal window would quickly drown out the enthusiasm of many would-be programmers. Fret Not! IDE to the rescue!
An IDE is a software application that facilitates application development. In general, an IDE offers a composing and editing environment that consists of a code editor, a compiler, a debugger, and a graphical user interface (GUI) builder. It provides an integrated environment whereby a developer can quickly execute different aspects of application development all-under-one-roof without the need to switch to other applications. A good IDE makes you more productive and efficient. Projects can get off the ground faster with the help of scaffolding, coding can moves faster with the help of intelligent autocompletes and refactoring tools, applications become more maintainable with the help of integrated unit testing tools.
For the purpose of Android development, you can choose between two IDEs - Android Studio or Eclipse.
- Android Studio
Android Studio is a new open source Android development environment developed by Google based on the open source community edition of IntelliJ IDEA. It was announced on 16 May 2013 and is currently in beta stage. While Eclipse was built to be a general all-purpose IDE that can be used with any platforms and any languages, Android Studio is designed specifically for Android development only. It is designed to provide new features and improvements over Eclipse ADT and will be the official Android IDE when it is ready.
- Eclipse with Android Development Kit (ADT)
Eclipse is an open source IDE famous for Java development traditionally. It is written primarily in Java. It is highly extensible and versatile. By means of appropriate plugins, Eclipse can also be used to develop applications in other programming languages: Ada, ABAP, C, C++, COBOL, Fortran, Perl, PHP, Python, R, Ruby, and many more.
To develop Android using Eclipse, a plugin called Android Development Kit (ADT) has to be added to Eclipse. The ADT extends the capabilities of Eclipse so that a developer can quickly set up new Android projects, create an application UI, add packages based on the Android Framework API, debug applications using the Android SDK tools, and export .apk files for distribution all within a single development environment.
Android Studio vs Eclipse ADT
Table 1 lists the key differences between Android Studio and Eclipse with ADT.
Table 1: Androd Studio vs Eclipse ADT Feature | Android Studio | Eclipse ADT | Elaboration |
Build Tools | Gradle | Ant | Build systems are software tools designed to automate the building, testing, publishing, deployment of software packages.
Android Studio utilizes the fast growing Gradle build system that builds on top of the concepts of Apache Ant and Apache Maven. Instead of the more traditional XML form of declaring the project configuration, Gradle introduces a Groovy DSL (Domain-Specific Language) that allows for scripted builds that opens up many automation possibilities.
Eclipse on the other hand uses Apache Ant as its main build system which a very robust XML based build system that many Java developers are familiar with.
|
Maven-based Build Dependencies | Yes | No | Rather than "reinventing the wheel", every software development is encouraged to reuse existing code as much as possible by taking advantage of other software components that are already available, or have already been designed and implemented for use elsewhere.
There is not much difficulty in managing dependencies for a single project, but when it comes to multi-module projects and applications that consist of tens or hundreds of modules, you would be thankful for having an excellent dependency management tool like Maven to help you to resolve the many dependency issues.
|
Build Variants | Yes | No | Many mobile applications have a free version and a paid version while other applications may ship seasonal variations.
With Gradle, you can generate multiple distributions (APKs) with different configurations for the same project. In this way, you can build different versions of your application without having to create a separate project for each of them.
|
Android Code Completion and Refactoring | Yes | No | Both IDEs provide the standard Java code auto completion but in the case of Android Studio, Google has added support for Android specific code completion and refactoring. |
Native Development Kit (NDK) | On the way | Yes | While the Android SDK supports Java, the NDK provides additional tools to support native development in C/C++.
|
User Interface Design Tool | Yes | Yes | User Interface Design Tool provides GUI access to many of the command line SDK tools as well as a UI design tool for rapid prototyping, designing, and building of your Android app's user interface.
Android Studio comes with its own new user interface design tool while for Eclipse, this is provded through the ADT plugin.
|
Applications Signing | Yes | Yes | The Android system requires that all installed applications be digitally signed with a certificate as a means of identifying the author of an application and establishing trust relationships between applications.
Both IDEs provide the neccessary tools to automate the signing process.
|
As Android Studio is currently in beta stage, some features are not yet ready and you may encounter bugs from time to time. But that is understandable for any project under development. The development of Android Studio project is being carried out in the open in Android Open Source Project. You are encouraged to try out Android Studio and send your feedback. I am definitely going to support it, so do you. :-D
Update:
On 8 December 2014, the Android team has officially released Android Studio 1.0 as the official IDE for Android development. Android Developers worldwide have been advised to adopt Android Studio.
Quote:
Note: If you have been using Eclipse with ADT, be aware that Android Studio is now the official IDE for Android, so you should migrate to Android Studio to receive all the latest IDE updates. For help moving projects, see Migrating to Android Studio.
Setting Up an Android Development Environment
You have learned of the tools that can be used to set up an Android development environment, let proceed to set up one on your computer. For the IDE, I strongly recommend Android Studio for the reasons that it is going to be the de facto IDE for Android development and its many new features as shown in Table 1. I will walk your through setting up an Android development environment on your computer. Let's get going...
Step 0 - Installing Java Development Kit (JDK)
Before setting up Android Studio, your computer will need to have JDK version 6 or greater installed. To check if your computer have any JDK installed and its version if any, open a terminal and type javac -version.
To open a terminal in Windows, do this: Start menu > Run > cmd > Enter. (Figure 1)
|
Figure 1: Checking JDK Version using a Terminal
|
On Mac OS X, open Applications folder > open the Utilities folder > start the Terminal application.
If the JDK is not available or the version is lower than 6, you can download the latest one here. Remember, it is JDK not JRE.
Step 1 - Installing Android Studio
Once the JDK is installed, head straight to the Android Studio page (Figure 2) at Android Developers site and download the correct version of Android Studio according to the platform of your computer.
|
Figure 2: Android Studio Download Page
|
-
Setting up Android Studio on Windows:
-
Double-click the downloaded EXE file, "android-studio-bundle-<version>.exe".
- Follow the setup wizard to install Android Studio, the whole process should be a breeze.
Although it rarely happens now, when you try to launch Android Studio for the first time, the launcher script may complains that it is not able to find the JVM, then you will have to go and set an environment variable to explicitly indicate the location of the JDK like this:
Select Start menu > Computer > System Properties > Advanced System Properties > under the Advanced tab > Environment Variables, and add a new system variable JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.6.0_25, and restart the installation again.
By default, your Android Studio will be located at C:\Users\<user>\AppData\Local\Android\android-studio which contains the tools and SDK packages.
-
Setting up Android Studio on Mac OS X:
-
Open the downloaded DMG file, "android-studio-bundle-<version>.dmg".
- Drag and drop Android Studio into the Applications folder.
When you attempt to open Android Studio for the first time, you might encounter a warning saying the package is damaged and should be moved to the trash. If this happens, go to System Preferences > Security & Privacy > under Allow applications downloaded from, select Anywhere. Then open Android Studio again.
By default, your Android Studio will be located at /Applications/Android\ Studio.app which contains the tools and SDK packages.
-
Setting up Android Studio on Linux:
-
Unpack the downloaded tar file, "android-studio-bundle-<version>.tgz", into an appropriate location for your applications.
- To launch Android Studio, navigate to the android-studio/bin/ directory in a terminal and execute studio.sh.
You can add android-studio/bin/ to the PATH environmental variable so that you can start Android Studio from any directory.
Catch a glimpse of the installing screen in Figure 3.
|
Figure 3: Android Studio Installing...
|
Once installed, your Android Studio directory should look like that as shown in Figure 4.
|
Figure 4: Android Studio Directory
|
Once the installation is done, Android Studio will start automatically. If not, just fire it out, you will then see the launch screen (Figure 5) followed by the welcome screen (Figure 6).
|
Figure 5: Android Studio Launching...
|
|
Figure 6: Android Studio Welcomes You
|
You may thought that the Android Studio is now ready for use. Not so fast. As the default SDK does not include everything you need to start developing, you still need to add some more components to make your Android SDK complete. So do it now...
Step 2 - Adding SDK Packages
Android SDK organizes tools, platforms, and other components into packages that you can download using the Android SDK Manager. The beauty of this is that when exisiting SDK tools are updated or a new platform or new components are released, the user can use the SDK Manager to quickly download and add them to the Android Studio. To launch the SDK Manager, on the Welcome screen, select Configure > SDK Manager. (Figures 7 and 8)
|
Figure 7: Configure
|
|
Figure 8: SDK Manager
|
Alternatively, you can launch it inside the Android Studio by selecting Tools > Android > SDK Manager. The packages in the Android SDK Manager are organized into three sections - Tools, Platforms, and Extras as shown in Figure 9.
|
Figure 9: Android SDK Manager
|
Click on the arrows besides the respective sections to reveal the packages. You can select which packages to download by toggling the corresponding checkboxes, then click the Install button to install the selected packages. Go through the list of packages to make sure that you have at least the following packages installed, if not download and install them.
Step 3 - Adding New Sites
This step is optional. In addition to the default packages provided by the SDK Manager, you can add Android packages from third-parties. To manage third party add-ons, in the SDK Manager, select Tools > Manage Add-on Sites (Figure 13).
|
Figure 13: Android SDK Manager
|
This will open a new window with two tabs - Official Add-on Sites (Figure 14) and User Defined Sites (Figure 15).
|
Figure 14: Official Add-on Sites
|
|
Figure 15: User Defined Sites
|
By default, SDK Manager displays packages available from the Android Repository and Official Add-on Sites. You can add additional sites that host their own Android SDK add-ons by entering their URLs in the User Defined Site, then download the SDK add-ons from those sites using the SDK Manager.
Step 4 - In Case of Trouble
If you encounter connection problems while using the Android SDK Manager to download packages, try switching from the default https to http protocal may help. To make the switch, follow these steps:
-
In the SDK Manager menu, select Tools > Options (Figure 16) which will open the Settings window.
|
Figure 16: Options Menu
|
-
In the Settings window, under the "Others" section, check the checkbox labeled "Force https://... sources to be fetched using http://...". (Figure 17)
|
Figure 17: Settings
|
- Click the Close button and it is done.
Step 4 - (Not a Step) Android Studio is Ready
So far you have successfully set up an Android development environment using Android Studio on your computer. You are almost ready to start learning to develop Android applications. But to test your applications, you need an Android device. The good news is Android SDK includes a virtual Android device emulator that you can use to test your applications on your computer without the need for any physical device. In order to make your Android development environment more complete, you should harness the power of this emulator, so it is imperative that you get to know it...
The Android emulator can emulate all of the hardware and software features of a typical Android device. It provides a screen in which your application is displayed. You use your mouse or keyboard to emulate any interaction with your application as you would on a physical device. For quick reference, Table 2 summarizes the mapping between the emulator keys and the keys of your keyboard (reproduced from Android Developers).
Table 2: Mapping between Emulator keys and Keyboard Keys Emulator Key | Keyboard Keys |
Home | HOME |
Menu (left softkey) | F2 or Page-up button |
Star (right softkey) | Shift-F2 or Page Down |
Back | ESC |
Call/dial button | F3 |
Hangup/end call button | F4 |
Search | F5 |
Power button | F7 |
Audio volume up button | KEYPAD_PLUS, Ctrl-F5 |
Audio volume down button | KEYPAD_MINUS, Ctrl-F6 |
Camera button | Ctrl-KEYPAD_5, Ctrl-F3 |
Switch to previous layout orientation (for example, portrait, landscape) | KEYPAD_7, Ctrl-F11 |
Switch to next layout orientation (for example, portrait, landscape) | KEYPAD_9, Ctrl-F12 |
Toggle cell networking on/off | F8 |
Toggle code profiling | F9 (only with -trace startup option) |
Toggle fullscreen mode | Alt-Enter |
Toggle trackball mode | F6 |
Enter trackball mode temporarily (while key is pressed) | Delete |
DPad left/up/right/down | KEYPAD_4/8/6/2 |
DPad center click | KEYPAD_5 |
Onion alpha increase/decrease | KEYPAD_MULTIPLY(*) / KEYPAD_DIVIDE(/) |
However, not being a real thing does impose some inherent limitations on the Android emulator. It does not, among other things, support the following functions:
-
Placing or receiving actual phone calls.
-
Bluetooth and USB connections.
-
Determining network connection state.
-
Determining battery charge level and charging state.
To use the Android emulator, you have to first create one or more emulator configurations called the Android Virtual Devices (AVDs). Read on...
Creating an Android Virtual Device (AVD)
An Android Virtual Device (AVD) is an emulator configuration that models an actual Android device by specifying the hardware and software options to be emulated. You can create as many AVDs as you need. In each AVD, you specify an Android platform to run in the emulator and the set of hardware options and emulator skin to use. Each AVD functions as an independent device, with its own private storage for user data, SD card, and so on. This makes running and testing Android applications on a wide range of devices possible and very cost effective.
Follow these steps to create an AVD:
-
In the SDK Manager menu, select Tools > Manage AVDs (Figure 18) which will open the AVD Manager window.
|
Figure 18: Manage AVDs Menu
|
Alternatively, you can launch it inside the Android Studio by selecting Tools > Android > AVD Manager.
-
Under the Android Virtual Devices tab of the AVD Manager window,you will see a list of previously created AVDs, if any. Click on the Create button to create a new AVD. (Figure 19)
|
Figure 19: AVD Manager
|
-
In the Create new Android Virtual Device (AVD) window, enter the values as shown in Figure 20, then click on the OK button to create the AVD.
|
Figure 20: Configure new AVD
|
You will see a pop-up window showing a summary of the options chosen for the new AVD, just dismiss it. You are free to experiment with different values.
-
The newly created AVD will appear in the list of AVDs in the AVD Manager window. Click on the Start button to start the new AVD.
|
Figure 21: New AVD Created
|
-
Be patient, and you will get to meet your first AVD like this one in Figure 22.
Note:
The Android emulator is a great tool, besides its intended use, it is also good for your health, though unintentionally, I swear, hear me out. Android emulator is well-known for its slowness, do not expect it to response immediately after a click, you have got to be very very patient with it. But the wait is worth it as it will do its work as promised, and it forces you to slow down your pace and that is good for your heart. In my previous article, I have recommended that once the emulator is started, leave it running while you do your work.
|
Figure 22: Your First AVD
|
-
Alternatively, you may wish to use pre-defined AVDs available under the Device Definitions tab of the AVD Manager window. (Figure 23)
|
Figure 23: Pre-defined AVDs
|
When you are selecting a system image target for your AVD, the API Level of the target must not be lower than the minSdkVersion attribute of the application's manifest file, otherwise, your application will not be able to run.
Congratulation! You have successfully set up an Android development environmet on your computer. You are now really ready to start your learning journey on Android development. However, that will be our subsequent journeys. For the next half of the journey, I shall round up some Best Practices that will guide you to creating better Android applications, followed by some Tips on how to use the Android Studio more efficiently and productively.
Best Practices
Here, I am not talking about best practices for programming in general, i.e. code commenting, code reuse etc. Yes, they are important and you should follow. But they are more of common sense and commonplace now. Instead, I will focus on best practices that pertain to mobile development in general and Android development in particular.
The Android market is highly fragmented. On one hand, there is the device fragmentation that sees Android devices ranging from smartphones, tablets, to watches and they come in all shapes, sizes, and different degrees of sophistication. On the other hand, there is the API fragmentation that sees many different versions of Android systems running on different devices.
In view of this highly fragmented market, before you start running off to creating Android applications, you are already confronted with a string of difficult questions, among them, how to ensure that your application can run on as many versions (new and old) as possible? How to make sure your application stays responsive to different devices? How to make sure that your application will run on all the devices that downloaded it? How to improve the usability of your application?
Backwards Compatibility
You should always declare the minimum SDK version that your application would support in the Android manifiest file . This ensures that your application is backwards compatible to run on older versions (down to the minimum SDK version). At the same time, the Android system will prevent users from installing the application if their system's API Level is lower than the minimum SDK version of the application.
On the question of which minimum SDK version your application should support, you should consult Android Dashboards for the frequent update on distributions of platform versions, screen sizes and densities of Android devices.
Forwards Compatibility
You should always build your Android application against the latest platform version in order to use new features when running on devices with the latest version of Android. In addition, you should also test your application on an AVD that uses a higher API Level as target than that required by your application. In other words, you should make your application forwards-compatible. Forward-compatibility ensures that when users who have used your application receive a system update, your application will continue to function properly.
Responsive Design
In Android, you can use Fragments (modular Activities) in your Android appilcations to achieve responsive design. By using Fragment, for example, your application can show a two-column table on a tablet, with the left column showing list of items and a detailed view of the selected item on the right; the same applicaton when runs on a smartphone will show the list of items on a single page and the selected item on another page.
Right Apps for Right Devices
You must make sure that your application will run on all of the devices that downloaded it. For example, if your application requires telephony services to work then it would make no sense to install it on a WiFi-only device. To prevent this from happening, you can declare this requirement explicitly in the Android manifest file like this:
<uses-feature android:name="android.hardware.telephony" />
Google Play uses the <uses-feature> elements declared in your application's manifest to filter your application from devices that do not meet the feature requirements. As a result, only users whose devices meet the application's feature requirements are presented with this app by Google Play.
More Usable Apps
Mobile apps do not have the luxury of fast CPU and abundant memory enjoyed by conventional computers. Getting your app up to speed is as important as the ideas of the app itself. Compare to ugly apps, slow apps are more likely to annoy and drive users away. Improving the speed of your app calls for careful design and coding approach right from the very beginning.
You app should avoid loading large amount of data over the network unless the users has intentionally requested it. In this case, consider showing a progress indicator like a progress bar for situations where it take several seconds to perform an action so that the users are kept informed and not thinking that the app has hung. Actually, there is a better approach, that is the use of AsyncTask.
AsyncTask let you run time-consuming tasks in background threads asynchronously while users continue to interact with their UIs. The results can be shown to the users' UIs when the background tasks compeleted.
Another aspect that could have slowed down your app is how the UI is rendered. For example, if your app is programmed in such a way that it loads and render an image first, followed by some text, followed by an image, then more text. This way of alternating between different data types uses up memory and slow down your app considerably. Instead, you should always render views of the same data type one by one before switching to views of another data type. This will allow the views to load quicker and save on memory space. The end result is faster app.
Tips
Android Studio provides a number of very useful tools to help you create quality applications in shorter time. They are briefly mentioned below. I have also inlcuded some other tips that can assist you in using the various development tools in Android Studio more efficiently and productively.
Live Layout
Android Studio comes with a Live Layout feature that lets you preview the user interface of your app during development without the need to run it on a physical device or the emulator. Some of its functions include zooming, refreshing, and even taking of screenshot. The live layout feature will save you many hours of work.
Templates
Android Studio comes with a number of templates that you can use to quickly set up a basic Android project. These templates automatically create an Activity and the necessary XML files to speed up development.
Android Lint
Android Studio includes a analyzer program called Android Lint that analyzes your Android project to detect potential defects. This will help to improve the performance and the overall stability of your app.
Using SDK tools without the Android Studio
You can launch SDK Manager, AVD Manager, or even an AVD as a stand-alone program from a terminal, thus saving your computer resources. To do this, open a terminal, navigate to the tools directory which is located insides the sdk directory of the Andriod Studio directory (as shown in Figure 4), then:
-
To launch SDK Manager, execute
android sdk
An illustration on Windows is shown in Figure 24:
|
Figure 24: Launch SDK Manager
|
-
To launch SVD Manager, execute
android avd
An illustration on Windows is shown in Figure 25:
|
Figure 25: Launch AVD Manager
|
-
To launch an AVD named "AVD-Nexus7", execute
emulator -avd AVD-nexus7
An illustration on Windows is shown in Figure 24:
|
Figure 26: Launch an AVD
|
Managing AVDs Intelligently
Running AVDs can take up a substantial amount of your computing resources in terms of memory and CPU time, so be able to managing them intelligently is crucial. Heed these:
-
Do not over-allocate resources to each AVD lest it slows down your computer unnecessarily, you can find the optimal configurations by trials and errors.
-
As the launching of an AVD takes a while, when you need it in your work, let it be the first to start up and the last to shut down.
Updating Android Studio
As Android Studio is currently undergoing development, there will be updates released periodically. You can check for availability of updates from within Android Studio, select Help > Check for updates... on Windows, or Android Studio > Check for updates... on Mac, to see whether an update is available. (Figure 27)
|
Figure 27: Update Info
|
Keyboard Shortcuts
You may want to accustom yourself to using the various keyboard shortcuts provided by Andriod Studio to speed up your development work. Shortcuts for commoner operations in Windows and Linux are listed in Tables 3 and 4 for quick reference. For a complete keymap reference guide on all platforms, visit IntelliJ IDEA.
Table 3: Editing Shorcuts Action | Shorcut |
Ctrl + Space | Basic code completion (the name of any class, method or variable) |
Ctrl + Shift + Space | Smart code completion (filters the list of methods and variables by expected type) |
Ctrl + Shift + Enter | Complete statement |
Ctrl + P | Parameter info for method |
Ctrl + Q | Quick documentation lookup |
Ctrl + Alt + L | Reformat code |
Ctrl + Alt + O | Optimize imports |
Ctrl + Alt + I | Auto-indent line |
Table 4: Project Shorcuts Action | Shorcut |
Ctrl + F9 | Make project |
Shift + F9 | Debug |
Shift + F10 | Run |
Taking Stock...
We have finally arrived at the second rest stop. This journey saw you picking up the various tools needed for Andriod development, and putting them together to set up an Android development environment using Android Studio and creating your first AVD. Towards the end of the journey, you have also picked up some best practices that will guide you in developing better Android applications, as well as tips for using the Android Studio more effeciently and productively. What a eventful trip! We should take a rest now so that we can walk further and faster in our next journey. :zzz:
Reference