Skip to content
Thiago Meneses edited this page Jan 31, 2019 · 2 revisions

Fork from https://gist.github.com/gnuanu/252fd406f48f7da2c1c7

Prerequisites

Hello World with - JNI - Android Library

  1. GNU/Linux
  2. IntelliJ IDEA
  3. My working directory is $JNI
  4. C++ IDE (QtCreator).
  5. NDK should be installed and ndk-build should be available in $PATH
  6. javah should be available in $PATH

Step 1 - Create The Android Project

  1. Create and Empty Project Named JNI (at location $JNI)
  2. Create a new Android application module named JNIHelloWorld.
  3. Package Name - com.jnisample.JNIHelloWorld
  4. Create one activity JNIActivity and define one native function getNativeMessage()
  5. Build the project

Step 2 - Create the JNI Header file

  1. Create a folder jni inside $JNI/JNIHelloWorld
  2. In Terminal(command line) move to $JNI/out/production/JNIHelloWorld directory (This is where the java files get compiled to class files by IDEA; Verify the directory structure is same as the package name com/jnisample/JNIHelloWorld/JNIActivity.class)
  3. Create the header for the native function using javah

$ javah $JNI/JNIHelloWorld/jni com.jnisample.JNIHelloWorld.JNIActivity
Java 8+ :
$ javac com.jnisample.JNIHelloWorld.JNIActivity.java -h $JNI/JNIHelloWorld/jni
With classpath Android.jar:
$ javah -classpath /home/<USER>/Android/Sdk/platforms/android-XX/android.jar:app/build/classes/ -d $JNI/jni com.jnisample.JNIHelloWorld.JNIActivit

Step 3 - Add a C++ Class

  1. Create a C++ class with name HelloWorld inside of$JNI/JNIHelloWorld/jni
  2. Create define a member function getNativeString() which returns a string

Step 4 - Implement JNI Entry (or Entries)

Implement the entry methodcom_jnisample_JNIHelloWorld_JNIActivity_getNativeMessagedefined in headercom_jnisample_JNIHelloWorld_JNIActivity.h

Step 5 - Configure make

DefineAndroid.mkandApplication.mkinside of$JNI/JNIHelloWorld/jni

Android.mk example:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-world
LOCAL_SRC_FILES := HelloWorld.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk example:
APP_ABI := armeabi-v7a #TARGET ARCH
APP_PLATFORM := android-21

Step 6 - Build Shared Object

  1. In Terminal, move to directory$JNI/JNIHelloWorld
    # Build the Shared Object using the commandndk-build
    $ ndk-build
    On successful build, the Shared Object file libmynative.so will be copied to $JNI/JNIHelloWorld/libs/armeabi-v7a

Step 7 - Build APK

  1. Build the android project in IntelliJ IDEA
    # Deploy and Run

Step 8 - Tests

The vendor interface is validated by the Vendor Test Suite (VTS), which is analogous to the Compatibility Test Suite (CTS). You can use VTS to automate HAL and OS kernel testing in both legacy and current Android architectures.

  • Research
    • VTS
    • CTS