Android ndk 入门4 - C++实现
16lz
2021-01-23
新建工程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();}
修改activity_main.xml:
<?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>
修改MainActivity.java:
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()); }}
修改local.propeties,加入:
ndk.dir=/opt/android-ndk-r10e
修改gradle.properties,加入:
android.useDeprecatedNdk=true
修改app/build.gradle,加入:
android { defaultBuild { ndk { moduleName "hello-jni-c++" stl "stlport_shared" ldLibs "log" } }}
点击MyNDK.java,右键->External Tools->javah,生成com_zj_ndktest3_MyNDK.h:
/* 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 __cplusplusextern "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
新建main.cpp:
#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");}
新建MyLog.h:
#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
新建jni/Android.mk:
LOCAL_PATH := ${call my-dir}include $(CLEAR_VARS)LOCAL_MODULE := hello-jni-c++LOCAL_SRC_FILES := main.cppinclude ${BUILD_SHARED_LIBRARY}
运行:
##################################################################
网上找到两种实现C++的方法:
1.在Application.mk上加入:
APP_STL := stlport_shared
2.在app/build.gradle上加入:
android { defaultConfig { ndk { stl "stlport_shared" } }}
更多相关文章
- android ndk 入门 - 一个简单的ndk工程
- android带图片的AlertDialog和文件管理器(代码)
- android GridView实现选中图片放大。
- android junit入门(一) JUNIT测试
- Android官方入门文档[5]建立操作栏
- Android中由于设置的背景图片太大导致OOM