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. C# 窗口自适应窗口宽度和高度

    其实winform并不适合做自适应这玩意的,虽然,能做是能做, 就像菜刀一样能切水果,但是,不是那么合适,毕竟有水果刀 wpf做自适应拉伸窗体,会更好一些. 代码: #region 自适应 priva ...

  2. 纯CSS3实现牛奶般剔透的3D按钮特效

    今天我们要来看一款非常特别的纯CSS3 3D按钮,它的外观酷似纯白剔透的牛奶,点击按钮的时候还会出现一种很柔和的弹力效果.按钮按下时,按钮会轻轻的弹动一下,非常逼真.本文我们在观赏演示的同时,也将源代 ...

  3. 10个样式各异的CSS3 Loading加载动画

    前几天我在园子里分享过2款很酷的CSS3 Loading加载动画,今天又有10个最新的Loading动画分享给大家,这些动画的样式都不一样,实现起来也并不难,你很容易把它们应用在项目中,先来看看效果图 ...

  4. ubuntu14.04_64位安装tensorflow-gpu

    第一步(可直接跳到第二步):安装nvidia显卡驱动 linux用户可以通过官方ppa解决安装GPU驱动的问题.使用如下命令添加Graphic Drivers PPA: sudo add-apt-re ...

  5. Memcached存储机制

    Memcached存储机制 memcached 内存管理 分析(转) 缓存.缓存算法和缓存框架简介 memcached全面剖析–PDF总结篇

  6. Java写 插入 选择 冒泡 快排

    /** * Created by wushuang on 2014/11/19. */ public class SortTest { @Test public void mainTest() { i ...

  7. 简单理解IoC与DI

    为了理解Spring的IoC与DI从网上查了很多资料,作为初学者,下面的描述应该是最详细,最易理解的方式了. 首先想说说IoC(Inversion of Control,控制倒转).这是spring的 ...

  8. js粘贴事件paste简单解析及遇到的坑

    在用户执行粘贴操作的时候,js能够获得剪切板的内容,本文讨论一下这个问题. 目前只有Chrome支持获取剪切板中的图片数据.还好需要这个功能的产品目前只支持Chrome和Safari,一些Chrome ...

  9. JavaBridge

    有的时候我们需要在PHP里调用JAVA平台封装好的jar包里的class类和方法 一般的做法是采用php-java-bridge做桥接 1.实现原理: 先打开java的一个监听端口,php调用java ...

  10. opencv3.2将中文输出到图片上

    opencv自带的putText函数无法输出utf8类型的字符,因此无法将中文打印到图片上.用这篇文章的freetype可以实现中文输出,但是需要将字符解码转码比较麻烦,而Pillow的Image函数 ...