Android Security Lab WS 2014/15 Lab 1: Android Application Programming



Similar documents
Running a Program on an AVD

J A D E T U TO R I A L

Android Environment SDK

Getting Started with Android Development

Tutorial on Basic Android Setup

Android Environment SDK

Android Framework. How to use and extend it

Android Java Live and In Action

Arduino & Android. A How to on interfacing these two devices. Bryant Tram

How to develop your own app

TomTom PRO 82xx PRO.connect developer guide

AndroLIFT: A Tool for Android Application Life Cycles

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

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

Developing NFC Applications on the Android Platform. The Definitive Resource

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

06 Team Project: Android Development Crash Course; Project Introduction

Programming with Android

Introduction to Android Programming (CS5248 Fall 2015)

Android Development Setup [Revision Date: 02/16/11]

-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

App Development for Smart Devices. Lec #2: Android Tools, Building Applications, and Activities

Android Application Development

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

App Development for Smart Devices. Lec #4: Services and Broadcast Receivers Try It Out

Технологии Java. Android: Введение. Кузнецов Андрей Николаевич. Санкт-Петербургский Государственный Политехнический Университет

Developing with Android Studio

UP L18 Enhanced MDM and Updated Protection Hands-On Lab

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

Lab 0 (Setting up your Development Environment) Week 1

Android Development. Marc Mc Loughlin

Login with Amazon Getting Started Guide for Android. Version 2.0

Android Mobile App Building Tutorial

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

Developing In Eclipse, with ADT

Android Framework. How to use and extend it

Getting Started: Creating a Simple App

How To Develop An Android App On An Android Device

Developing Android Applications Introduction to Software Engineering Fall Updated 7 October 2015

An Android-based Instant Message Application

How To Run A Hello World On Android (Jdk) On A Microsoft Ds.Io (Windows) Or Android Or Android On A Pc Or Android 4 (

How To Develop Android On Your Computer Or Tablet Or Phone

Practical Android Projects Lucas Jordan Pieter Greyling

How to Create an Android Application using Eclipse on Windows 7

Android Development Exercises Version Hands On Exercises for. Android Development. v

Android Application Repackaging

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

ADT Plugin for Eclipse

Getting Started with Android

Android Programming: Installation, Setup, and Getting Started

OpenCV on Android Platforms

Android Development Tutorial. Nikhil Yadav CSE40816/ Pervasive Health Fall 2011

Overview of Web Services API

Operating System Support for Inter-Application Monitoring in Android

Android Setup Phase 2

SSL VPN Setup for Windows

Android Developer Fundamental 1

Tutorial on Basic Android Setup

Android Tutorial. Larry Walters OOSE Fall 2011

Lab 4 In class Hands-on Android Debugging Tutorial

Android 多 核 心 嵌 入 式 多 媒 體 系 統 設 計 與 實 作

l What is Android? l Getting Started l The Emulator l Hello World l ADB l Text to Speech l Other APIs (camera, bitmap, etc)

Android Services. Services

WEARIT DEVELOPER DOCUMENTATION 0.2 preliminary release July 20 th, 2013

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

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

ID TECH UniMag Android SDK User Manual

CSE476 Mobile Application Development. Yard. Doç. Dr. Tacha Serif Department of Computer Engineering Yeditepe University

Developing for MSI Android Devices

Hello World. by Elliot Khazon

Developing Android Apps: Part 1

1. Introduction to Android

Android Application Development - Exam Sample

Introduction to Android Development. Jeff Avery CS349, Mar 2013

How To Use Titanium Studio

Oracle FLEXCUBE Direct Banking Android Tab Client Installation Guide Release

Basic Android Setup Windows Version

Chapter 2 Getting Started

Android Application Development Lecture Notes INDEX

Android Environment Emulator

Introduction to Android Development

CSA Software Listing Table of Contents. Both Windows and Mac platforms are supported.

Tutorial: Mobile Business Object Development. SAP Mobile Platform 2.3 SP02

Tutorial: Mobile Business Object Development. SAP Mobile Platform 2.3

Introduction to NaviGenie SDK Client API for Android

How to Program an Android Application to Access and Manage the Wi-Fi Capabilities of a Smartphone

Android Geek Night. Application framework

Tutorial #1. Android Application Development Advanced Hello World App

POOSL IDE Installation Manual

Q1. What method you should override to use Android menu system?

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Introduction to Android. CSG250 Wireless Networks Fall, 2008

Now that we have the Android SDK, Eclipse and Phones all ready to go we can jump into actual Android development.

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

Operational Decision Manager Worklight Integration

Android Basics. Xin Yang

Transcription:

Saarland University Information Security & Cryptography Group Prof. Dr. Michael Backes saarland university computer science Android Security Lab WS 2014/15 M.Sc. Sven Bugiel Version 1.0 (October 6, 2014)

Login on Lab Machines Username: Password: smartseclab android Introduction In this Lab, we provide a simple introduction to application programming on Android, with focus on familiarizing the students with the most important IDE elements and on inter-application communication based on Intents and Services. 1 Development environment In this lab we use theandroid Development Tools (ADT) 1, which are based on the Eclipse IDE. Alternative IDEs exist, e.g., Android Studio based on IntelliJ IDEA 2. 1. Open the ADT from your desktop 2. As workspace chose the /usr/local/home/smartseclab/androidlab directory 3. On the bottom you see Android development specific tools such as Logcat, Emulator control, etc., which you will use later. Figure 1: Android development specific tools 4. In the top right corner you can switch to DDMS, the debugging view for Android if required. 5. Further, in the Menu Bar, you see two new entries: (a) for the Android SDK Manager (left) (b) for the Android AVD (Android Virtual Device) Manager (right) Figure 2: Android Menu Bar entries 1 http://developer.android.com/sdk/index.html 2 http://developer.android.com/sdk/installing/studio.html 2

2 Creating an Android Virtual Device 1. Open the AVD Manager. 2. Use the wizard to create a new Intel Atom (x86) Android v4.3 AVD by either configuring it according to your wishes (first tab) or using a pre-configured device definition (second tab). 3. Start your new virtual device 4. Open a terminal (using the panel entry on top of your screen or from the desktop link) and use the Android Debug Bridge (adb) to observe the device s log output: $ adb logcat For a full list of the adb features (e.g., emulator control), please refer to the adb help page. 5. Once the device has been fully booted, switch to the DDMS view in ADT and briefly familiarize yourself with available options (e.g., overview of device s processes, colored logcat view at the bottom, etc.). In particular, use the emulator control to send an SMS message to your device and verify your virtual device has received the message. 6. The emulator control is essentially just a GUI for a telnet-based service to send commands to the emulator. Sending an SMS message could, thus, also be accomplished from the terminal using a telnet client: $ telnet localhost 5554 $ sms send 5556 "Your message" where 5554 is the ID of the emulator (check the emulator window name) and 5556 the sender number of the SMS. 7. Create and boot a second virtual device using the same configuration for SDK version as for the first device. 8. Once this second device has booted, go to the SMS ( Messaging ) app and send a test SMS to the first emulator device. The phone number of each device equals the number (e.g., 5554) in the title bar of the emulator window. 9. To end emulators, simply close their windows, no special shutdown procedure is required. 10. Close both emulators and the AVD window. 3 Creating a first Android App 1. Switch back to the Java view in Eclipse. 2. Create a new Android Application Project (cf. Figure 3). Figure 3: Creating a new Android Application Project 3. Since we will extend this app in the next lab session, please use the following settings: (a) Application Name App1 3

(b) Minimum required SDK API 8 (Froyo) (c) Leave the rest in this option dialog and all following dialogs as default 4. Once the project has been created, you should see a screen similar to Figure 4, which shows a simple application skeleton with a Hello World Activity. 5. To quickly test your app, start one of your virtual devices and wait until it has booted up. Then right-click on your App1 project and chose Android Application in the Run as context menu (cf. Figure 5). When there is only one emulator running, the app will be automatically started on this one. Otherwise a menu pops up in which you can chose the target virtual device. Figure 4: New app template Figure 5: Run as Android application 6. In the following, we will extend this app with a simple Service and a BroadcastReceiver component. 3.1 Adding a BroadcastReceiver component 1. Create a new class Receiver.java in your package (i.e., com.example.app1.receiver) 2. The following Listing presents a minimal implementation of the BroadcastReceiver component: package com.example.app1; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.util.log; public class Reciever extends BroadcastReceiver { @Override public void onreceive(context context, Intent intent) { // This code is called when the receiver is triggered 4

3. Add the following line of code to the onreceive method in order to log the received Intent message: Log.d("App1.Receiver", "Called with Intent "+intent.tostring()); 4. To actually register the BroadcastReceiver in the system upon app installation, we have to extend the application entry of the AndroidManifext.xml of App1. You could do this using the GUI of ADT. For now, however, we will edit the XML directly by adding the following lines of XML: <application...>... <receiver android:name="com.example.app1.receiver"> <intent-filter> <action android:name="com.example.intent.action.broadcast"/> </intent-filter> </receiver>... </application> This registers the BroadcastReceiver component in this system. It defines an Intent filter, which means the BroadcastReceiver will only receive broadcasted Intents that match the action string com.example.intent.action.broadcast. 5. To quickly test your new BroadcastReceiver, re-install your modified app by running it again on the virtual device. Start monitoring the logcat output, either in the DDMS view or in a terminal by filtering for the string App1. Run in a (second) terminal the following command to instruct the ActivityManager on the device to send a broadcast that fits the Intent filter of the receiver component: $ adb shell am broadcast -a com.example.intent.action.broadcast 6. If done correctly, you should be able to see debug output similar to: D/App1.Receiver( 2136): Called with Intent Intent{ act=com.example.intent.action.broadcast flg =0x10 cmp=com.example.app1/.reciever 7. A broadcast receiver can also be registered dynamically at runtime through an application s context. However, at this point, we refer you to the documentation 3 for more information on this. 3.2 Adding a simple Service component 1. Next, we will add a simple Service component to the application to which other apps can bind and then interact with in a client-server fashion. First, define the interface of the new service through the Android Interface Definition Language (AIDL) 4. Create in your App1 source code package a new file IRemoteService.aidl with the following content: package com.example.app1; interface IRemoteService { int getpid(); 3 http://developer.android.com/reference/android/content/broadcastreceiver.html 4 http://developer.android.com/guide/components/aidl.html 5

2. Now we will implement this interface in a dedicated Service component (cf. Figure 8 and Figure 9 in Appendix A for an illustration of a Service s life-cycle). Thus, add a new class to your App1 package called RemoteService.java. The following shows its implementation: package com.example.app1; import android.app.service; import android.content.intent; import android.os.ibinder; import android.os.process; import android.os.remoteexception; public class RemoteService extends Service { @Override public IBinder onbind(intent intent) { // Object to be returned to caller apps for RPC return mbinder; // Stub of our service, i.e., implementation of the interface private final IRemoteService.Stub mbinder = new IRemoteService.Stub() { @Override public int getpid() throws RemoteException { // We use android.os.process return Process.myPid(); ; The Stub implements the Service interface. The only remotely callable function getpid() simply returns the service s process ID to the caller. Callers that bind to this service, receive an IBinder interface object for this service, which they can use for RPC. 3. Of course, we also have to register this service in the system through the AndroidManifest.xml upon installation: <application...> <service android:name="com.example.app1.remoteservice"> <intent-filter> <action android:name="com.example.app1.iremoteservice"/> </intent-filter> </service>... </application> 4. To test this service, we create a simple second caller application. 3.3 Creating a second Android App 1. Repeat the steps for the creation of App1, however, call this new app App2. 2. Since App2, as a caller to the service of App1, has to know the Service interface, we have to copy the AIDL file from the package of App1 to the package of App2 as shown in the following Figure 6. 3. For this setup, we keep binding and calling to App1 s service very simple and integrate them strictly into the life-cycle of App2 s main activity (cf. Figure 7 in Appendix A for an illustration of the Activity life-cycle). Add the following code to the MainActivity.java of App2: 6

Figure 6: Setup for App2 to be able to create a Proxy for App1 s service. IRemoteService miremoteservice; private ServiceConnection mconnection = new ServiceConnection() { // Called when the connection with the service is established public void onserviceconnected(componentname classname, IBinder service) { // This gets an instance of the IRemoteInterface, which we can use to call on the service miremoteservice = IRemoteService.Stub.asInterface(service); int pid = -1; try { pid = miremoteservice.getpid(); catch (RemoteException e) { Log.e("App2.MainActivity", "Error calling getpid()", e); Log.d("App2.MainActivity", "getpid()="+pid); // Called when the connection with the service disconnects unexpectedly public void onservicedisconnected(componentname classname) { miremoteservice = null; ; @Override protected void onresume() { super.onresume(); if(bindservice(new Intent(IRemoteService.class.getName()), mconnection, Context. BIND_AUTO_CREATE)) { Toast.makeText(this, "Binding to successful", Toast.LENGTH_LONG).show(); else { Toast.makeText(this, "Binding to failed!", Toast.LENGTH_LONG).show(); @Override protected void onpause() { super.onpause(); if(miremoteservice!= null) { unbindservice(mconnection); This code will bind to the service every time the Activity comes back to the foreground on the screen (onresume()) and will directly use the IBinder object received upon binding to call the remote service. The received PID from App1 is logged. The result of the binding operation is briefly shown as a pop-up (Toast) on the screen to the user. To correctly cast the IBinder object to the interface of App1 s service, App2 requires the AIDL file. To avoid leaking service connections, the activity unbinds from the service as soon as the activity is not longer in the foreground on the screen (onpause()). 7

4. Install first App1 and afterwards App2 by running them as Android applications. Observe the screen for successful binding and the logcat output for the logged PID. 5. For simpler service implementations, such as IntentServices, we refer to the Android documentation 5. 5 http://developer.android.com/reference/android/app/intentservice.html 8

A Activity and Service Life-Cycle Figure 7: Activity lifecycle (Source http://developer.android.com/reference/android/app/ Activity.html) 9

Figure 8: General Service life-cycle (left: service creation with startservice(); right: service creation with bindservice(); source http://developer.android.com/guide/components/services.html) 10

Figure 9: Bound-Services life-cycle (Source http://developer.android.com/guide/components/ bound-services.html). 11