Enter the mobile world of Android and build your first app The mobile space is a promising one, and Android is an exciting platform for developers looking to break into it. This series begins with an introduction to Android, tailored specifically for Java™ developers looking to get into the mobile space. We'll learn about Android's three-tiered versioning system (and see why it's important), then set up your development environment and build your first Android application. About this series : Mobile app releases are exploding, and mobile development technology is here to stay. This series of articles will introduce developers who have programming experience and are new to the mobile space to what's happening in this space. This series starts with writing native apps in Java code, then expands your toolbox to include JVM languages, scripting frameworks, HTML5/CSS/JavaScript, third-party tools, and more. You'll gradually gain the skills you need to meet the needs of almost any mobile development scenario. The recent surge in mobile technology has surprised even industry insiders. Apple CEO Tim Cook expressed this in a September 2012 interview while discussing the success of the iPad. Selling 67 million iPads in just two years is an incredible feat. Cook compared the number to historical sales, saying, “This is equivalent to 24 years of Mac sales, five years of iPod sales, and three years of iPhone sales.” If iOS has been a huge success, then the Java-centric Android platform has also performed well. According to a recent statement from Google, 500 million Android devices have been activated since Android was officially released in 2008, with about 1.3 million devices activated every day. Apple's iTunes App Store and Google Play reportedly have at least 1.4 million applications and more than 60 billion mobile applications downloaded in total. The activation of all these mobile devices means new app sales opportunities and a larger market for mobile development technologies. Android is a great place to start for developers who want to quickly build mobile skills. If you can write Java code and other JVM languages, you can focus your initial learning curve on mobile deployment needs. Android's versatility means you can easily adopt and try out some of the newer technologies, such as HTML5 and PhoneGap. And the ubiquity of the platform (with Google Play) will ensure that your production apps can be widely distributed. This first article in our Mobile for the Masses series will provide a brief introduction to Android. We'll start by learning about Android's very sophisticated versioning system, then set up a development environment and start building your first application. Android boom Android devices are being distributed faster than any other mobile platform, making Android a great choice for your first mobile development journey, especially if you are a Java developer. One downside to Android's mass adoption is storage fragmentation, which creates a huge challenge for testing applications. The harsh reality for most developers is that it is impossible to test applications for all combinations of Android devices and OS version profiles. (In fact, testing in a cost-effective manner is becoming a burgeoning service industry.) Subsequent articles will explore some of the technical issues, including porting to multiple Android profiles and Java-based tools for testing Android mobile applications. This article will look at the version control aspects of Android, which you can use to develop for the platform. Three Paths into Mobile: At a high level, the mobile ecosystem has three main development platforms: native, web, and hybrid.
This series of articles will discuss these three platforms. Android version control Android OS version numbers are sequential, such as 1.6, 2.1, 4.2, and so on. Versions are named after dessert menus: Donut, Eclair, and Jelly Bean. When someone refers to Jelly Bean in the context of Android, they are referring to Android version 4.2. Android versions can also be described in terms of API levels, which are sequentially expressed as ascending integers. So Android API level 17 refers to Android version 4.2, which is Jelly Bean. If you find these names confusing, you are not alone. However, it is important to understand them because the three versions are often used interchangeably. Most Java developers have to completely change their previous thinking when it comes to supporting different Android versions. The Java platform is considered a server-side technology, and users usually interact with this technology through web pages. If the web page is browsed, then the visitor of the web page does not care which Java version your application is running. However, with Android, this means that the application leaves the protection of the server and runs on a device that you do not control. Many potential users of your app will upgrade to a new version of the Android OS only after their old devices stop working. It's important to know which Android OS you support. Although the latest Android version is 4.2, almost half of all Android devices still support Android 2.3.x. An app written for Android 3.1 will work on newer versions like 4.2, but not on older versions like 2.3. So it stands to reason that if you want to achieve wider distribution, your best option is to target an older OS. One of the downsides of a more widely released platform that targets older systems is that new versions always introduce new features that you might want to use in your app. If you're lucky, the Android development team will release a compatibility library that lets you use these new features on older versions. Create and run applications using Android and Eclipse ADT In this introduction to Android, I'll help you create a simple Android application as quickly as possible. You'll use Android 4.2 and the Eclipse Android Development Tools (ADT) plug-in to create a Java-based Hello World application. Android-compatible IDE: When developing mobile applications for Android, you can use the Java tools and frameworks that you are already familiar with. For example, the two most popular Java development IDEs are Eclipse and IntelliJ, both of which support Android. Android is closely tied to Eclipse, which provides plug-ins that make it easy to implement the "build-test-deploy" cycle of Android applications. The examples in this article were built using Eclipse, but you can also build it using IntelliJ. There are two ways to set up a development environment for Android. Developers who are new to Eclipse and Android can start by downloading the ADT (Android Developer Tools) Bundle. The ADT Bundle bundles an Android SDK and a version of Eclipse with the ADT plug-in already installed. This package helps you use Android quickly and easily. If Eclipse is already installed and set up on your desktop, manually download and install the Android SDK. The base Android SDK contains some system tools that simplify Android development, but it lacks the Android libraries required to develop for specific Android versions. After downloading the base Android SDK for your operating system, access the tools directory of the SDK installation directory and run the Android script in Listing 1 . Listing 1. Launching the Android SDK Manager
This script launches the Android SDK Manager, which lets you download various versions and libraries of Android and create emulators or virtual device instances. The Android SDK Manager opens a window that displays a long list of options to download, as shown in Figure 1 . Figure 1. SDK Manager In the checkbox on the left, select Tools, then select Android 4.2 (API 17). The folder will display many options. Select SDK Platform and the ARM EABI v7a System image, as shown in Figure 2 . Figure 2. Selections in the SDK Manager Click Install packages and accept any necessary permissions; the manager will complete the rest. Creating a Simulator Next, create an emulator instance, which is a virtual device that runs your application. The emulator must target a specific version of Android, so we'll create an emulator instance running Android 4.2. Restart the SDK Manager if it is closed. If the Android SDK Tools or Platform Tools have been updated, restart the SDK Manager. In the SDK Manager top-level menu, select Tools > Manage AVDs. A new dialog box for the Android Virtual Device Manager opens, as shown in Figure 3 . Figure 3. Android Virtual Device Manager This dialog lists all the emulators defined on your machine. The list of emulators is empty until you create an emulator. Click New to open the Create a new Android Virtual Device (AVD) dialog, where you can define an emulator. Select Android version 4.2 in the Target drop-down list. In the AVD Name folder, give the emulator a name; I used Android_4.2. When you are ready to test your application, you can use this name to start the emulator from the command line. Figure 4. Creating a new Android virtual device In Figure 4 , you can see some AVD options, including device type, camera settings, memory, and storage. In my example, I selected the Galaxy Nexus and used a 200MB SD card for it. Finally, I checked the Use Host GPU emulation option, which can improve the performance of the emulator instance. Click OK to see your new virtual device listed in the AVD dialog. Select the device and click Start, which will open a dialog with launch options. For now, use the defaults and click Launch. The emulator instance will take a while to start, so go grab a cup of coffee, check Facebook, or update your Twitter status. Figure 5. New Android device! After a few moments, a shiny emulator device (similar to Figure 5 ) will appear on your home screen, which means you can start creating Android 4.2 applications. #p# Setting up Eclipse ADT If Eclipse is already installed on your system, you must add the Android Development Tools (ADT) plugin repository before continuing. In the Eclipse menu, open Help > Find New Software. Click Add and the URL for the ADT plug-in: https://dl-ssl.google.com/android/eclipse/ (see Figure 6 ). Figure 6. Adding the ADT plugin library Once the plugin has downloaded, follow the instructions to complete the installation. Configure the ADT plugin to use the SDK you just installed. Your project is now set up for the Android 4.2 platform and uses the emulator you configured. Using Android 4.2! To create an Android application in Eclipse, select the option to create an Android Application Project. In the New Android Application dialog box, provide an application name, a project name, and a package name. The application name is what users see in the device icon; the project name is what your project is called in Eclipse. Figure 7. Creating a new Android project In the New Android Application dialog box shown in Figure 7 , perform the following steps:
If the project is created successfully, you will see a new project with a user interface similar to the one shown in Figure 8 . Figure 8. Sample startup project in Eclipse Run the application in the Android emulator By default, an Android project created using the ADT project wizard creates a simple Hello World mobile application (see Figure 8 ). Before we start embellishing this application, let's take a look at what happens when we run this basic application on the emulator. If the Android 4.2 emulator is off, open it. This time, instead of launching the emulator in the AVD Manager, you’ll launch it from the command line. You can access the emulator script from the tools directory of the SDK. When you launch the script, you’ll be asked to provide a name for your emulator. My emulator is named Android_4.2, so I run the script in Listing 2 to launch my emulator instance: Listing 2. Accessing the simulator from the command line
Start your emulator instance, return to Eclipse, and select your project. Right-click and select Run As > Android Application. Figure 9. Running the project from Eclipse By default, Eclipse will deploy your application on the currently running emulator ( Figure 9 ). Your emulator instance now displays a white screen with the greeting message that all Java developers love! If Eclipse fails to run your application on the emulator for some reason, you will need to return to the Run As option and select Run Configurations. In the Target tab, reselect Automatically select compatible device and make sure your emulator is selected as well. Figure 10. Hello World! #p# Basic Android Application You now have a basic Hello World application that can run on any 4.2-compatible Android device. Eventually, you will probably want to customize this application to add more functionality. For now, I want to profile the Android application. Android's Specialized JVM: Android applications are written in Java code, but we don't run them on a standard JVM. As part of the Android build, the Java byte code is converted and specially processed for the memory and processing needs of mobile devices. Learn more about the architecture of Android applications and the Dalvik virtual machine. Return to the Eclipse IDE. In the project's src directory, navigate to the project's .java file. If you left all the defaults, the file should be named MainActivity. The Activity class is the driving engine of an Android mobile application, similar to what servlets are to Java server-side applications. Each Activity has a life cycle, similar to the life cycle of a Java applet. The Hello World application contains only basic architecture, so its Activity is very simple. First, the class will extend from Android's Activity class. The entry point of the activity is the onCreate method, which is launched by the platform when the application loads. The key code in this method is the setContentView call. Listing 3 shows the default MainActivity class. Listing 3. MainActivity
In Listing 3 , setContentView loads the activity_main layout, which is the name of the view you see in Eclipse when you first open the project. This name corresponds to the activity_main.xml file in the res/layout directory. Open that file to see a graphical representation of the application. In the tab at the bottom left of the view, there is an option to view the raw XML file, which you can click to view the raw file, which looks like Listing 4 . Listing 4. Original layout definition
Android views are defined in XML files. Each view has a layout to which you can add widgets, such as buttons or text fields. Currently, the sample application has a RelativeLayout and a single widget: TextView. If you look closely at the widget, you'll find a text field that is set to a reference: @string/hello_world. This variable is a pointer to the actual string, which you can find in the res/values/strings.xml file. Open that file. To see the raw XML, click the strings.xml tab in the lower left area. Listing 5. strings.xml file
The second string definition in Listing 5 maps hello_world to Hello World!. Android manifest You have now seen how the three important elements of an Android application are connected: behavior, layout, and resource files. When the application starts, the Android OS calls the onCreate method of MainActivity. The onCreate method then loads the layout, which gets some values from another XML resource file. In this case, the result is a simple UI that displays "Hello World!". But how does the device know which Activity to call? This is where AndroidManifest.xml comes into play. Look for this file in the root directory of your project. Click on it, and Eclipse will load a neat custom view with some tabs in the lower left corner. Clicking on the tab labeled AndroidManifest.xml will display the raw XML shown in Listing 6 . Listing 6. AndroidManifest.xml
The purpose of this file is to explain your application to the underlying device. It contains an XML block called application where activities are defined. Your MainActivity is referenced, and actions are defined and contain a category. The action tells the device which activity to start. In this case, action describes your activity as MAIN. Other important declarations in the AndroidManifest.xml file include the uses-sdk section, which indicates that the min version is 8 (that is, Android 2.2.x) and the target version is 17, which is Android 4.2. I will introduce these details in a later article. For now, it is enough to understand that the Android platform provides libraries that are compatible with earlier versions. Conclusion You have successfully created your first Android application. In the process, you learned some important things about Android (and mobile development). You installed the Android SDK, downloaded Android version 4.2, and created an AVD (or emulator) to simulate running your application on a device. If you are using Eclipse, you need to configure the IDE to build and deploy your Android application to the selected AVD. All of this setup is necessary to create an application in Android. You also saw how easy it is to wire up an Android application, doing most of the work with the Activity class, which is defined for the underlying device using AndroidManifest.xml. You saw the end result: a simple Hello World application, and went on to customize it. We'll dive deeper into Android in the next article. In the meantime, I recommend that you play around with the application setup you've completed so far. Add a few widgets or modify some values in the XML file. If you're still feeling unsatisfied, try creating a new Hello World application without the defaults. |
<<: 2014 Mobile Platform Summit Forum held in Beijing
>>: Android Wear software review: Not bad for smartphone companion software
Addendum 1: Because I have to queue up for nuclei...
"Seeing a leopard through a bamboo tube"...
According to foreign media reports, US District J...
According to Reuters, Volkswagen's labor-mana...
Tianjin Eco-City, a green new city growing out of...
70 years since the founding of New China Electron...
With the release of the movie "Jurassic Worl...
Newton once said: " If I have seen further t...
In August 2011, Steve Jobs, then CEO of Apple, an...
Chinese name: Fraud game/Scam game Foreign name: ...
It’s so noisy! This is probably our most direct a...
This article takes the five major mainstream plat...
Recently, a message was posted on social media pl...
According to industry insiders, mini programs wil...
The method of selling goods through live streamin...