SlideShare a Scribd company logo
1 of 49
Download to read offline
Running native
    code on Android
           C       NDK   ARM




Cédric @deltheil               OSDC.fr 2012-10-13
- Why using native
     code? -
1. Reuse some C
      code
2. Use 3rd party
    libraries
3. Create portable
   components
   Android   iOS   ...
4. Use hardware
  acceleration
  SIMD   CPU   algorithm
5. ... because it’s
        fun :)
...but How?




Native Development Kit
- Android NDK
  overview -
Downloads
http://developer.android.com/tools/sdk/ndk/index.html




                                           cross-platform
Contents
1. Tools for Linux,OS X,Win to cross-compile
native ARM,x86,MIPS binaries.
Contents
2. System files and headers for Android native
APIs.




           w/ Java Native Interface (a.k.a JNI) <jni.h>
Contents
3. Easy-to-use build system: Android.mk &
ndk-build (make wrapper).
Contents
4. Documentation and
code samples.
- Steps to embed C
       code -
1/3 Wrap C
            Java


          libmisc.so




Java
             JNI
 C


           C code
1/3 Wrap C

/* 1. Wrap your C code with the JNI */

#include <jni.h>

jstring
Java_com_example_Foo_bar(JNIEnv* env, jobject thiz) {
  char buffer[512];
  /* ... */
  return (*env)->NewStringUTF(env, buffer);
}


namespace   class   method   native interface   instance
2/3 Build native lib
          Java


        libmisc.so




Java
           JNI
 C


         C code
2/3 Build native lib

# 2. Generate a lib with ndk-build

$ ndk-build
Compile thumb   :   misc <= misc.c
StaticLibrary   :   libmisc.a
SharedLibrary   :   libmisc.so
Install         :   libmisc.so => libs/armeabi/libmisc.so
...
3/3 Java ext
             Java


           libmisc.so




Java
              JNI
 C


            C code
3/3 Java ext
/* 3. Expose the logic via a native extension */

package com.example;

public class Foo extends /* ... */ {

    static {
      System.loadLibrary("misc");
    }

    public native String bar();

}
Basic
         Java


       libmisc.so




Java
          JNI
 C


        C code
Advanced
                            Java


                      libmisc.so




     Java
                            JNI
       C


3rd party libs             C code

A sources         libA.a            libB.a   B sources
- Building 3rd
  party libs -
Pro-tip


Use the Standalone Toolchain.
What?

A customized install for a given platform,
arch.
 handy
Why?

No need to write specific Android.mk
makefiles: reuse existing build systems.
  0 LOC    Makefile    autotools
How?
[Ex. 1] jsmn




Makefile   C   JSON   parser
[Ex. 1] jsmn 1/3




override Makefile variables
[Ex. 1] jsmn 2/3
[Ex. 1] jsmn 3/3




choose the proper arch... and that’s it!


            Full gist @ http://git.io/ndk-jsmn
[Ex. 2] msgpack




autotools   C/C+   binary   serialization
[Ex. 2] msgpack 1/3
[Ex. 2] msgpack 2/3
[Ex. 2] msgpack 3/3




use the cross-toolchains... and that’s it!


           Full gist @ http://git.io/ndk-msgpack
- Using a prebuilt
     library -
Overview
Android.mk




                                     libs/
                       ndk-build
                                   armeabi/
                                   libfoo.so



Application.mk


                            single      arch
Overview
Android.mk




                                     libs/
                       ndk-build
                                    armeabi/
                                    libfoo.so

                                   armeabi-v7a/
                                   libfoo.so
Application.mk


                             fat        binary
- Loading a native
     library -
Basic




Pick the right
   arch vs.
target device
Medium
Custom
    frontal library

    native method (frontal lib.)



          Pick the right arch
               yourself!
Custom: why?

[1] Properly target ARMv7 without NEON CPUs
when getCpuFeatures() can’t be used at
runtime (e.g. 3rd party libs)


[2] Bypass a loadLibrary bug on ICS!

[1] e.g. «the NVidia Tegra 2 generation SoC has a dual-core ARM Cortex-A9 CPU (lacking ARM's
advanced SIMD extension—NEON)» - see http://en.wikipedia.org/wiki/Tegra
[2] see http://www.moodstocks.com/2012/03/20/ice-cream-sandwich-why-native-code-support-
sucks
- Quick JNI hints -
C pointer = int field
                                          e.g. persist a DB handle
                                          and use it throughout
                                          the JNI extensions




Don’t forget to destruct it (explicitly
or at finalize() time)
C error codes




            C error code
            & string
Resources
#AltDevBlogADay NDK Part 1 & Part 2   Linux Mag., 07/11




       Tokyo Cabinet Java Pkg          VLC for Android
Thanks!


Questions? Comments? cedric@moodstocks.com | @deltheil

More Related Content

Viewers also liked

Jak na obsah v sociálních médiích
Jak na obsah v sociálních médiíchJak na obsah v sociálních médiích
Jak na obsah v sociálních médiíchmascha_dudu
 
Behavioural economics
Behavioural economicsBehavioural economics
Behavioural economicsAyush Gangwar
 
Фотопроект Марины Васильевой и Юрия Максименко
Фотопроект Марины Васильевой и Юрия МаксименкоФотопроект Марины Васильевой и Юрия Максименко
Фотопроект Марины Васильевой и Юрия МаксименкоЮрий Максименко
 
Kokkinis manos 12-2012
Kokkinis manos 12-2012Kokkinis manos 12-2012
Kokkinis manos 12-2012mkokkinis
 
Copyright and fair use
Copyright and fair useCopyright and fair use
Copyright and fair useDrJasonBeach
 
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWA
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWAVIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWA
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWAManueldv
 
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケKojiro Fukazawa
 
Beckman_outliers final project
Beckman_outliers final projectBeckman_outliers final project
Beckman_outliers final projectJessie Seeley
 
Николай Петрович Румянцев в литературе
Николай Петрович Румянцев в литературеНиколай Петрович Румянцев в литературе
Николай Петрович Румянцев в литературеЮрий Максименко
 
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...mascha_dudu
 

Viewers also liked (16)

Solve sysbyelimmult (1)
Solve sysbyelimmult (1)Solve sysbyelimmult (1)
Solve sysbyelimmult (1)
 
Jak na obsah v sociálních médiích
Jak na obsah v sociálních médiíchJak na obsah v sociálních médiích
Jak na obsah v sociálních médiích
 
Behavioural economics
Behavioural economicsBehavioural economics
Behavioural economics
 
Фотопроект Марины Васильевой и Юрия Максименко
Фотопроект Марины Васильевой и Юрия МаксименкоФотопроект Марины Васильевой и Юрия Максименко
Фотопроект Марины Васильевой и Юрия Максименко
 
Kokkinis manos 12-2012
Kokkinis manos 12-2012Kokkinis manos 12-2012
Kokkinis manos 12-2012
 
Copyright and fair use
Copyright and fair useCopyright and fair use
Copyright and fair use
 
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWA
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWAVIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWA
VIAJE, BALI,LOMBOK,JAVA,KARIMUNJAWA
 
7 thgr.chp1feelings
7 thgr.chp1feelings7 thgr.chp1feelings
7 thgr.chp1feelings
 
Spa1 wkbk chap 02
Spa1 wkbk chap 02Spa1 wkbk chap 02
Spa1 wkbk chap 02
 
Ulotka c13 2
Ulotka c13 2Ulotka c13 2
Ulotka c13 2
 
Modul protim 2013 (edisi 3)
Modul protim 2013 (edisi 3)Modul protim 2013 (edisi 3)
Modul protim 2013 (edisi 3)
 
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ
共働き・子持ちの夫婦がコワーキングスペース運営をしてるワケ
 
Beckman_outliers final project
Beckman_outliers final projectBeckman_outliers final project
Beckman_outliers final project
 
Husos granulom d i n 1045
Husos granulom d i n 1045Husos granulom d i n 1045
Husos granulom d i n 1045
 
Николай Петрович Румянцев в литературе
Николай Петрович Румянцев в литературеНиколай Петрович Румянцев в литературе
Николай Петрович Румянцев в литературе
 
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...
Marketing v sociálních médích | 2. hodina | Kolik sítí znáš | Typologie sociá...
 

Similar to Running native code on Android #OSDCfr 2012

Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Paris Android User Group
 
Android OS Porting: Introduction
Android OS Porting: IntroductionAndroid OS Porting: Introduction
Android OS Porting: IntroductionJollen Chen
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development KitPeter R. Egli
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application DevelopmentRamesh Prasad
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotoolsThierry Gayet
 
Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDKBeMyApp
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)DroidConTLV
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)Ron Munitz
 
Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel ToolsXavier Hallade
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Xavier Hallade
 
Cross-compilation native sous android
Cross-compilation native sous androidCross-compilation native sous android
Cross-compilation native sous androidThierry Gayet
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - IntroductionRakesh Jha
 
JNA - Let's C what it's worth
JNA - Let's C what it's worthJNA - Let's C what it's worth
JNA - Let's C what it's worthIdan Sheinberg
 
NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)Ron Munitz
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building BlocksMax Kleiner
 

Similar to Running native code on Android #OSDCfr 2012 (20)

Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
 
Android OS Porting: Introduction
Android OS Porting: IntroductionAndroid OS Porting: Introduction
Android OS Porting: Introduction
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application Development
 
From gcc to the autotools
From gcc to the autotoolsFrom gcc to the autotools
From gcc to the autotools
 
olibc: Another C Library optimized for Embedded Linux
olibc: Another C Library optimized for Embedded Linuxolibc: Another C Library optimized for Embedded Linux
olibc: Another C Library optimized for Embedded Linux
 
Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDK
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)NDK Primer (Wearable DevCon 2014)
NDK Primer (Wearable DevCon 2014)
 
Android on IA devices and Intel Tools
Android on IA devices and Intel ToolsAndroid on IA devices and Intel Tools
Android on IA devices and Intel Tools
 
Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
Cross-compilation native sous android
Cross-compilation native sous androidCross-compilation native sous android
Cross-compilation native sous android
 
Android ndk - Introduction
Android ndk  - IntroductionAndroid ndk  - Introduction
Android ndk - Introduction
 
JNA - Let's C what it's worth
JNA - Let's C what it's worthJNA - Let's C what it's worth
JNA - Let's C what it's worth
 
Android ndk
Android ndkAndroid ndk
Android ndk
 
Android NDK
Android NDKAndroid NDK
Android NDK
 
NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)NDK Primer (AnDevCon Boston 2014)
NDK Primer (AnDevCon Boston 2014)
 
Understanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual MachineUnderstanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual Machine
 
DLL Design with Building Blocks
DLL Design with Building BlocksDLL Design with Building Blocks
DLL Design with Building Blocks
 

Running native code on Android #OSDCfr 2012