Multithreading and Java Native Interface (JNI)!



Similar documents
Introduction to Native Android Development with NDK

The Decaffeinated Robot

Development_Setting. Step I: Create an Android Project

OpenCV on Android Platforms

Android Hands-On Labs

Native code in Android Quad Detection

How to develop your own app

ODROID Multithreading in Android

Android Development. Marc Mc Loughlin

Android Basics. Xin Yang

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

Lecture 1 Introduction to Android

An Introduction to Android. Huang Xuguang Database Lab. Inha University

Table of Contents. Adding Build Targets to the SDK 8 The Android Developer Tools (ADT) Plug-in for Eclipse 9

Multi-core Programming System Overview

Introduction to Android Programming (CS5248 Fall 2015)

Overview of CS 282 & Android

An Introduction to Android

How To Develop Android On Your Computer Or Tablet Or Phone

Research and Design of Universal and Open Software Development Platform for Digital Home

Extending your Qt Android application using JNI

Android NDK Native Development Kit

Introduction to Android

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

Contents at a Glance

How To Develop An App For Ios (Windows)

Example of Standard API

Restraining Execution Environments

Introduction to Android. CSG250 Wireless Networks Fall, 2008

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

Computer Graphics on Mobile Devices VL SS ECTS

Chapter 2 Getting Started

Production time profiling On-Demand with Java Flight Recorder

Leak Check Version 2.1 for Linux TM

Smartphone market share

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

Admin. Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources. Recap: TinyOS. Recap: J2ME Framework

Getting started with Android and App Engine

Monitoring, Tracing, Debugging (Under Construction)

HTTPS hg clone plugin library SSH hg clone plugin library

Android Architecture. Alexandra Harrison & Jake Saxton

System Reference 2013

AllJoyn Android Environment Setup Guide

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)

Performance Analysis of Android Platform

To Java SE 8, and Beyond (Plan B)

An Introduction to Android Application Development. Serdar Akın, Haluk Tüfekçi

Introduction to NaviGenie SDK Client API for Android

Tutorial on OpenCV for Android Setup

A Study on Android development kits and the Phone Gap Framework

Real Time Programming: Concepts

Accelerate your Mobile Apps and Games for Android on ARM. Matthew Du Puy Software Engineer, ARM

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

Load Testing Ajax Apps using head-less browser tools. NoVaTAIG April 13, 2011 Gopal Addada and Frank Hurley Cigital Inc.

Debugging with TotalView

Getting Started with the Internet Communications Engine

BogDan Vatra and Andy Gryc. Qt on Android: Is it right for you?

Numerical Algorithms Group

Testing for Security

Creating a List UI with Android. Michele Schimd

IOIO for Android Beginners Guide Introduction

Building native mobile apps for Digital Factory

Android Programming and Security

Android Fundamentals 1

Spring Design ScreenShare Service SDK Instructions

Reach 4 million Unity developers

Getting Started: Creating a Simple App

Lecture 10 Fundamentals of GAE Development. Cloud Application Development (SE808, School of Software, Sun Yat-Sen University) Yabo (Arber) Xu

directory to "d:\myproject\android". Hereafter, I shall denote the android installed directory as

ANDROID BASED MOBILE APPLICATION DEVELOPMENT and its SECURITY

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

Extending Android's Platform Toolsuite

INTRODUCTION TO ANDROID CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 11 02/15/2011

TomTom PRO 82xx PRO.connect developer guide

An Easier Way for Cross-Platform Data Acquisition Application Development

Mobile Application Development 2014

Frameworks & Android. Programmeertechnieken, Tim Cocx

An Oracle White Paper July Oracle Primavera Contract Management, Business Intelligence Publisher Edition-Sizing Guide

Finding Performance and Power Issues on Android Systems. By Eric W Moore

TUTORIALS AND QUIZ ANDROID APPLICATION SANDEEP REDDY PAKKER. B. Tech in Aurora's Engineering College, 2013 A REPORT

Introduction to Android SDK Jordi Linares

Review On Google Android a Mobile Platform

PART IV Performance oriented design, Performance testing, Performance tuning & Performance solutions. Outline. Performance oriented design

FileMaker Server 7. Administrator s Guide. For Windows and Mac OS

Introduction to Android

HybriDroid: Analysis Framework for Android Hybrid Applications

Java Coding Practices for Improved Application Performance

New Features in XE8. Marco Cantù RAD Studio Product Manager

Hello World. by Elliot Khazon

Mobile Application Development

Android Mobile App Building Tutorial

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

Appium mobile test automation

Board also Supports MicroBridge

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

Android Application Development

Mobile Application Development Android

Transcription:

SERE 2013 Secure Android Programming: Best Practices for Data Safety & Reliability Multithreading and Java Native Interface (JNI) Rahul Murmuria, Prof. Angelos Stavrou rmurmuri@gmu.edu, astavrou@gmu.edu

Multi-core processors in Android devices p Natural Progression: " Servers -> Workstations -> Laptops -> Phones p Multiple serial tasks can run in parallel" For example: Each core can handle different tasks while rendering browser pages: n Execute JavaScript n Process network connections n Manage protocol stack or control services You get concurrency for free

Multi-core processors in Android devices Cost of multi-threading: p Does multi-threading always bring bugs? n Threading should not be an after-thought p Are multi-threaded applications always powerinefficient? n Well-designed multi-threaded program gives flexibility to the operating system in managing energy better Retrofitting concurrency is a bad idea.

Multi-threading on Android p Normal Java threading and synchronization support is available to the App developer p Additionally Android SDK provides additional API support for threading for both Java applications and C/C++ native code So what s different on Android, compared to traditional software?

Use-case determines choice of API p Responsiveness in the UI n Example: Performing a network operation in background, leaving the UI thread for I/O p Speed and efficiency of long-running operations n Example: Decoding multiple image files to show in tiled-view on a scrollable screen We discuss two main scenarios.

Use-case determines choice of API p Responsiveness in the UI n Example: Performing a network operation in background, leaving the UI thread for I/O p Speed and efficiency of long-running operations n Example: Decoding multiple image files to show in tiled-view on a scrollable screen We discuss two main scenarios; UI first.

Badly written Android Application public class MainActivity extends Activity { private TextView view; protected void oncreate( ) { super.oncreate( ); setcontentview( activity_main); try { Thread.sleep(10000); } catch ( ) { printstack(); } view.settext( how are you ); Log.v(MyAppName, oncreate completed ); } What s wrong with this code?

Removing the burden from UI Thread public class MainActivity extends Activity { ViewSetterClass task; String mytext = how are you ; protected void oncreate( ) { super.oncreate( ); setcontentview( activity_main); task = new ViewSetterClass(view); task.execute(mytext); } Continued

Removing the burden from UI Thread class ViewSetterClass extends AsyncTask<String, Void, String> { private TextView view; protected String doinbackground(string params) { // params come from the execute() call in previous slide try { Thread.sleep(10000); } catch ( ) { printstack(); } return params[0]; } protected String onpostexecute(string mytext) { view.settext(mytext); Log.v(MyAppName, oncreate completed ); } } UI Thread does not sleep during the 10 seconds.

Memory Analysis of a production code p We will analyze Image Downloader Application from Android Developer s Blog n http://android-developers.blogspot.com/2010/07/ multithreading-for-performance.html p Memory Analysis can be used to find out n Memory leaks n Duplicate Strings, Weak references, etc. Why should we care about memory analysis?

Memory Structure in Android p Lot of shared memory between processes n Physical RAM is mapped to multiple processes n Physical memory usage is not as relevant as the scaled reading based on ratio of number of processes accessing a given page in memory (Pss value) p Some memory analysis tools: n adb shell dumpsys meminfo <process-name> n adb shell procrank n Eclipse Memory Analyser

Demo of memory analysis p Let s try analysis a few applications using the memory analysis tools described in previous slide.

Threading data-intensive operations p Multiple threads in an Android app using a thread pool object n You can also communicate between the threads p Create a pool of threads:

External Link to Sample Application p For ThreadPool Google has a sample application at their developer website: n https://developer.android.com/training/multiplethreads/index.html

Motivation for Native Code p Access Low-level OS features ioctl, poll, etc. p Explore advanced CPU features NEON instruction set for signal and video processing p Reuse large or legacy C/C++ programs p Improve performance of computationally intensive operations p OpenGL p OpenSL ES

Multiple Ways to Program Native p Using Java Application to present a UI to the user, and perform parts of logic in native code n Interfacing between Java and C is done using : Java Native Interface (JNI) p Create purely native Activity with UI designed using OpenGL n Not common practice p Android has a C Library called Bionic, custom built for use on mobile phones. We focus on the first method using JNI

JNI Example Step by Step p Make new application called n Project: HelloJni n Package: edu.gmu.hellojni n Activity Name: HelloJni p The Java sources are under folder HelloJni/src p Make new subdirectory in project folder called jni n i.e., HelloJni/jni p In jni directory make new file called n MyHelloJni.cpp

JNI Example (p2) p In this file, MyHelloJni.cpp, put #include <string.h> #include <jni.h> extern "C" { } JNIEXPORT jstring JNICALL Java_edu_gmu_HelloJni_HelloJniActivity_stringFromJNI { } ( JNIEnv* env, jobject thiz ) return env->newstringutf("hello from JNI"); p Important: There is a logic to that complicated function name, and it is required to follow the convention.

JNI Example (p3) p In HelloJni/jni make new file called Android.mk p Put the following in Android.mk LOCAL_PATH := $(call my- dir) include $(CLEAR_VARS) LOCAL_MODULE := HelloJni LOCAL_SRC_FILES := MyHelloJni.cpp include $(BUILD_SHARED_LIBRARY) p Note that LOCAL_MODULE defines the module name

JNI Example (p4) p Build library n Open terminal. n cd to <workspace>/hellojni/jni n Run build p <android-ndk-r7b>/ndk-build n Check that libhellojni.so is created Java code compiles using the Android SDK" Native code compiles using the Android NDK

On startup Working of JNI p The JNI library is loaded when System.loadLibrary() is called. p Every function in the native C code maps to a function declaration on the Java side. n The declarations are defined as native n public native int getnextframe(parameters); Demo : Let s look at some samples in code Ref: http://developer.att.com/developer/forward.jsp? passeditemid=11900170

Security implications of C code p Java Virtual Machine (JVM) does a lot of work to make the Java code secure: n Protects against buffer overruns, and stack smashing attacks n It performs bounds checking on all arrays p Jni code is a blackbox to the JVM n Native code also runs with same privileges as the Java code that spawned it, however, sandboxing is weaker

Other tips for Reliable Development p If using pthreads in C for native threads, remember to detach each of the threads before exiting p All arguments passed to and from the native code are local references to the JNI functions n There is API to define global references explicitly p Make use of onpause/onresume to save or close resources that are not needed in the background n Specially useful if you have multiple threads, or content listeners which are not for other applications to use

Thank you

Extra Slides

Tutorial Links p JNI: http://marakana.com/s/post/1292/ jni_reference_example p Multithreading: http://developer.android.com/ training/multiple-threads/index.html

In java HelloJni p After public class HelloJniActivity extends Activity { n public native String stringfromjni(); // the c++ function name n static { n System.loadLibrary("HelloJni"); // shared lib is called libhellojni.so. p // this name is from the LOCAL_MODULE part of the Android.mk file n } p In oncreate, after setcontentview(r.layout.main); put n Log.e("debug","calling jni"); n Log.e("debug",stringFromJNI()); // last part of name of c++ function n Log.e("Debug","done"); p Run and check log p Note: public native allows any function to be defined. But when this function is called, the shared library must have already been loaded (via System.loadLibrary)