• 定义wrap类,声明native函数,加载库
package com.ndk.hello;

public class Classs {

    public native String say_hello();
static
{
System.loadLibrary("HelloAndroidNDK");
}
}
  • 在项目根目录创建jni文件夹,在此文件夹生成JNI头文件
javah -classpath ../bin/classes com.ndk.hello.Classs
  • 为生成的com_ndk_hello_Classs.h写实现文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_ndk_hello_Classs */ #ifndef _Included_com_ndk_hello_Classs
#define _Included_com_ndk_hello_Classs
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_ndk_hello_Classs
* Method: say_hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_ndk_hello_Classs_say_1hello
(JNIEnv *, jobject); #ifdef __cplusplus
}
#endif
#endif
#include "com_ndk_hello_Classs.h"

JNIEXPORT jstring JNICALL Java_com_ndk_hello_Classs_say_1hello(JNIEnv * env, jobject this)
{
return (*env)->NewStringUTF(env,"Hello Java NDK!");
}
  • 在jni文件夹写Android.mk文件
# Copyright (C)  The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := HelloAndroidNDK
LOCAL_SRC_FILES := com_ndk_hello_Classs.c include $(BUILD_SHARED_LIBRARY)
  • 在jni文件夹中交叉编译mk文件
$NDK/ndk-build
  • 将生成libs/armeabi/libHelloAndroidNDK.so文件
  • 编写安卓框架程序,调用native方法。
package com.ndk.hello;
import com.ndk.hello.Classs;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; public class HelloAndroidNDK extends Activity{
@Override
public void onCreate(Bundle s)
{
super.onCreate(s); Classs c = new Classs(); String say = c.say_hello();
TextView tv = new TextView(this);
tv.setText(say);
setContentView(tv);
}
}

最新文章

  1. C#_技巧:.net下C++调用C#的dll
  2. 深入理解Redis系列
  3. C指针-数组和指针的归一
  4. button标签和input button
  5. Unity3D使用经验总结 优点篇
  6. 使用 xlrd 模块实现对excel 的读取、excel转json 、excel 转 mysql insert 语句
  7. IOS应用沙盒文件操作
  8. 做bbs论坛项目的收获(1)
  9. 创建dblink 同义词
  10. ERROR 2003 (HY000): Can&#39;t connect to MySQL server
  11. ashx中session的使用
  12. JSP/Serlet 使用fileupload上传文件
  13. Linux-基础学习(一)-基本命令
  14. Scrapy基础(三) ------xpath基础
  15. loader 的理解
  16. webpack学习总结(一)
  17. 将tomcat做成windows服务
  18. 【Spring】Spring之依赖注入(DI)传递参数的方式
  19. Publish/Subscribe Model——Notification chain——观察者模式
  20. anaconda资源链接

热门文章

  1. git学习--更新本地仓库单独文件
  2. P1060 开心的金明(动态规划背包问题)
  3. poj3020 建信号塔(匈牙利算法 最小覆盖边集)
  4. POJ 3067 Japan (树状数组 &amp;&amp; 控制变量)
  5. 解决windows server 2008 r2 登录进入桌面只显示一片蓝色背景
  6. LeeCode(5. Longest Palindromic Substring)
  7. python模块之wordcloud
  8. forEach与map
  9. MVC 路由检测组件 Routing Debugger
  10. SparseArray替代HashMap来提高性能