标签:
首先下载NDK 然后在最外层的
local.properties文件夹中 写上 ndk的路径
ndk.dir=/Users/wyj/Desktop/androidStudioSdk/android-ndk-r10d
1.新建工程myapp
2.在MainActivity中写好
1 public static native String stringFromJNI();
2.生成对应的class文件
右键工程 Make Module ‘myapp‘ 会生成 build文件夹 旗下很多 *.class
3.生成native对应的 .h 文件
注意MainActivity.class是个二进制文件,当前文件夹目录是myapp
终端下切换到myapp下 执行 要带上android.jar,因为 Activity中一些类需要android.jar中的类
mac下(:)
javah -classpath build/intermediates/classes/debug:/Users/wyj/Desktop/a/android.jar -d jni com.example.wyj.myapplication.MainActivity
windows下(;)
javah -classpath bin\\classes;F:\\aa\\android.jar -d jni com.example.wyj.myapplication.MainActivity
其中Android.mk的内容是 其中:
LOCAL_MODULE := hello 生成的库名 生成 libhello.so
LOCAL_SRC_FILES := hello.c 指定哪个文件 都是.c文件
1 # Copyright (C) 2009 The Android Open Source Project 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 LOCAL_PATH := $(call my-dir) 16 17 include $(CLEAR_VARS) 18 19 LOCAL_MODULE := hello 20 LOCAL_SRC_FILES := hello.c 21 22 include $(BUILD_SHARED_LIBRARY)
com_example_wyj_myapplication_MainActivity.h的内容如下
1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class com_example_wyj_myapplication_MainActivity */ 4 5 #ifndef _Included_com_example_wyj_myapplication_MainActivity 6 #define _Included_com_example_wyj_myapplication_MainActivity 7 #ifdef __cplusplus 8 extern "C" { 9 #endif 10 #undef com_example_wyj_myapplication_MainActivity_BIND_ABOVE_CLIENT 11 #define com_example_wyj_myapplication_MainActivity_BIND_ABOVE_CLIENT 8L 12 #undef com_example_wyj_myapplication_MainActivity_BIND_ADJUST_WITH_ACTIVITY 13 #define com_example_wyj_myapplication_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L 14 #undef com_example_wyj_myapplication_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 15 #define com_example_wyj_myapplication_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L 16 #undef com_example_wyj_myapplication_MainActivity_BIND_AUTO_CREATE 17 #define com_example_wyj_myapplication_MainActivity_BIND_AUTO_CREATE 1L 18 #undef com_example_wyj_myapplication_MainActivity_BIND_DEBUG_UNBIND 19 #define com_example_wyj_myapplication_MainActivity_BIND_DEBUG_UNBIND 2L 20 #undef com_example_wyj_myapplication_MainActivity_BIND_IMPORTANT 21 #define com_example_wyj_myapplication_MainActivity_BIND_IMPORTANT 64L 22 #undef com_example_wyj_myapplication_MainActivity_BIND_NOT_FOREGROUND 23 #define com_example_wyj_myapplication_MainActivity_BIND_NOT_FOREGROUND 4L 24 #undef com_example_wyj_myapplication_MainActivity_BIND_WAIVE_PRIORITY 25 #define com_example_wyj_myapplication_MainActivity_BIND_WAIVE_PRIORITY 32L 26 #undef com_example_wyj_myapplication_MainActivity_CONTEXT_IGNORE_SECURITY 27 #define com_example_wyj_myapplication_MainActivity_CONTEXT_IGNORE_SECURITY 2L 28 #undef com_example_wyj_myapplication_MainActivity_CONTEXT_INCLUDE_CODE 29 #define com_example_wyj_myapplication_MainActivity_CONTEXT_INCLUDE_CODE 1L 30 #undef com_example_wyj_myapplication_MainActivity_CONTEXT_RESTRICTED 31 #define com_example_wyj_myapplication_MainActivity_CONTEXT_RESTRICTED 4L 32 #undef com_example_wyj_myapplication_MainActivity_MODE_APPEND 33 #define com_example_wyj_myapplication_MainActivity_MODE_APPEND 32768L 34 #undef com_example_wyj_myapplication_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 35 #define com_example_wyj_myapplication_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L 36 #undef com_example_wyj_myapplication_MainActivity_MODE_MULTI_PROCESS 37 #define com_example_wyj_myapplication_MainActivity_MODE_MULTI_PROCESS 4L 38 #undef com_example_wyj_myapplication_MainActivity_MODE_PRIVATE 39 #define com_example_wyj_myapplication_MainActivity_MODE_PRIVATE 0L 40 #undef com_example_wyj_myapplication_MainActivity_MODE_WORLD_READABLE 41 #define com_example_wyj_myapplication_MainActivity_MODE_WORLD_READABLE 1L 42 #undef com_example_wyj_myapplication_MainActivity_MODE_WORLD_WRITEABLE 43 #define com_example_wyj_myapplication_MainActivity_MODE_WORLD_WRITEABLE 2L 44 #undef com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_DIALER 45 #define com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_DIALER 1L 46 #undef com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_DISABLE 47 #define com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_DISABLE 0L 48 #undef com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 49 #define com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L 50 #undef com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 51 #define com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L 52 #undef com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SHORTCUT 53 #define com_example_wyj_myapplication_MainActivity_DEFAULT_KEYS_SHORTCUT 2L 54 #undef com_example_wyj_myapplication_MainActivity_RESULT_CANCELED 55 #define com_example_wyj_myapplication_MainActivity_RESULT_CANCELED 0L 56 #undef com_example_wyj_myapplication_MainActivity_RESULT_FIRST_USER 57 #define com_example_wyj_myapplication_MainActivity_RESULT_FIRST_USER 1L 58 #undef com_example_wyj_myapplication_MainActivity_RESULT_OK 59 #define com_example_wyj_myapplication_MainActivity_RESULT_OK -1L 60 /* 61 * Class: com_example_wyj_myapplication_MainActivity 62 * Method: stringFromJNI 63 * Signature: ()Ljava/lang/String; 64 */ 65 JNIEXPORT jstring JNICALL Java_com_example_wyj_myapplication_MainActivity_stringFromJNI 66 (JNIEnv *, jclass); 67 68 #ifdef __cplusplus 69 } 70 #endif 71 #endif
自己创建个 hello.c
1 #include <stdio.h> 2 #include <string.h> 3 #include "com_example_wyj_myapplication_MainActivity.h" 4 5 JNIEXPORT jstring JNICALL Java_com_example_wyj_myapplication_MainActivity_stringFromJNI 6 (JNIEnv *env, jclass jclass){ 7 return (*env)->NewStringUTF(env, "Hello from wf! "); 8 }
4.生成 so文件
终端在jni的上一级目录中 执行 ndk-build (ndk-build所在路径中的文件夹不得存在空格否则就报错了)
5.使用 so文件
在main中建立 jniLibs文件夹 然后把 armeabi/*.so 拷贝进去 然后就可以访问了。
或者不新建jniLibs文件夹,那么在build.gradle中
android {
sourceSets {
main {
jniLibs.srcDirs = [‘xxx‘]
}
}
}
jniLibs.srcDirs = [‘xxx‘] //这里xxx代表一个目录。可以改变jniLibs的读取路径 , 默认的 jniLibs结构是 src/main/jniLibs
指定xxx是libs就可以了armeabi不需要在路径中指定,它是不同设备下的自动会被寻找的。
6.在MainActivity中 写上
public static native String stringFromJNI(); static { System.loadLibrary("hello"); //装载库文件 } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str=stringFromJNI(); //可被使用 System.out.print(str); }
7.运行程序就可以 stringFromJNI 就可以看出结果了
额外说明:
.c文件中的函数为
1 JNIEXPORT jstring JNICALL Java_com_example_wyj_myapplication_MainActivity_stringFromJNI
类的结构 是 com.example.wyj.myapplication 文件包结构的 MainActivity类 下面的 stringFromJNI 函数才能被识别并使用
放入其它工程如果不是遵从这个结构的 会报错,函数找不到,当然*.so可以加载。
如果其他工程是遵守如下结构的当然也能识别出stringFromJNI函数并使用
标签:
原文地址:http://www.cnblogs.com/friends-wf/p/4729662.html