Android Hands-On Labs



Similar documents
Introduction to Native Android Development with NDK

Qualcomm IR-I 2 C Bridge Demo

Development_Setting. Step I: Create an Android Project

RTI Connext DDS. Core Libraries Getting Started Guide Addendum for Android Systems Version 5.2.0

OpenCV on Android Platforms

The "Eclipse Classic" version is recommended. Otherwise, a Java or RCP version of Eclipse is recommended.

Running a Program on an AVD

Yocto Project Eclipse plug-in and Developer Tools Hands-on Lab

How To Develop Android On Your Computer Or Tablet Or Phone

AllJoyn Android Environment Setup Guide

Android Environment SDK

Lab 0 (Setting up your Development Environment) Week 1

Multithreading and Java Native Interface (JNI)!

Board also Supports MicroBridge

Building graphic-rich and better performing native applications. Pro. Android C++ with the NDK. Onur Cinar

Android Programming and Security

Q N X S O F T W A R E D E V E L O P M E N T P L A T F O R M v Steps to Developing a QNX Program Quickstart Guide

With a single download, the ADT Bundle includes everything you need to begin developing apps:

Native code in Android Quad Detection

How to Install Applications (APK Files) on Your Android Phone

Getting Started with Android Development

ERIKA Enterprise pre-built Virtual Machine

Android Setup Phase 2

Android Environment SDK

10 STEPS TO YOUR FIRST QNX PROGRAM. QUICKSTART GUIDE Second Edition

Developing In Eclipse, with ADT

NVIDIA Tegra Android Platform Support Pack Getting Started Guide

Tutorial on Basic Android Setup

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

ID TECH UniMag Android SDK User Manual

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Tutorial on OpenCV for Android Setup

Waspmote IDE. User Guide

Extending your Qt Android application using JNI

Procedure to Create and Duplicate Master LiveUSB Stick

How to develop your own app

Fahim Uddin 1. Java SDK

Setting up Sudoku example on Android Studio

SheevaPlug Development Kit README Rev. 1.2

What else can you do with Android? Inside Android. Chris Simmonds. Embedded Linux Conference Europe Copyright 2010, 2net Limited.

Chipsee Embedded Industrial Computer Android User Manual V1.0.1

Example Connection between USB Host and Android

USB HSPA Modem. User Manual

Advantages. manage port forwarding, set breakpoints, and view thread and process information directly

Android Development. 吳 俊 興 國 立 高 雄 大 學 資 訊 工 程 學 系

Developing for MSI Android Devices

Android Programming. Høgskolen i Telemark Telemark University College. Cuong Nguyen,

WA1826 Designing Cloud Computing Solutions. Classroom Setup Guide. Web Age Solutions Inc. Copyright Web Age Solutions Inc. 1

Chapter 1 Hardware and Software Introductions of pcduino

Reminders. Lab opens from today. Many students want to use the extra I/O pins on

Instructions for Installing and Using the FOCUS DL-15 Data Transfer Software

System Reference 2013

The embedded Linux quick start guide lab notes

Technical Note. TN_134 FTDI Android D2XX Driver

Android Development Tools for Eclipse

Developing an Application for the i.mx Devices on the Linux Platform

Silk Test Testing Mobile Applications

Android Tutorial. Larry Walters OOSE Fall 2011

Intel Do-It-Yourself Challenge Lab 2: Intel Galileo s Linux side Nicolas Vailliet

ADB (Android Debug Bridge): How it works?

Lab 1: Introduction to the network lab

ScanWin Installation and Windows 7-64 bit operating system

Intel Integrated Native Developer Experience (INDE): IDE Integration for Android*

Android Development. Lecture AD 0 Android SDK & Development Environment. Università degli Studi di Parma. Mobile Application Development

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Android Application Development: Hands- On. Dr. Jogesh K. Muppala

How to use the VMware Workstation / Player to create an ISaGRAF (Ver. 3.55) development environment?

IOIO for Android Beginners Guide Introduction

TomTom PRO 82xx PRO.connect developer guide

DS-5 ARM. Using the Debugger. Version 5.7. Copyright 2010, 2011 ARM. All rights reserved. ARM DUI 0446G (ID092311)

Android Development. Marc Mc Loughlin

Software Prerequisites Linux Ubuntu LTS. Estimated completion time: 15min. The goal of this hands-on is to:

Appium mobile test automation

file://d:\webs\touch-base.com\htdocs\documentation\androidplatformnotes52.htm

Introduction to Gear VR Development in Unity APPENDIX A: SETUP (WINDOWS 7/8)

PetaLinux SDK User Guide. Application Development Guide

Shearwater Research Dive Computer Software Manual

Installing the Android SDK

Android: How To. Thanks. Aman Nijhawan

XenMobile Logs Collection Guide

Cosmic Board for phycore AM335x System on Module and Carrier Board. Application Development User Manual

Android Environment Emulator

Wireless Mobile Broadband Setup Guide for Linux OS

-Android 2.3 is the most used version of Android on the market today with almost 60% of all Android devices running 2.3 Gingerbread -Winner of

Code::Block manual. for CS101x course. Department of Computer Science and Engineering Indian Institute of Technology - Bombay Mumbai

Future Technology Devices International Ltd. Mac OS-X Installation Guide

Università Degli Studi di Parma. Distributed Systems Group. Android Development. Lecture 1 Android SDK & Development Environment. Marco Picone

Smooks Dev Tools Reference Guide. Version: GA

How To Develop An Android App On An Android Device

Android-based Java Programming for Mobile Phone LED Control

Getting Started with Kinetis SDK (KSDK)

Android, Bluetooth and MIAC

Workshop Intel Galileo Board

Mobile Development Documentation

Colorfly Tablet Upgrade Guide

1. Downloading. 2. Installation and License Acquiring. Xilinx ISE Webpack + Project Setup Instructions

ML310 Creating a VxWorks BSP and System Image for the Base XPS Design

APPLICATION NOTE. How to build pylon applications for ARM

How to Create an Android Application using Eclipse on Windows 7

TEGRA X1 DEVELOPER TOOLS SEBASTIEN DOMINE, SR. DIRECTOR SW ENGINEERING

Transcription:

Android Hands-On Labs Introduction This document walks you through the 5 Android hands-on labs with i.mx53 Quick Start Board for today s session. The first part regards host preparation, which is not needed today as you are using a ready-to-go virtual machine with all dependencies, softwares and BSPs installed. The 5 labs will guide you through the process of downloading the available android BSP images to the SD card and running them on the board, then, they guide you over 4 necessary steps to build a new android application with access to GPIOs: BSP compilation with new kernel patch, creation of the graphical application with eclipse SDK, creation of C/C++ application to control the GPIO and debugging these steps. The last part integrates this new application into the image so you can build a custom BSP containing your new application. We hope you enjoy the labs presented and in case of any question, feel free to stop one of us during the presentation. Host Preparation (not needed today) Install the Linux Host on your PC, Android recommends Ubuntu 10.04 64-bits, download it at http://releases.ubuntu.com/lucid/. Install Android SDK (r20 is the latest at this time), NDK (r8b is the latest) and ADT (20.0.0): Just as recommendation, you can install these tools on the following folders: /home/[user]/software/ides/eclipse, /home/[user]/software/sdks/android-sdk-linux, /home/[user]/software/sdks/android-ndk-linux. To install SDK, follow instructions on developer.android.com/sdk/installing/index.html, in our case, Android 2.3.3 Gingerbread is being used as platform. Install Eclipse IDE and ADT plugin as described here: http://developer.android.com/sdk/installing/installing-adt.html. Then, install Android NDK, as decribed here: http://tools.android.com/recent/usingthendkplugin. Finnaly install Android BSP R4.3 from Adeneo, downloading it from Adeneo website: http://www.adeneo.com/products/board-support-packages/freescale-i.mx53-qsb.

You can unpack BSP content on your home folder. Follow document Release Notes r4.3.pdf in doc folder inside the BSP for: Installing prerequisites, download, patch and build Android. Also follow instructions on i.mx_android_r10.3_user_guide.html, chapter 5.9, How to setup PC (Windows) to support ADB/RNDIS For Linux PC in doc folder, in order to correctly setup USB to support ADB (Android Debug Bridge). Laboratories Please open the Virtual Machine. To do that, Start menu on Windows then find VirtualBox application. Inside Virtual Box, you will see a virtual machine called Ubuntu 10.04, double click-it to open. Virtual Machine user: freescale pass: fsl. Lab1 - Booting Gingerbread Android from Adeneo s pre-built images In this first lab, you will prepare an SD card to boot Android on the i.mx53 QSB. Further during the training, you will have a better understanding of the BSP and the AOSP (Android Open Source Project). For now, the goal is to have a first contact with the OS running in an ARM device, so no details are presented. Insert the SD card in the SD card reader and change to the Adeneo s BSP scripts directory. The BSP is installed under ~/imx53_training/android/i.mx53_...: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/scripts Run the script to install Android prebuilt images to the SD card: #./flash_prebuilt_android.sh [NAME OF YOUR SD CARD DEVICE (e.g. /dev/mmcblk0 or /dev/sdb)] Note: you can use dmesg command to see what /dev entry corresponds to your SD card reader. Make sure you have the SD card reader available inside the virtual machine before executing the flash command. After the script finishes flashing Android image to the SD card, removes the card from the reader and insert it into i.mx53 QSB micro SD card connector.

Connect an USB-Serial cable to your host machine USB port and to the serial port of the i.mx board. Open a new terminal inside the virtual machine and connect to the serial adapter using screen: # screen /dev/ttyusb0 115200n8 Note: you can use dmesg command to see what /dev entry corresponds to your USB-Serial adapter. To disconnect screen, use Ctrl+a, k, y. After all the setup has been done, plug the power supply to the i.mx53 QSB board. You should see the Android OS initializing on the terminal running screen. During the first boot of the device, you will need to calibrate the touch screen. Calibration screen will be presented before the system finishes the boot process. Note: in case you fail to calibrate the device, by marking an invalid point, for instance, you can remove the calibration file and reboot the board in order to calibrate the device again. Calibration file is under /data/system/calibration. Calibrate your touch and wait for Android to present you the home screen, it may take a couple of minutes when booting for the first time. You can play around with some of the native applications eventually. Lab2 - Building Gingerbread Android from Adeneo s BSP You will end this lab with a new Android image, that contains kernel support for i.mx53 QSB user LED control, compiled and burnt into your SD card. In order to enable IO control for the LED at kernel level, prior to compiling the new image, you need to apply a patch to the kernel source tree. We suggest that you take a look at the patch content before you apply it: # cd ~/imx53_training/android/labs/lab02 # cat add_leds_gpio.patch Check what the patch does: it enables the GPIO control on the linux kernel def config files and on the initialization file for the QSB board; it creates a platform-device structure to control one user LED on the board; and then it initializes the structure during execution of the init function. Now, to apply the patch, run commands below: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/src/kernel_imx # patch -p1 < ~/imx53_training/android/labs/lab02/add_leds_gpio.patch Having the kernel patched, you are ready to build a new image.

However, as Adeneo s BSP checks for the presence of.config file inside the kernel source code tree to determine whether the kernel needs to be compiled or not, you need to remove this file to ensure BSP will run kernel compile task again: # rm.config Next step is to compile the kernel, and only the kernel to save time: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/scripts #./build_android.sh --board=imx53_qsb \ --build-choice=kernel --lunch-type=eng --cpus-kernel=4 Note: Android and uboot were previously compiled to save time for this lab, otherwise, you would have to use --build-choice=all to build them too and build time would be extremely higher. Finally, you need to burn the new kernel image to the SD card, so first connect the SD card to the reader and be sure it is connected to the Virtual Machine, then run the commands: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/out # sudo dd if=uimage of=/dev/sdb bs=1m seek=1; sync Note: here, again for the sake of time, you are copying only the kernel image to the SD card, preserving all the filesystems that were previously created when flashing the prebuilt image. To flash the whole Android system, instead of using./flash_prebuilt_android.sh, you could use./flash_android.sh at this point. After burning the new kernel image, you can reconnect the SD card to the i.mx board to test kernel level LED control. At the terminal connected to the serial cable running screen, type the following commands: # cd /sys/class/leds/mx53::user # echo 0 > brightness # echo 1 > brightness You can repeat the last two commands as many times as you want, and hopefully you will see the user LED changing its status. Lab3 - Building first Java application for Android in Eclipse After completing the first two labs for a first contact with the Android BSP and to add low level support for led control, you will switch to the other end of the application, the UI, using Android SDK through Eclipse IDE to start development. The application that will be developed during this lab, in its first version, will not be able to control the actual user LED yet, as it will not include the native glue to the kernel LED driver

interface. But the resulting application will be reused in the next labs and will show you some concepts and tools behind Android. To get started, open Eclipse: # cd ~/Software/IDEs/eclipse #./eclipse & On Eclipse IDE, create a new Android Project by clicking File > New > Project... Android > Android Application Project: New Android Application ApplicationName: LedControl Project Name: LedControl Package Name: com.freescale.android.ledcontrol Build SDK: Android 2.3.3 (API 10) Minimum Required SDK: API 8: Android 2.2 (Froyo) Configure Launcher Icon - Next Create Activity - Next New Blank Activity Activity Name: LedControlActivity Layout Name: activity_led_control Title: LedControlActivity Click Finish Edit layout file of application just created (LedControl - res - layout - activity_led_control.xml): Add two buttons Name them LedOn and LedOff, by right-clicking them, then selecting edit text, then New String, then: String: LedOn New R.string: led_on for LedOn button and: String: LedOff New R.string: led_off for LedOff Set On click property to setledon for LedOn button and setledoff for LedOff button, by left clicking on each button, going to the Outline view, then properties, and finding the On Click event on the end of the list. Edit the main activity class (LedControl - src - com.freescale.android.ledcontrol - LedControlActivity.java) and add the following lines at the beginning: import android.view.view; import android.widget.toast; Inside LedControlActivity class body, add the following code at the end:

/* Click handler for the On button */ public void setledon(view view) { displayledstatus(1); /* Click handler for the Off button */ public void setledoff(view view) { displayledstatus(0); private void displayledstatus(int status) { String text = "Error while setting Led status!"; if(status!= -1) text = "Led is " + (status == 1? "On" : "Off"); Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); Connect a micro USB cable to the i.mx board micro conector and to the host PC, and run the Debugger from Eclipse IDE, by clicking the Debug button and selecting Debug as Android Application. You can add breakpoints to the source code to interact with the debugging tool. It s specially interesting to set a breakpoint at the following line of the activity source code, as it s called every time one of the buttons is clicked: Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); Pay special attention to logcat tab. It s a helpful tool to debug software too. Add more perspectives to the project like DDMS and Hierarchy View to get familiar with them. You can collect data from the running processes with the first and analyze the complete view hierarchy with the second. Lab4 - Adding native code to the application in Eclipse Now that you have a high level application to control LED status, you need to implement the low level glue to the actual kernel driver to effectively control the user LED status. To start this lab, add Android Native Support to LedControl project by right clicking the project root folder on the Package Explorer tab and setting a library name as follows: Library Name: libled_control_jni.so (note that the lib prefix and the.so suffix are already set by default) Note that you have been automatically switched to C/C++ perspective. Change back to Java perspective to create a new Java class with the following information: Package: com.freescale.android.ledcontrol Name: LedControlLib And add the following two methods to it:

public static native int setledon(); public static native int setledoff(); Add also the following static block to the end of the class body: static { System.loadLibrary("led_control_jni"); Edit LedControlActivity.java and replace: displayledstatus(1); by: displayledstatus(ledcontrollib.setledon()); and: displayledstatus(0); by: displayledstatus(ledcontrollib.setledoff()); Now that you have a Java library, that is actually just a simple class for the purpose of this laboratory, you can try to launch the Debugger again. You will notice that the application will run, but If you try to click on any of the buttons, you will get an "UnsatisfiedLinkError" exception, because although the high level class declared the two native methods that are now called from the LedControlActivity, they are not implemented yet in native code. To add native code, note that Eclipse already created the first two files for you (jni/android.mk and jni/led_control_jni.cpp) when you selected to Add Native Support to the project. To make it easier to create the prototypes of the native functions in led_control_jni.cpp, it is possible to generate a native header file from the Java Library class using a tool called javah, that extracts meaningful data from a Java class to a C/C++ header file. In a terminal, type the following commands to create the native header file: # cd ~/Software/Workspace/LedControl # javah -jni \ -classpath bin/classes \ -d jni com.freescale.android.ledcontrol.ledcontrollib

Back to Eclipse IDE, go back to C/C++ perspective and refresh the whole project so that the jni directory gets updated (either right click LedControl project and select Refresh or left click it and press F5). You will notice that the file com_freescale_android_ledcontrol_ledcontrollib.h has been created under jni directory. Rename this file to led_control_jni.h and open it to see how it looks like. Note especially that: jni header file is included there is a block protecting the C function declarations to avoid problems with C++ compiler the full namespace from Java is adapted to C conventions to name functions and keep track of their relationship with the Java world Now that the header file is ready to be used, you can finally open led_control_jni.cpp to implement the native functions. The end file can look like this: #include "led_control_jni.h" #include <string.h> #include <stdio.h> #include <android/log.h> #define DEBUG_TAG "LedControl" static jint setledstatus(jint status) { const char *fname = "/sys/class/leds/mx53::user/brightness"; FILE* file = fopen(fname, "r+"); if(file) { android_log_print(android_log_debug,debug_tag, "Writing %d to file %s", status, fname); fprintf(file, (status? "1" : "0")); fflush(file); fclose(file); return (jint) status; else { android_log_print(android_log_debug, DEBUG_TAG, "Failed to open file %s for writing %s", fname, status); return (jint) -1; JNIEXPORT jint JNICALL Java_com_freescale_android_ledcontrol_LedControlLib_setLedOn (JNIEnv *env, jclass clazz) { return setledstatus((jint) 1); JNIEXPORT jint JNICALL Java_com_freescale_android_ledcontrol_LedControlLib_setLedOff (JNIEnv *env, jclass clazz) { return setledstatus((jint) 0);

Note that a native API (android/log.h) is being used to generate log messages. With this API in use, you need to tell the linker where to find its implementation. To pass this information to the C/C++ linker, edit jni/android.mk file and add the following line in bold: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libled_control_jni LOCAL_SRC_FILES := led_control_jni.cpp LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) Now you are ready to rebuild and rerun the application. If you try to click one of the buttons on the application again, however, you will still get an error that this time will crash the application without even throwing an exception. This new error is related to the permissions associated with the node used to control the led brightness (/sys/class/leds/mx53::user/brightness). As this node is owned by root and is not accessible for write operations to any other users, you need to workaround this limitation somehow. For this lab, you can simply make this node globally accessible by executing the following command in the terminal running screen: # chmod 0777 /sys/class/leds/mx53::user/brightness After altering this device node permission, you will be able to see your full application in action, changing LED status by clicking each UI button. Note: you should be able to debug the native code through Eclipse as well. To enable native debugging, you need to enable some debugging features first. Add NDK_DEBUG=1 after ndkbuild in the Build command of the C/C++ Build settings inside project properties. Then create a new Debug configuration, choosing to run debug as Android Native Application. You may face some problems to get native symbols resolved, as by the time of this writing there are some known issues with Android ADT plugin running on Eclipse Juno with CDT. Give it a try. Lab5 - Adding application to Android BSP Once you have your stable application developed, it s common to want to embed it in a product as a system application instead of distributing it as a self contained package for pos installation. To start embedding LedControl application into Android BSP, create a vendor specific directory in the BSP root directory, where BSP scripts will look for applications by default: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/src # mkdir -p vendor/fsl

# cd vendor/fsl Add the LedControl application to the vendor specific directory: # cp -r ~/Software/Workspace/LedControl. # cd LedControl # rm -rf.settings bin libs obj gen assets.project.classpath.cproject Create a top level Android.mk file inside vendor/fsl/ledcontrol: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_SDK_VERSION := current LOCAL_PACKAGE_NAME := LedControl LOCAL_JNI_SHARED_LIBRARIES := libled_control_jni include $(BUILD_PACKAGE) # Use the following include to make native implementation. include $(call all-makefiles-under,$(local_path)) And edit the other Android.mk file inside the jni directory, so that it looks like this: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE := false LOCAL_MODULE := libled_control_jni LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := led_control_jni.cpp LOCAL_SHARED_LIBRARIES := liblog LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) Now, although the application is already recognized by the Android build environment, it s also necessary to add a reference to this application into i.mx product packages list, editing file device/fsl/imx5x/imx5x.mk and adding lines in bold, otherwise, the application will not be included in the final image: PRODUCT_MANUFACTUER := freescale # Android Hands On apk

PRODUCT_PACKAGES += \ LedControl \ libled_control_jni.so # Android infrastructures Last implementation step is to edit init.rc (device/fsl/imx5x/init.rc) file to change permissions for /sys/class/leds/mx53::user/brightness during boot, adding the line in bold: chown root radio /proc/cmdline # Chmod/chown FSL specific sys entry chown system system /sys/class/backlight/pwm-backlight.0/brightness chmod 0660 /sys/class/backlight/pwm-backlight.0/brightness chmod 0777 /sys/class/leds/mx53::user/brightness # Define TCP buffer sizes for various networks Finally you are ready to rebuild the BSP and reflash the SD card: # cd ~/imx53_training/android/i.mx53-qsb-android-gingerbread-release4.3/scripts #./build_android.sh --board=imx53_qsb \ --build-choice=android --lunch-type=eng --cpus-android=4 #./flash_android.sh /dev/sdb Summary Following picture summarizes some tools, processes and concepts you ve learned during this hands on session. We hope you enjoyed!

Ref: http://marakana.com/forums/android/examples/49.html