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 <jni.h>
#include <string> #include <ghttp/ghttp.h> #include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HttpRequest.h>
#include <Poco/Net/HttpResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/NetException.h>
#include <Poco/BinaryReader.h>
#include <Poco/URI.h>
#include <string> 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<char> eos;
std::string s(std::istreambuf_iterator<char>(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());
}

android开发-c++代码调用so库的更多相关文章

  1. 36个Android开发常用代码片段

    //36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...

  2. Android开发_如何调用系统默认浏览器访问

    Android开发_如何调用系统默认浏览器访问 2015-10-20 17:53 312人阅读  http://blog.sina.com.cn/s/blog_6efce07e010142w7.htm ...

  3. Android开发中怎样调用系统Email发送邮件(多种调用方式)

    在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...

  4. Android开发之经常使用开源库直接拿来用

    1.from  代码家 整理比較好的源代码连接 **************************************************************************** ...

  5. Android 开发有用代码积累

    Android开发需求变化快,开发周期要求尽量短,接下来一系列文章从实际使用出发总结一些常用的代码片段,便于查找,也为后来人提供一份参考. 1.获取Manifest的基本信息(升级页面和软件关于页面一 ...

  6. android 开发必用的开源库

    LogReport:  https://github.com/wenmingvs/LogReport,   崩溃日志上传框架 wcl-permission-demo:Android 6.0 - 动态权 ...

  7. Android开发系列之调用WebService

    我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...

  8. android使用C/C++调用SO库

    有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用.这样人家反编译你的apk,就知道你侵犯了人家的版权.为了达到混淆的目的,我们可以 ...

  9. Android开发学习之路-机器学习库(图像识别)、百度翻译

    对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...

随机推荐

  1. 强大!HTML5 3D美女图片旋转实现教程

    又到周末,来弄点HTML5的特效玩玩,今天要折腾的是HTML5 3D图片特效,图片在垂直方向上被分割成一条条小矩形,在图片上拖动鼠标即可让每一个小矩形旋转,从而让图片形成3D立体的效果,来看看效果图: ...

  2. 磁盘格式化/磁盘挂载/手动增加swap空间

    4.5/4.6 磁盘格式化 4.7/4.8 磁盘挂载 4.9 手动增加swap空间 磁盘格式化 查看centos7支持的文件系统格式 cat  /etc/filesystem,centos7默认的文件 ...

  3. rdlc报表 矩阵控件下的按组分页

    场景: 使用rdlc开发报表,例如订单产品报表,显示多个订单,一个订单有动态生成的固定的多个产品组成,同时统计每个订单里多个产品数量总数. 数据库层面分析: 此报表属于交叉报表,例如5个订单,3个产品 ...

  4. 文件传输协议(FTP,SFTP,SCP)(修改中)

    FTP(File Transfer Protocol):是TCP/IP网络上两台计算机传送文件的协议,FTP是在TCP/IP网络和INTERNET上最早使用的协议之一,它属于网络协议组的应用层.FTP ...

  5. webGL 光照

    1.着色(shading) 在三维图形学术语“着色”的真正含义就是,根据光照条件重建“物体各表面明暗不一的效果”的过程.明白着色过程,需要考虑两件事:    1.发出光线的光源类型.    2.物体表 ...

  6. 【2018年12月10日】A股最便宜的股票

    新钢股份(SH600782) - 当前便宜指数:196.21 - 滚动扣非市盈率PE:2.86 - 动态市净率PB:0.95 - 动态年化股息收益率:1.78% - 新钢股份(SH600782)的历史 ...

  7. Android系统自带样式(android:theme)

    Theme.Dialog : (图1)Activity显示为对话框模式 Theme.NoTitleBar : (图2)不显示应用程序标题栏 Theme.NoTitleBar.Fullscreen : ...

  8. Java多线程之可见性与原子性——synchronized VS volatile

    <转:http://blog.csdn.net/uniquewonderq/article/details/48113071> 程序举例: 代码: package com.synch; p ...

  9. ios开发之--tableview刷新某一个区和某一行

    在开发中,有时候,我们不需要刷新整个表,只需要刷新局部数据即可,具体代码如下: //section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWith ...

  10. python的httplib、urllib和urllib2的区别及用

    慢慢的把它们总结一下,总结就是最好的学习方法 宗述 首先来看一下他们的区别 urllib和urllib2 urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Re ...