an introduction to android developmenttools for application development ! android sdk ! provides the...

53
AN INTRODUCTION TO ANDROID DEVELOPMENT CS231M | Alejandro Troccoli

Upload: others

Post on 25-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

AN INTRODUCTION TO ANDROID DEVELOPMENT CS231M | Alejandro Troccoli

Page 2: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Outline

!   Overview of the Android Operating System !   Development tools !   Deploying application packages !   Step-by-step application development !   Calling native code from your application

Page 3: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

The Android ecosystem

!   An open source platform for mobile, embedded and wearable devices

!   Google is the principle maintainer !   Other companies contribute to the system. !   Each device manufacturer can customize Android to suite their

needs

Page 4: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Android architecture

Linux Kernel (GPL license) C code – compiled to native platform (x86, arm, mips)

Native framework layer User mode C, C++ code – compile to native platform or 32bit

compatibility mode on 64 bits.

Android framework Java classes under com.android

User applications Use Java framework and, optionally, native code.

Page 5: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Android versioning

! Plaform version !   4.4.2 KitKat !   4.4.3 Jellybean MR2

!   Framework API level !   SDK compatibility !   Each platform version has an API level

!   NDK API level !   API level for native headers

Page 6: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Browsing the Android Source

!   Source at: ! https://android.googlesource.com/

!   Porting instructions (for system developers) ! https://source.android.com/devices/index.html

!   New camera HAL ! https://source.android.com/devices/camera/camera.html

Page 7: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Reference to the framework APIs

! com.android classes ! http://developer.android.com/reference/packages.html

Page 8: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Outline

!   Overview of the Android Operating System !   Development tools !   Deploying application packages !   Step-by-step application development !   Calling native code from your application

Page 9: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Tools for application development

!   Android SDK !   Provides the Java framework classes !   Compiles to java bytecode !   Class framework is updated with every OS release

!   Android NDK !   C/C++ toolchain for compiling to machine code

!   Android platform tools ! adb (android debug bridge) : runs and debugs apps from your dev machine

!   Android developer tools !   Eclipse plug-in for Android

Page 10: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

All tools packed in a Virtual Machine

!   To speed up setup, a virtual machine has been setup.

Page 11: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Tegra Android Development Pack

Page 12: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Tegra Android Development Pack

!   Register for an account at: ! https://developer.nvidia.com/user/register

!   Sign-up for Gameworks Registered Developer Program

Page 13: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Tegra Note 7

Click 7 times to enable developer options

Page 14: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Application packages

!   .apk files: compressed files !   class byte code !   resources( icons, sounds, etc). !   Binary native files

!   All .apks are signed !   Default development key is created by SDK. !   When updating an application, signature are checked.

Page 15: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Outline

!   Overview of the Android Operating System !   Development tools !   Deploying application packages !   Step-by-step application development !   Calling native code from your application

Page 16: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Installing an application

!   From application distribution markets !   Google Play !   Amazon AppStore

!   From your local computer using adb

Page 17: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Enabling android debug bridge (adb)

Page 18: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Outline

!   Overview of the Android Operating System !   Development tools !   Deploying application packages !   Step-by-step application development !   Calling native code from your application

Page 19: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Useful adb commands

Page 20: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Hello Android!

!   In Eclipse !   File -> New -> Android

Application Project

Page 21: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Hello Android!

Page 22: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Hello Android!

Page 23: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Import an project from existing code

Page 24: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

AndroidManifest.xml

!   Package Name / version !   Required SDK and target SDK !   Application/Activities !   Permissions

Page 25: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Android Activity

!   Provides user interaction ! http://developer.android.com/reference/android/app/Activity.html

!   Callbacks for life-cycle management ! onCreate() ! onResume() ! onPause()

!   An application can have multiple activities.. !   Needs one launcher activity…

Page 26: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

HelloAndroidActivity

Page 27: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

HelloAndroidActivity

!   Use onCreate() to create UI.

Page 28: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Launch!

Add Activity to AndroidManifest.xml:

Page 29: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Launch! (take 2)

Page 30: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Launch with debugger

Page 31: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Launch (take 3!)

Page 32: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Views

!   Can be composed in a tree hierarchy. !   The root View is the argument to setContentView

Page 33: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Creating a layout

Page 34: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Accessing layout elements from Activtiy

!   Use findViewById R.id.name corresponds to the name given in the xml file

Page 35: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Event listeners (and logging, too)

Page 36: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Logcat

!   Window -> Show View -> Other -> Android -> Logcat

Page 37: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Creating a log file

Page 38: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Writing to the log file

Page 39: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Long running task

!   Long running tasks on the main thread can block the UI !   App looks unresponsive

Page 40: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Use a separate Thread instead

Page 41: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Use Handlers to update UI

Page 42: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Add a Progress dialog

Page 43: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Outline

!   Overview of the Android Operating System !   Development tools !   Deploying application packages !   Step-by-step application development !   Calling native code from your application

Page 44: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Adding native code: Java Native Interface

In the Java class, add a method without implementation and the native prefix

Create the jni headers: > javah –d jni –classpath .\bin\classes edu.stanford.cs231m.helloandroid.HelloAndroidActivity

Page 45: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Adding native support

!   Right-click on project -> Android Tools -> Add Native Support

Page 46: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Android.mk

! Makefile for NDK

Page 47: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

HelloAndroid.cpp

Page 48: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Let’s run it!

!   Modify the Java code to call square and run the app…

Page 49: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Unfortunately, HelloAndroid has stopped.

Need to load the native library into the Java virtual machine!

Page 50: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Application.mk

! Makefile options that are applied to all modules! !   Target ABI !   Choice of STL implementation !   Global compiler options..

Page 51: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Debugging Native Code

!   Enable debug build with NDK_DEBUG

Page 52: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Launch: Debug as Native app

Need to wait for debugger to attach

Page 53: AN INTRODUCTION TO ANDROID DEVELOPMENTTools for application development ! Android SDK ! Provides the Java framework classes ! Compiles to java bytecode ! Class framework is updated

Little trick to wait for debugger

Android.mk

Define waitForDebugger() and insert a call to wait in your program. Once the debugger attaches, pause the program and set _debug to 0.