Android NDK的一个简单的例子,使用JNI进行Java和C的交互,带你进入Android底层编程世界
-
public class Java2CJNI { public native String java2C(); }
-
在项目根目录下,进入main->java目录,全选文件目录栏,直接输入cmd命令并按回车键进入docs命令
javah -classpath . -jni -d jni com.example.chengzj.ndk.simple.Java2CJNI
-
#include "com_example_chengzj_ndk_simple_Java2CJNI.h" JNIEXPORT jstring JNICALL Java_com_example_chengzj_ndk_simple_Java2CJNI_java2C (JNIEnv *env, jobject jobj){ return (*env)->NewStringUTF(env,"i am from native C."); }
-
LOCAL_PATH := $(call my-dir) //固定写法,把路径赋给LOCAL_PATH变量
include $(CLEAR_VARS) //清除其他LOCAL变量
LOCAL_MODULE := Java2C //这个模块的名字,最后生成的.so的名字就是它
LOCAL_SRC_FILES := Java2C.c //这里是要编译的文件
include $(BUILD_SHARED_LIBRARY) //SHARED_LIBRARY就是动态库,即.so文件
-
在项目的工具类中选择Build->Rebuild Project,进行重新编译工程,然后AS会为我们生成so文件,so文件所在目录为:app\build\intermediates\ndk\debug\lib下
ndk { moduleName "Java2C" //so文件名 abiFilters "armeabi", "armeabi-v7a", "x86" //CPU类型 }
-
static { System.loadLibrary("Java2C"); }
-
String result = new Java2CJNI().java2C(); Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();
ImageDownLoader 手写的三级缓存框架二百多行代码搞定图片缓存
- Github: github.com/cheng2016
- Email: mitnick.cheng@outlook.com
- QQ: 1102743539
- CSDN: souls0808
Copyright 2016 cheng2016,Inc.
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.