Android Foundation Note 2

Building Your First App

Creating Project

Using Android Studio to create an Android app is simple. There are a lot of setup you could do at the time when you creating the app. I usually just created a blank app to get started, but depends on the case, you could also use some other default settings when creating the project.

Some common used parameters and terms:

  • Application Name: Is the app name that appears to users.
  • Company Domain: a qualifier that will be appended the the package name.
  • Package name: a fully qualified name for the project. Must be unique.
  • Minimum SDK: I usually using API 8: Android 2.2 for my project, but that is an company requirement. In fact, I think make it begin from 2.3 is better.

There are couple of other options there when you start a new project. Again, just leave some fancy function like TV and Wear blank, and using the blank template.

You should have no problem to create a new app now. At the end of the process, IDE will ask you to create a Activity. Name it as you want.

IDE will also create a layout file for the activity, also some other setting up files. There are some files we need to pay attention.

Activities and Layout

Normally, each activity is binded with a layout xml file. Activity file is a Java file, and Layout file is a xml description file. You could reuse xml file for different activities or let one activity rendering different xml files at different situation. But general idea is layout xml file will taking care of most view layout stuff, and activity class will take care of logic part.

AndroidManifest

AndroidManifest.xml, the ultimate configuration file for your app. You will need to setting up permissions, register activities and services, and other configurations in this file. Before Android using gradle building system, this is basicly the only place to do the set up. When Google introduced gradle system, some parts of the configurations, like version info and SDK requirement has been moved to the gradle configuration.

build.gradle

build.gradle. This the the gradle compile configuration file. Gradle is a very nice building system, and at least for me, it is much easier to use than Ant. Do be careful about the path of the build.gradle file. If you have only one app in your project, you should see two build.gradle file. One for your app and one for your project. And if you have some other support projects and libraries, you may see more than 2 build.gradle files. Do be careful when you edited them.

There are a lot about gradle building services. I will go detail when we comes to that part. For now, there are couples of worth to mention parameters in this file:

  • compiledSdkVersion: This is the version of SDK which you will use to compile your app. The idea is always setting this to the lastest version of SDK.
  • minSdkVersion: This is the minimum SDK version your app will support. It is the version number you choose during create project phase. You could change it here at anytime.
  • targetSdkVersion: Indicates the highest version of Android with which you have tested your application. You should always test your app on the newest sdk and update this value.
  • applicationId: this is a fully qualified package name for your application that you specified during the New Project workflow.

Resources:

The /res subdirectories contains resources for your app. That includes images, values, layouts, menu settings, strings, colors and so on. We will go detail when we comes to the ‘Resources Overview’ part.

Create a Project with Command Line Tools

Using terminal is fun. But I admit, most time I use IDE’s button….

Still good to know these stuff, since once familiar with the usage of the CLT, it always faster and provides more details.

  1. Open terminal, and navigate to Android SDK path.
  2. Execute:
tools/android list targets

This should print out all available Android platforms that you have downloaded. Like this:

id: 12 or "Google Inc.:Google APIs (x86 System Image):19"
     Name: Google APIs (x86 System Image)
     Type: Add-On
     Vendor: Google Inc.
     Revision: 14
     Description: Android x86 + Google APIs
     Based on Android 4.4.2 (API level 19)
     Libraries:
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.media.effects (effects.jar)
          Collection of video effects
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
 Tag/ABIs : default/x86
----------
id: 13 or "Google Inc.:Google APIs:21"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 1
     Description: Android + Google APIs
     Based on Android 5.0.1 (API level 21)
     Libraries:
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.media.effects (effects.jar)
          Collection of video effects
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in, AndroidWearRound, AndroidWearSquare, AndroidWearRound, AndroidWearSquare
 Tag/ABIs : google_apis/x86
----------
id: 14 or "Google Inc.:Google APIs:22"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 1
     Description: Android + Google APIs
     Based on Android 5.1.1 (API level 22)
     Libraries:
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.media.effects (effects.jar)
          Collection of video effects
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in, AndroidWearRound, AndroidWearRound400x400, AndroidWearRoundChin320x290, AndroidWearSquare, AndroidWearSquare320x320, AndroidWearRound, AndroidWearRound400x400, AndroidWearRoundChin320x290, AndroidWearSquare, AndroidWearSquare320x320
 Tag/ABIs : google_apis/armeabi-v7a, google_apis/x86, google_apis/x86_64

Make a note of target ID of the version of SDK you want to use to compile your project, for example, if I want to use SDK 22 to compile my app, I will take note id is 14

  1. Execute:
android create project --target <target-id> --name MyFirstApp \
--path <path-to-workspace>/MyFirstApp --activity MyActivity \
--package com.example.myfirstapp

Replace with ID you have taken note from, and with the location which you want to save your project.

Running Your App

About Emulator

We need to talk a little about Emulator. Since we have too many Android devices available on the market, and it is impossible to purchases of of them for testing. The Emulator is definitely required for testing and development.

Unfortunately, the official Android emulator is way to slow to use. Its a pain on the ass when testing using that emulator, especially on AMD machines which don’t have Intel Virtual support.

Luckily, someone developed a much better emulator for Android testing, called Genymotion. You could download it from here:
https://www.genymotion.com/
The free version should be good enough for regular development. If you need more function , you could consider their paid version.

About Real Devices

When you connected and Android devices, phone or tablet, to your computer, there is a chance that it won’t be recognized by IDE, which forbidden your to run your app on it. Check these to solve the problem:

  1. Open developer options on phone’s setting. How to open it? Well different devices has different ways. One way I know for Samsung Galaxy is that you need to god to Settings -> More -> About device. And touch Build number seven times to open. I’m not joking. click that row seven times, then there will be developer option available to you.
  2. In developer option, with USB connected to computer. revoke USB permission and turn it back on. When ask to trust click yes. It solves 90% of problems.
  3. If you are using Samsung, there is a chance you need to install a special software to let the phone woking. Called Kies: http://www.samsung.com/ca/support/usefulsoftware/KIES/
  4. It is possible that other brands also ask you to install special driver to connect.
  5. If still can’t solve the problem, just google the device type with keyword ‘usb connection’ and good luck.

Run the App

From IDE

Running your app from IDE is simple. Assume you have no bugs and compile stopper errors, you should just click that launch button. If everything goes well, it should popup a dialog (You could change this behavior at IDE’s preference ), and select the devices on the list, click ok and your app will be installed and running on either real device or emulator.

From Command line

  1. Open terminal and navigate to the root of your project directory.
  2. On Mac/Linux:
chmod +x gradlew
./gradlew assembleDebug
  1. If succeeded, you will find apk file under /app/build/outpus/apk
  2. Execute:
adb install app/build/outputs/MyFirstApp-debug.apk
  1. Run:
adb install app/build/outputs/MyFirstApp-debug.apk

 

Leave a Reply

Your email address will not be published. Required fields are marked *