新建工程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"       }   }}

更多相关文章

  1. 调整 FMX Android(安卓)文字显示「锯齿」效果
  2. android permission Suggestion: add 'tools:replace="android:v
  3. android,进入页面textview默认获得焦点问题,如何取消焦点
  4. Android(安卓)全屏设置
  5. Android(安卓)源码热门改动速查(持续更新.....)
  6. Android(安卓)软键盘遮挡Dialog
  7. Android(安卓)Studio 小提示,新建Activity
  8. android studio 新建项目 界面一直停在 【“building ‘ 项目名
  9. 修改ListView 分割线Seperator line

随机推荐

  1. Android(安卓)Studio debug调试
  2. contentprovider的学习实例总结
  3. Android(安卓)退出activity 的方式总结
  4. Android下DLAN中DMS模块的实现
  5. Robotium第一天:搭建环境测试微信
  6. [置顶] 很实用的android压缩图片的算法
  7. Android复杂自定义Listview实现
  8. android4.0拦截HOME
  9. [置顶] Android(安卓)UI设计之RecyclerVi
  10. error: conversion from 'Test*' to non-