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. 文本处理三剑客之 sed详解

    1.简介 sed是非交互式的编辑器,它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏幕.具体过程如下 ...

  2. Linux账号和权限管理

    一. 用户和组的管理  - Linux中用户种类 种类 特点 root 是管理员,拥有至高无上的权限,不受限制,UID为0 普通用户 管理员创建的用户,受权限限制,UID一般从500开始,可以登录系统 ...

  3. u3d加载加密和未加密

    using UnityEngine; using System.Collections; public class loadnew : MonoBehaviour { public bool IsCo ...

  4. ASP.NET js控制treeview中的checkbox实现单选功能

    ASP.NET js控制treeview中的checkbox实现单选功能 function OnTreeNodeChecked() { var element = window.event.srcEl ...

  5. SSM框架快速整合实例——学生查询

    一.快速准备 SSM 框架即 Spring 框架.SpringMVC 框架.MyBatis 框架,关于这几个框架的基础和入门程序,我前面已经写过几篇文章作为基础和入门介绍了.这里再简单的介绍一下: 1 ...

  6. mysql初始化时报错bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory的处理

    问题描述: 今天新安装了一个linux虚拟机,然后安装mysql 5.7.21,在进行初始化的时候,报错 bin/mysqld: error : cannot open shared object f ...

  7. golang-defer坑的本质

    https://blog.csdn.net/hittata/article/details/77836435

  8. centos7 安装 最新版本的docker

    yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...

  9. [CNN] Face Detection

    即将进入涉及大量数学知识的阶段,先读下“别人家”的博文放松一下. 读罢该文,基本能了解面部识别领域的整体状况. 后生可畏. 结尾的Google Facenet中的2亿数据集,仿佛隐约听到:“你们都玩儿 ...

  10. oracle sqlplus常用命令大全

    show和set命令是两条用于维护SQL*Plus系统变量的命令 SQL> show all --查看所有68个系统变量值 SQL> show user --显示当前连接用户 SQL> ...