新建工程NDKTest3,新建活动MainActivity
新建MyNDK.java:
package com.zj.ndktest3; /** * Created by root on 15-11-26. */ public class MyNDK { static { System.loadLibrary("hello-jni-c++"); } public static native String hello(); }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.zj.ndktest3.MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
package com.zj.ndktest3; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mTextView = (TextView)findViewById(R.id.text_view); mTextView.setText(MyNDK.hello()); } }
ndk.dir=/opt/android-ndk-r10e
android.useDeprecatedNdk=true
android { defaultBuild { ndk { moduleName "hello-jni-c++" stl "stlport_shared" ldLibs "log" } } }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_zj_ndktest3_MyNDK */ #ifndef _Included_com_zj_ndktest3_MyNDK #define _Included_com_zj_ndktest3_MyNDK #ifdef __cplusplus extern "C" { #endif /* * Class: com_zj_ndktest3_MyNDK * Method: hello * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_zj_ndktest3_MyNDK_hello (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
#include <jni.h> #include <iostream> #include <string.h> #include "com_zj_ndktest3_MyNDK.h" #include "MyLog.h" using namespace std; JNIEXPORT jstring JNICALL Java_com_zj_ndktest3_MyNDK_hello (JNIEnv *env, jclass cla) { LOGI("function begins"); string ret; LOGI("ok"); char s[] = "Golden Global View,disk * desk"; const char *d = " ,*"; LOGI("next step is define char *p=NULL"); char *p=NULL; LOGI("every thing is ok"); p = strtok(s,d); int i=0; LOGI("previous step is ok"); while(p){ ret += p; ret += " "; p=strtok(NULL,d); } return env->NewStringUTF(ret.c_str()); //return env->NewStringUTF("hello jni"); }
#ifndef _MYLOG_H_ #define _MYLOG_H_ #include <android/log.h> #define TAG "test" #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) #endif
LOCAL_PATH := ${call my-dir} include $(CLEAR_VARS) LOCAL_MODULE := hello-jni-c++ LOCAL_SRC_FILES := main.cpp include ${BUILD_SHARED_LIBRARY}
##################################################################
网上找到两种实现C++的方法:
1.在Application.mk上加入:
APP_STL := stlport_shared
2.在app/build.gradle上加入:
android { defaultConfig { ndk { stl "stlport_shared" } } }