本文共 2596 字,大约阅读时间需要 8 分钟。
配置javah 、ndk-build、ndk-build clean这三个命令
Program: $JDKPath$/bin/javahParameters: -classpath $Classpath$ -v -jni $FileClass$Working directory: $SourcepathEntry$\..\jni
Program: D:\java\androidstudio\sdk\ndk-bundle\ndk-build.cmdParameters: Working directory: $ProjectFileDir$\app\src\main
Program: D:\java\androidstudio\sdk\ndk-bundle\ndk-build.cmdParameters: cleanWorking directory: $ProjectFileDir$\app\src\main
android { ... defaultConfig { ... ndk{ moduleName "test" } sourceSets { main { jni.srcDirs = [] jniLibs.srcDirs = ['libs'] } } } buildTypes { ... }}
android.useDeprecatedNdk=true
public class Test { static { System.loadLibrary("Test"); } public native String getString();}
注:先build下整个工程,然后执行第7步
com_mazaiting_jni_Test.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include/* Header for class com_mazaiting_jni_Test */#ifndef _Included_com_mazaiting_jni_Test#define _Included_com_mazaiting_jni_Test#ifdef __cplusplusextern "C" {#endif/* * Class: com_mazaiting_jni_Test * Method: getString * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_mazaiting_jni_Test_getString (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
Test.cpp
//// Created by Administrator on 2017/10/30.//#include "com_mazaiting_jni_Test.h"JNIEXPORT jstring JNICALL Java_com_mazaiting_jni_Test_getString (JNIEnv *env, jobject){ return (*env).NewStringUTF("This is test jni!");}
Applicaition.mk
APP_MODULES := TestAPP_ABI := all
Android.mk
具体解释请查看LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := TestLOCAL_SRC_FILES := Test.cppinclude $(BUILD_SHARED_LIBRARY)
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = (TextView) this.findViewById(R.id.textView); textView.setText(new Test().getString()); }}
程序运行结果
转载地址:http://oyjko.baihongyu.com/