ndk操作stl库

1、创建android工程,ndk工具类,生成头文件

      activity代码:

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=findViewById(R.id.sample_text);
        tv.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sample_text:
                 StlUtils.stlApply();
                break;
        }
    }
}

      ndk工具类的StlUtils

    

public class StlUtils {
    static {
        System.loadLibrary("stlib");
    }
    public static  native  void  stlApply();
}

  生成头文件

#include <jni.h>
/* Header for class dh_fcw_cn_myapplication_StlUtils */
#ifndef _Included_dh_fcw_cn_myapplication_StlUtils
#define _Included_dh_fcw_cn_myapplication_StlUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     dh_fcw_cn_myapplication_StlUtils
 * Method:    stlApply
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_dh_fcw_cn_myapplication_StlUtils_stlApply
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

二、配置Cmake编译

    创建CMakeLists文件 


cmake_minimum_required(VERSION 3.4.1)
add_library( stlib  SHARED src/main/jni/stlib.cpp )
find_library( log-lib  log )
target_link_libraries( stlib  ${log-lib} )

  buildgradle配置

  

 defaultConfig {
        applicationId "dh.fcw.cn.myapplication"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk{
             abiFilters("armeabi-v7a","x86")
             ldLibs("android","log")
             stl="stlport_static"
        }
        externalNativeBuild {
            cmake { cppFlags "-std=c++14"}
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

3.编写c++代码

    在jni 文件夹下创建stlib.cp

    

#include "dh_fcw_cn_myapplication_StlUtils.h"
#include <jni.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <android/log.h>
#define random(x) (rand()%x)
#define TAG "MyLog"
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
using namespace std;

class  Student{
   public:
    Student(){}
    ~Student(){}
    string  name;
    int    age;
    string address;
    int    index;
    void printStuInfo(){
        LOGI("姓名=%s,年齡=%d,地址=%s,序號=%d",name.c_str(),age,address.c_str(),index);
    }
};

typedef struct   Person{
    char*  name;
    int    age;
    char*  address;
    int    index;

} Per;

void printPersonInfo( Per* p){
    char* name=p->name;
    int age=p->age;
    char* address=p->address;
    int index=p->index;
    LOGI("姓名=%s,年齡=%d,地址=%s,序號=%d",name,age,address,index);
}

JNIEXPORT void JNICALL Java_dh_fcw_cn_myapplication_StlUtils_stlApply(JNIEnv * jniEnv, jclass obj){
    try {
        vector<string>  list;
        string str="ABCDEFGLHGKLMNOPQRSTUVWXYZ";
        for(int i=0;i<10;i++){
            srand(static_cast<unsigned int>((int)time(0)));
            int start=random(15);
            string t= str.substr(start,25);
            list.push_back(t);
        }

        for(int i=0;i<list.size();i++){
            string  r=list[i];
            LOGI("r=%s",r.c_str());
        }
    }catch (const char* msg ){
       LOGE("ERR=%s",msg);
    }


    try {
        vector<Student>  listStu;
        for(int i=0;i<10;i++){
            Student stu1;
            stu1.name="劉翠";
            stu1.age=i*3;
            stu1.address="河南開封蘭考";
            stu1.index=i;
            listStu.push_back(stu1);
        }

        for(int i=0;i<listStu.size();i++){
            Student  s=listStu[i];
            s.printStuInfo();
        }
    }catch (const char* msg ){
        LOGE("ERR=%s",msg);
    }


    try {
        vector<Per>  listPerson;
        for(int i=0;i<10;i++){
            Per *p=NULL;
            Per  per;
            p=&per;
            p->name="刘萍";
            p->address="河南信阳新县";
            p->index=i;
            p->age=i*4;
            listPerson.push_back(per);
        }

        for(int i=0;i<listPerson.size();i++){
               Per per=listPerson[i];
               Per* p=NULL;
               p=&per;
               printPersonInfo(p);
        }
    }catch (const char* msg ){
        LOGE("ERR=%s",msg);
    }


}


运行结果:

  

08-08 20:01:48.237 3795-3795/dh.fcw.cn.myapplication I/MyLog: r=LHGKLMNOPQRSTUVWXYZ
08-08 20:01:48.238 3795-3795/dh.fcw.cn.myapplication I/MyLog: r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
    r=LHGKLMNOPQRSTUVWXYZ
08-08 20:01:48.239 3795-3795/dh.fcw.cn.myapplication I/MyLog: 姓名=劉翠,年齡=0,地址=河南開封蘭考,序號=0
    姓名=劉翠,年齡=3,地址=河南開封蘭考,序號=1
    姓名=劉翠,年齡=6,地址=河南開封蘭考,序號=2
    姓名=劉翠,年齡=9,地址=河南開封蘭考,序號=3
    姓名=劉翠,年齡=12,地址=河南開封蘭考,序號=4
    姓名=劉翠,年齡=15,地址=河南開封蘭考,序號=5
    姓名=劉翠,年齡=18,地址=河南開封蘭考,序號=6
    姓名=劉翠,年齡=21,地址=河南開封蘭考,序號=7
    姓名=劉翠,年齡=24,地址=河南開封蘭考,序號=8
    姓名=劉翠,年齡=27,地址=河南開封蘭考,序號=9
    姓名=刘萍,年齡=0,地址=河南信阳新县,序號=0
    姓名=刘萍,年齡=4,地址=河南信阳新县,序號=1
    姓名=刘萍,年齡=8,地址=河南信阳新县,序號=2
    姓名=刘萍,年齡=12,地址=河南信阳新县,序號=3
    姓名=刘萍,年齡=16,地址=河南信阳新县,序號=4
    姓名=刘萍,年齡=20,地址=河南信阳新县,序號=5
    姓名=刘萍,年齡=24,地址=河南信阳新县,序號=6
    姓名=刘萍,年齡=28,地址=河南信阳新县,序號=7
    姓名=刘萍,年齡=32,地址=河南信阳新县,序號=8
08-08 20:01:48.240 3795-3795/dh.fcw.cn.myapplication I/MyLog: 姓名=刘萍,年齡=36,地址=河南信阳新县,序號=9

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值