Page MenuHomePhabricator

Android
Updated 1,676 Days AgoPublic

Introduction

This document will help you build the EFLs for the Android operating system.

Android is built upon the Linux kernel, but has basically a whole different stack for everything, and uses little “standard” open-source software.

Of interest is that:

  • the standard C library is Bionic: it is not fully compatible with POSIX, and some POSIX-calls may not be present, some may exist but not work as expected;
  • the display server is SurfaceFlinger.

The endorsed way to have code native code instead of Java is by using JNI (Java Native Interface) and the NDK (Native Development Kit).

Using the NDK, an exemple

Let’s suppose we want to have a native sum function in a libsum library.
We can have:

  • sum.c

    #include "sum.h" int sum(int o1, int o2) { return o1 + o2; }
  • and its interface

    #ifndef SUM_H #define SUM_H int sum(int o1, int o2); #endif
  • jni.c

    #include "tld_domain_sub_NativeCodeInterface.h" #include "sum.h" JNIEXPORT jint JNICALL Java_tld_domain_sub_Sum_nativeSum(JNIEnv *jEnv, jclass jClass, jint o1, jint o2) { return sum(o1, o2); }
  • and its interface

    #ifndef TLD_DOMAIN_SUB_SUM_NATIVE_H #define TLD_DOMAIN_SUB_SUM_NATIVE_H

    #ifdef __cpluscplus extern "C" { #endif

    JNIEXPORT jint JNICALL Java_tld_domain_sub_Sum_nativeSum(JNIEnv *jEnv, jclass jClass, jint o1, jint o2);

    #ifdef __cpluscplus } #endif

    #endif
  • Sum.java

    package tld.domain.sub;

    public class Sum { public static int sum(int o1, int o2) { return nativeSum(o1, o2); }

    public static int nativeSum(int o1, int o2);

    static { System.loadLibrary("libsum"); } }
  • Android.mk

    LOCAL_PATH = $(call my-dir)

    include $(CLEAR_VARS) LOCAL_MODULE := libsum LOCAL_SRC_FILES := sum.c jni.c include $(BUILD_SHARED_LIBRARY)

You can now call Sum.sum on Java code!

Last Author
illogict
Last Edited
May 9 2014, 4:00 AM
Projects
None
Subscribers
seoz