01. Android Technology

  • Description: General overview of the Android OS, layers and tools
  • Practical part: Android tools, emulators, ADB, Beacon Mountain

Lecture

Practical

Resources

Files

Task 0 - Setting up the environment

This section is meant you help you set up your environment at home. All links are found in the resources.

There are two ways of setting up your environment. The first way is using Intel's Beacon Mountain for Android solution. This sets up your whole environment including SDK and NDK and contains some additional profiling tools and HAXM, needed for virtualizing Intel Atom based virtual devices. Run the installer and follow the steps.

The second way will set up the bare minimum needed for creating an NDK project.

  1. Download the Android SDK and NDK and extract them in convenient locations
  2. Install Eclipse
  3. Install the ADT plugin for Eclipse (see tutorial in link provided), provide the location of the SDK when asked
  4. Setup up the Android SDK: Open the SDK Manager by clicking Window > Android SDK Manager on Eclipse's menu bar.
  5. Select the latest versions of Android SDK Tools, Android SDK Platform-tools, Android SDK Build-tools
  6. Select everything in Android 4.4.2 (API Level 19) through Android 4.2.2 (API Level 17) (you can skip the sources if you want)
  7. For x86 virtualization support on Windows, install Intel HAXM on Debian based systems you need to install the following packages: qemu-kvm, libvirt-bin, bridge-utils and ubuntu-vm-builder or debian-vm-builder

Task 1 - Creating and running a virtual device

Create a virtual device in Eclipse:

  1. Click Window > Android Virtual Device Manager from Eclipse's menu bar
  2. Select New
  3. Name your device
  4. Select a device from the list: these are predetermined settings for resolution, available RAM and VM Heap (Maximum Dalvik Heap). You can define your device in Device Definitions tab, in the Android Virtual Device Manager window, but it's not required.
  5. Give the device 512-1024 MBs of RAM and 64 MB VM Heap
  6. Select Android 4.4.2 as the target
  7. Select Intel Atom x86 under CPU/ABI
  8. Give the device and 100 MB SD Card
  9. You can select Use Host GPU

On Windows, if HAXM service is started, Android Virtual Devices will run virtualized.

On Linux you either set it up in Eclipse, or a per project basis:

  1. Click Run > Run configurations from Eclipse's menu bar
  2. Under Android Application select your project
  3. Select the Target tab
  4. In the Emulator launch parameters pane, add as additional parameters: -qemu -enable-kvm

Or you can start the device using the following command:

emulator64-x86 -avd <device_name> -gpu on -qemu -enable-kvm

If you receive an error related to OpenGLES emulation library you need to add the location of the library to the LD_LIBRARY_PATH variable. The library is located in SDK_DIRECTORY/tools/lib/. Or you can remove the -gpu on parameter if your application does not require complex graphical features.

Task 2 - Use ADB to transfer files and install apps

After the device boots, you can connect to it using ADB. ADB can be used from a terminal on all operating systems. To get a list of connected devices use can use:

adb devices

To get a shell on the connected device use:

adb shell

Android devices normally come with a more limited set of commands, so a text editor might not be available.

Create a text file on your machine. Copy it to the connected device using:

adb push <local> <remote>

The file should be copied to the /sdcard/ partition on the phone. Connect to the device using adb shell, and use cat to check the contents of the file. To copy from the device use:

adb pull <remote> [<local>]

Both push and pull can be used with both directories and files.

To install an application through adb use:

adb install <file>

Install LogLevel.apk (in the laboratory archive).

To check all available options of the ADB use:

adb --help

Task 3 - Create and run an application

Create a new Android Project in Eclipse (File > New Android Application). Choose a name and leave other options as they are. On the next screen make sure Create activity is checked and Mark project as library is not. Create an icon if you checked the option, and then choose Blank Activity when prompted to.

This creates an application with a single activity.

Optional: If using Linux, set qemu parameters to enable virtualization like in Task 1.

Run the project. Eclipse will prompt you to choose a device. Select the device created previously. This will launch the emulator, if it is not already started, and after it boots up the new application should start as well.

Task 4 - Add a button

The MainActivity.java file should be displayed. If not, open it using the Package Explorer pane. You will find it in the src folder.

The Activity should be very simple. It has an onCreate method which gets called when the Activity gets started and an onCreateOptionsMenu method which creates the dropdown menu.

In the onCreate method the Activity calls

setContentView(R.layout.activity_main);

This sets the layout for the Activity. R is a class generated by the SDK and it maps every resource to a number. It contains subclasses for every folder found in the res folder, in this case layout. Open the res/layout/activity_main.xml file. You will see the layout editor. In the Palette pane, find the button, and drag it to the activity. You will see the Outline pane to the right of the screen change to reflect this. Select the button and check its id field in the Properties panel, under the list of UI elements. Change the text of the button to “Press me!”. Also check the id field of the TextView element.

Go back to MainActivity.java.

Add the following code to the onCreate method. Make sure the ids match the ones in your layout.

        final Button b1 = (Button) findViewById(R.id.button1);
        final TextView tv1 = (TextView) findViewById(R.id.textView1);
 
        b1.setOnClickListener(new OnClickListener() {
        	int n = 1;
 
		@Override
		public void onClick(View v) {
			tv1.setText("Hello again! " + Integer.toString(n++));
		}
	});

This gets the references to both the Button you added and the TextView that was already declared. They are declared final so they can be visible inside the new OnClickListener. The OnClickListener objects contains an onClick method which gets executed when the button is pressed. In this case, it updates the TextView to reflect the number of clicks on the button.

Task 5 - Use logcat for debugging

Start the application installed in Task 2. Make sure the Logcat tab is the one selected in the panel at the bottom. Type any text in the box and then click one of the buttons. A new line containing your text should appear in the Logcat. Depending on what button you press, the line colour will vary. It is also possible to filter these log entries: above the logs themselves there is a text entry input which allows textual filtering. It allows you to limit scope by prefixing searches with pid:, tag:, app: or text: and it accepts Java-style regular expressions. To the right of this text field there is a dropdown which allows you to select verbosity. Choosing a level will only allow messages of that level or higher to be displayed.

Import the LogLevel project into Eclipse (File > Import and select General > Existing project into workspace). Look over the code. Each button has a method attached which calls a static method from the Log class. These methods generate log entries on different levels of importance: d=debug, i=info, w=warning, e=error.

Add another two buttons to the application: one that generates an assert level log entry, and one that repeats the previously generated log entry, but with a higher level of importance, except in the case of assert, which will remain the same.

Task 6 - View weight

Use the LogLevel project as a start. Notice that the EditText box only takes up one line of the screen and there should be some screen space left at the bottom of the screen. Make the EditText box take up all available space. To do this, you should set the weight parameter of the EditText view to 1 (or any integer).

The weight parameter is used to distribute empty screen space to the elements which need it. First, the layout builder will place all the elements for which the size is easy to determine. Then, the empty space will be distributed to the elements with weight, based on their weight: if the total weight of all elements is 4, an element with a weight of 1 will get 1/4 of the empty screen space, an element with 2 weight will get 2/4, and so on.

Task 7 - Multiple resources

In Android you can have multiple resources with the same ID. This allows building a single application for all the languages, screen resolutions or formats, and have the system select the correct resource when running the application.

In the LogLevel project, notice that there is a single layout folder. Modify this layout to fit two screen densities: mdpi and hdpi. To do this, rename the folder layout to layout-hdpi and make a copy of it named layout-mdpi. This will allow the system to select the mdpi layout for systems with medium dpi or lower, and the hdpi layout for the rest. Modify the mdpi layout to display the buttons on two columns instead of a single button on each row. Use multiple horizontal LinearLayouts.

ndk/courses/01.txt · Last modified: 2014/11/05 11:54 by laura.gheorghe
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0