博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android开发-c++代码调用so库
阅读量:6991 次
发布时间:2019-06-27

本文共 4984 字,大约阅读时间需要 16 分钟。

Android项目的CMakeLists.txt代码如下,so文件放在项目的$Project/app/src/main/jniLibs/$arch下, $arch替换为arm64-v8a armv7a等
cmake_minimum_required(VERSION 3.4.1) set(ARCH arch-arm64) set(libs_DIR /Users/musictom/libs/android/${ARCH}/usr/lib) set(libs_include_DIR /Users/musictom/libs/android/${ARCH}/usr/include)
add_library(ghttp-lib SHARED IMPORTED) set_target_properties(ghttp-lib PROPERTIES IMPORTED_LOCATION              ${libs_DIR}/libghttp.so)
add_library( # Sets the name of the library.              native-lib              # Sets the library as a shared library.              SHARED              # Provides a relative path to your source file(s).              src/main/cpp/native-lib.cpp )
target_link_libraries( # Specifies the target library.                        native-lib my-lib ghttp-lib pocofoundation-lib poconet-lib pocoutil-lib                        # Links the target library to the log library                        # included in the NDK.                        ${log-lib} ) app.gradle如下:
apply plugin: 'com.android.application' android {
compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig {
applicationId "com.example.musictom.andcpp" minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions -I/Users/musictom/libs/android/arch-arm64/usr/include/" } } ndk {
// Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. //'x86', 'x86_64', abiFilters 'arm64-v8a' } // 设置so文件路径 so文件如果不在jniLibs目录下,需要在这里指定目录 //sourceSets {
// main {
// jniLibs.srcDirs = ['src/main/jni/arm64-v8a'] // } //} } buildTypes {
release {
minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild {
cmake {
path "CMakeLists.txt" } } } dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7' testCompile 'junit:junit:4.12' } native-lib.cpp代码如下:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
std::string get_ghttp() { /* This is the http request object */ ghttp_request *request = NULL; /* Allocate a new empty request object */ request = ghttp_request_new(); /* Set the URI for the request object */ ghttp_set_uri(request, "http://www.fuhan.org/wechat/verify"); /* Close the connection after you are done. */ ghttp_set_header(request, http_hdr_Connection, "close"); /* Prepare the connection */ ghttp_prepare(request); /* Process the request */ ghttp_process(request); /* Write out the body. Note that the body of the request may not be null terminated so we have to be careful of the length. */ //fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout); char* buf = ghttp_get_body(request); std::string s = buf; /* Destroy the request. This closes any file descriptors that may be open and will free any memory associated with the request. */ ghttp_request_destroy(request); return s; } extern std::string aa(); extern "C" JNIEXPORT jstring JNICALL Java_com_example_musictom_andcpp_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; hello = aa(); std::string result; std::string responseText; if (false) { try { Poco::URI url("http://www.fuhan.org"); Poco::Net::HTTPClientSession session(url.getHost(), url.getPort()); //Poco::Net::HTTPClientSession s("www.cnblogs.com"); //Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/auroratony/archive/2012/06/06/2537516.html"); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/wechat/verify", Poco::Net::HTTPRequest::HTTP_1_1); //request.set("/wechat/verify", ""); //Poco::Net::HTMLForm form; //form.add("entry1", "value1"); //form.prepareSubmit(request); session.sendRequest(request); Poco::Net::HTTPResponse response; std::istream &rs = session.receiveResponse(response); //Poco::StreamCopier copier; //copier.copyToString(rs, responseText); std::istreambuf_iterator
eos; std::string s(std::istreambuf_iterator
(rs), eos); responseText = s; } catch (Poco::Net::NetException &ex) { result = ex.displayText(); responseText = "error:" + result; } } else { responseText = get_ghttp(); } return env->NewStringUTF(responseText.c_str()); }
你可能感兴趣的文章
Tesseract 引擎翻译
查看>>
Android之复选框对话框
查看>>
【RabbitMQ系列】队列、绑定、交换器
查看>>
Run as ant build每次都执行两次
查看>>
如何在微信公众号下载保存图片??
查看>>
Spring读书笔记——bean解析
查看>>
算法练习(5)数字列表中 连续最大的和
查看>>
C# 导出 不保存 直接显示
查看>>
bzoj4445&&dtoj#2348. 小凸想跑步(convex)
查看>>
常见模块设计--权限管理(一)
查看>>
[Docker]容器镜像
查看>>
stl学习之模板
查看>>
元学习 - Learning How to Learn - 第一课:集中与发散思维
查看>>
一种具有细节保留功能的磨皮算法。
查看>>
排序算法7--选择排序--堆排序
查看>>
iOS开发规范&建议
查看>>
[原]如何为SqlServer2008数据库分配用户
查看>>
【leetcode】Basic Calculator III
查看>>
回归到jquery
查看>>
Visual Studio 2008常见问题
查看>>