Pages

Saturday, May 24, 2014

1. Welcome to Android Studio

In this first tutorial we will do the traditional thing and write a "Hello World" Android app. This is very simple with Android Studio and doesn't require any actual coding since the default app says Hello World. Nevertheless, it does demonstrate how to build an Android Project from scratch and understand the options in the New Project Wizard.

Figure 1. Welcome to Android Studio

When you have installed and launched Android Studio, the first screen that you will see is shown in Figure 1. Clicking on "New Project" will start the New Project Wizard (Figure 2).

Figure 2. New Project Wizard

2. The New Project Wizard

The New Project Wizard has the following options:

Application Name - This is the title of your application and what is shown in the Google Play store. It is also what is shown in the Manage Applications list in Settings. We have imaginatively used Tutorial1 for this first app. This name is stored within the strings.xml file under the variable "app_name" should you need to use it in your project.

Module Name - The module name is only used within the Android Studio Integrated Development Environment (IDE). Some quick terminology, a project in Android Studio represents a complete Android app. Android Studio projects consist of one or more modules. A module is a component of your app that you can build, test, or debug independently. We only have one module in our app so just use the application name.

Package Name - The package name is the unique identifier of your application. To ensure that it is unique, Android recommend that you use the reverse domain name of your organisation with an application identifier (e.g. au.com.reefwing.tutorial1). It must be a valid Java package name, note the use of lower case for the application name. Users will not normally see the package name but it is associated with your app for its lifetime. It is how multiple versions of your app are considered the same.

Project Location - Where the app source and build files will be located. This doesn't need to be anywhere special but for ease of finding and backup we put all of our projects in a directory called AndroidStudioProjects.

Minimum Required SDK: This is the earliest version of Android that your application will support. The default is API 7: Android 2.1 (Eclair). Your choice is a tradeoff of supported devices vs new language features. According to Android, targeting API 8 or later will reach approximately 95% of the market. To get access to Ads using Google's Admob you will need at least API 8. For this app, we are not interested in supporting legacy devices so we will select the latest API 19: Android 4.4 (Kit Kat).

Target SDK - This is the highest API that the application is known to work with. If you do not target the current version then your app could look dated. The latest version currently is API 19: Android 4.4 (Kit Kat).

Compile With - The target API that you want to compile your code against. Typically this is the most recent version or the first version that supports all the API's that you want to access. As before we will select API 19.

Language Level - Selects what Java language level you want to use. KitKat supports full Java 7, Gingerbread and up support Java 6 and older versions use Java 5, at least when it comes to the core Java API's. The wizard defaults to 6.0 which isn't a bad option, although Java 7 did introduce some nice new features, including:

Strings in switch

Binary literals and underscores in numeric literals

Multi-catch and more precise rethrow

Improved type inference for generic instance creation (diamond)

try-with-resources statement

3. Android App Themes

Figure 3. Holo Dark & Holo Light themes

The final option is the Theme. Themes are Android's mechanism for applying a consistent style to an app or activity. The style specifies the visual properties of the elements that make up your user interface, such as colour, height, padding and font size. To promote greater cohesion between all apps on the platform, Android provides two system themes that you can choose from when building apps:

Holo Light

Holo Dark

Applying these themes will go a long way in helping you to build apps that fit right into the general visual language of Android. Pick the system theme that best matches the needs and design aesthetics for your app. If your desire is to have a more distinct look for your app, using one of the system themes as a starting point for your customisations is a good idea. The system themes provide a solid foundation on top of which you can selectively implement your own visual stylings.

For this first app, select the default, Holo Light with Dark Action Bar. Click the Next button (bottom right of the Wizard screen) and the Select Icon (Figure 4) page is shown. For our first app we will just use the default icon provided.

Figure 4. Select Icon

4. Activity Template

Click Next on the Select Icon page to bring up the Activity Template shown in Figure 5.

Figure 5. Activity Template

Selecting the Blank Activity and clicking Next takes us to the Template Detail form (Figure 6). You can keep these all as default if you wish. Note that from Android Studio 0.3, a Fragment Layout is added by default.

Figure 6. Template Detail

Clicking Finish will generate our Hello World app. The first project build will take a while as Android Studio downloads Gradle. You will need internet access at this point. The Gradle download only happens on the first build. We will discuss Gradle further in a separate tutorial.

Once the project has completed its build, the Android Studio IDE will open up and should look something like Figure 7.

Figure 7. Android Studio IDE with Hello World App.

5. The Emulator

At this point you would probably like to run your app in the emulator or on a device. To do this, attach the device if you are going to use an actual phone or tablet for development and click on the green run button in the top tool bar (next to the drop down box with the app name - it looks like a play icon). This will compile your app, build it with Gradle and display the screen shown in Figure 8.

Figure 8.

If you have already configured a virtual device or have a real device connected to your development computer then you can select it and click Ok. We have a virtual device configured as a Nexus 7 (since this is our development tablet). Eventually, the emulator will launch your app and you should see the result shown in Figure 9. If this is the first time that you have launched the Emulator you may need to click the run button again after the Emulator has launched.

Figure 9. Emulator running Hello World app.

The first thing that you will notice is how slow the emulator is to launch a newly compiled app. For development purposes an actual device is MUCH quicker and is a good investment regardless. If you haven't set up a virtual device then we will show you how in the next tutorial.

We are using the Developer Preview (version 0.4.0) of Android Studio at the time of writing. Installation details may change so visit the Android Studio site to check out the latest instructions and to download the relevant setup program.

From the Android Studio site:

Android Studio requires JDK 6 or greater (JRE alone is not sufficient). To check if you have JDK installed on a Mac (and which version), open a terminal and type javac -version. If JDK is not available or the version is lower than 6, download JDK from here.

To install Android Studio:

Download the Android Studio package from above.

Install Android Studio and the SDK tools:

Windows:

Launch the downloaded EXE file, android-studio-bundle-<version>.exe.

Follow the setup wizard to install Android Studio.

Known issue: On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.

Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder, for exampleC:\Program Files\Java\jdk1.7.0_21.

Mac OS X:

Open the downloaded DMG file, android-studio-bundle-<version>.dmg.

Drag and drop Android Studio into the Applications folder.

Known issue: Depending on your security settings, when you attempt to open Android Studio, you might see a warning that says the package is damaged and should be moved to the trash. If this happens, go to System Preferences > Security & Privacy and under Allow applications downloaded from, select Anywhere. Then open Android Studio again.

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 may want to add android-studio/bin/ to your PATH environmental variable so that you can start Android Studio from any directory.

That's it! You're ready to start developing apps with Android Studio.

Note: On Windows and Mac, the individual tools and other SDK packages are saved within the Android Studio application directory. To access the tools directly, use a terminal to navigate into the application and locate the sdk/directory. For example:

Smart editing includes inline resource lookups that make it easier to read your code. Code refactoring allows you to transform your code across the scope of the entire project.

Static code analysis helps you identify bugs quickly. On top of the hundreds of code inspections that IntelliJ IDEA provides, Google have added custom inspections. For example, metadata to the Android APIs, that flag which methods can return null and which can’t, which constants are allowed for which methods, and so on. Android Studio uses that data to analyze your code and find potential errors.

Be aware that Android Studio is currently available as an early access preview. Several features are either incomplete or not yet implemented and you may encounter bugs. If you are not comfortable using an unfinished product, you may want to instead download (or continue to use) the ADT Bundle (Eclipse with the ADT Plugin).

About the Author

David is an independent software developer located in Sydney, Australia. He has been coding for over 30 years and is very experienced in c, c#. c++, objective c, Java, Python and Lua. David also works with embedded systems, particularly Arduino & Raspberry Pi based applications and is an experienced trainer.

David's software development company, Reefwing Software, has released a number of top selling apps including Life Goals and Personality Profile.

If you would like an app developed or training in embedded systems then you can contact David Such at the Reefwing Software website (www.reefwing.com.au).

David has completed an MBA (Marketing), BE (Electrical), BSc (Computing Science & Physics), and a BAppSc (Wine Science).