Please refer to https://www.youtube.com/watch?v=0fEtrekNcOo.
First, download NDK and install it. Then, launch Android to create a Hello World app. Then,
1) Open the Activity class, then add following code below inside this class.
public native String HelloJNI();
static
{
System.loadLibrary("HelloJNI");
}
2) Then, within Android Studio, launch Android Terminal,
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
javah -d jni -classpath C:\Users\Qingxu_Li\AppData\Local\Android\sdk\android-sdk\platforms\android-21\android.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v4.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar;..\..\build\intermediates\classes\debug com.ebookfrenzy.hellojni.HelloJNIActivity
3) Now, under HelloJNI\app\src\main\jni, you can see a com_ebookfrenzy_hellojni_HelloJNIActivity.h file generated by the javah command above.
4) Create a HelloJNI.c file under HelloJNI\app\src\main\jni. The file content is below:
#include "com_ebookfrenzy_hellojni_HelloJNIActivity.h"
JNIEXPORT jstring JNICALL Java_com_ebookfrenzy_hellojni_HelloJNIActivity_HelloJNI
(JNIEnv *env, jobject obj)
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
5) Change your activity's onCreate(...) method as below.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
TextView tv = new TextView(this);
tv.setText( HelloJNI() );
setContentView(tv);
}
6) In your gradle\local.properties file, add following line. This is where your NDK is installed.
ndk.dir=C\:\\Users\\Qingxu_Li\\AppData\\Local\\Android\\android-ndk-r10d
7) In your app\build.gradle file, add the ndk section below.
defaultConfig {
applicationId "com.ebookfrenzy.hellojni"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "HelloJNI"
}
}
8) Under HelloJNI\app\src\main\jni, create a new file Android.mk file as below.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJNI
LOCAL_SRC_FILES := HelloJNI.c
include $(BUILD_SHARED_LIBRARY)
8) Under HelloJNI\app\src\main\jni, create a new file Application.mk file as below.
APP_ABI := all
9) In app\build.gradle file, add the sourceSets.main as below. This section should be under the "android" node.
sourceSets.main
{
jni.srcDirs = [] // ndk-build.cmd needs to be invoked from command line
jniLibs.srcDir "src/main/libs"
}
10) With Android Studio, go to Android Terminal.
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
ndk-build
11) Then, run it!
First, download NDK and install it. Then, launch Android to create a Hello World app. Then,
1) Open the Activity class, then add following code below inside this class.
public native String HelloJNI();
static
{
System.loadLibrary("HelloJNI");
}
2) Then, within Android Studio, launch Android Terminal,
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
javah -d jni -classpath C:\Users\Qingxu_Li\AppData\Local\Android\sdk\android-sdk\platforms\android-21\android.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v4.jar;C:\Users\Qingxu_Li\AppData\Local\Android\sdk\extras\android\support\v7\appcompat\libs\android-support-v7-appcompat.jar;..\..\build\intermediates\classes\debug com.ebookfrenzy.hellojni.HelloJNIActivity
3) Now, under HelloJNI\app\src\main\jni, you can see a com_ebookfrenzy_hellojni_HelloJNIActivity.h file generated by the javah command above.
4) Create a HelloJNI.c file under HelloJNI\app\src\main\jni. The file content is below:
#include "com_ebookfrenzy_hellojni_HelloJNIActivity.h"
JNIEXPORT jstring JNICALL Java_com_ebookfrenzy_hellojni_HelloJNIActivity_HelloJNI
(JNIEnv *env, jobject obj)
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
5) Change your activity's onCreate(...) method as below.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* Create a TextView and set its content.
* the text is retrieved by calling a native
* function.
*/
TextView tv = new TextView(this);
tv.setText( HelloJNI() );
setContentView(tv);
}
6) In your gradle\local.properties file, add following line. This is where your NDK is installed.
ndk.dir=C\:\\Users\\Qingxu_Li\\AppData\\Local\\Android\\android-ndk-r10d
7) In your app\build.gradle file, add the ndk section below.
defaultConfig {
applicationId "com.ebookfrenzy.hellojni"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "HelloJNI"
}
}
8) Under HelloJNI\app\src\main\jni, create a new file Android.mk file as below.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJNI
LOCAL_SRC_FILES := HelloJNI.c
include $(BUILD_SHARED_LIBRARY)
8) Under HelloJNI\app\src\main\jni, create a new file Application.mk file as below.
APP_ABI := all
9) In app\build.gradle file, add the sourceSets.main as below. This section should be under the "android" node.
sourceSets.main
{
jni.srcDirs = [] // ndk-build.cmd needs to be invoked from command line
jniLibs.srcDir "src/main/libs"
}
10) With Android Studio, go to Android Terminal.
cd C:\PublicSourceCode\AndroidStudioSamples\HelloJNI\app\src\main
ndk-build
11) Then, run it!