根據上一篇,這裡寫一個簡單demo
工程目錄結構
JniDemo
package com.test.git.jnidemo.JniUtil;
/**
* Created by lk on 16/9/18.
*/
public class JniDemo {
//路徑:/JNIDemo/app/build/intermediates/classes/debug/com/test/git/jnidemo/JniUtil/JniDemo.class
public native String helloJni();
static {
System.loadLibrary("NdkJniDemo");
}
}
com_test_git_jnidemo_JniUtil_JniDemo.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_test_git_jnidemo_JniUtil_JniDemo */
#ifndef _Included_com_test_git_jnidemo_JniUtil_JniDemo
#define _Included_com_test_git_jnidemo_JniUtil_JniDemo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_git_jnidemo_JniUtil_JniDemo
* Method: helloJni
* Signature: ()V
*/
JNIEXPORT jstring JNICALL Java_com_test_git_jnidemo_JniUtil_JniDemo_helloJni
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
JniDemo.cpp
//
// Created by lk on 16/9/18.
//
#include "com_test_git_jnidemo_JniUtil_JniDemo.h"
JNIEXPORT jstring JNICALL Java_com_test_git_jnidemo_JniUtil_JniDemo_helloJni
(JNIEnv *env, jobject obj){
return env->NewStringUTF("hello jni");
}
MainActivity
package com.test.git.jnidemo.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.test.git.jnidemo.JniUtil.JniDemo;
import com.test.git.jnidemo.R;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity-";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JniDemo jd = new JniDemo();
Log.i(TAG, "helloJni->" + jd.helloJni());
}
}