android开发-c++代码调用so库
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库的更多相关文章
- 36个Android开发常用代码片段
//36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...
- Android开发_如何调用系统默认浏览器访问
Android开发_如何调用系统默认浏览器访问 2015-10-20 17:53 312人阅读 http://blog.sina.com.cn/s/blog_6efce07e010142w7.htm ...
- Android开发中怎样调用系统Email发送邮件(多种调用方式)
在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...
- Android开发之经常使用开源库直接拿来用
1.from 代码家 整理比較好的源代码连接 **************************************************************************** ...
- Android 开发有用代码积累
Android开发需求变化快,开发周期要求尽量短,接下来一系列文章从实际使用出发总结一些常用的代码片段,便于查找,也为后来人提供一份参考. 1.获取Manifest的基本信息(升级页面和软件关于页面一 ...
- android 开发必用的开源库
LogReport: https://github.com/wenmingvs/LogReport, 崩溃日志上传框架 wcl-permission-demo:Android 6.0 - 动态权 ...
- Android开发系列之调用WebService
我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...
- android使用C/C++调用SO库
有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用.这样人家反编译你的apk,就知道你侵犯了人家的版权.为了达到混淆的目的,我们可以 ...
- Android开发学习之路-机器学习库(图像识别)、百度翻译
对于机器学习也不是了解的很深入,今天无意中在GitHub看到一个star的比较多的库,就用着试一试,效果也还行.比是可能比不上TensorFlow的,但是在Android上用起来比较简单,毕竟Tens ...
随机推荐
- Linux语言设置修改乱码
1.system-config-language 命令语言改成英文.(安装yum install system-config-language) 如何系统安装后,使用的语言不是自己想要的.但是在图形 ...
- CSS3和jQuery实现的自定义美化Checkbox和Radiobox
现在经常可以在网络上看到一些非常奇特的表单元素,例如Checkbox复选框和Radiobox单选框,浏览器默认的样式确实是太丑了,而且更让人蛋疼的是各个浏览器的样式还不统一,考虑到现在越来越多的用户使 ...
- 分布式缓存系统 Memcached 整体架构
分布式缓存系统 Memcached整体架构 Memcached经验分享[架构方向] Memcached 及 Redis 架构分析和比较
- Android使用http协议与服务器通信
网上介绍Android上http通信的文章很多,不过大部分只给出了实现代码的片段,一些注意事项和如何设计一个合理的类用来处理所有的http请求以及返回结果,一般都不会提及.因此,自己对此做了些总结,给 ...
- 内存管理 初始化(六)vmalloc_init 及 ioremap
是不是我错了,本想这个函数会如网上所说将进行非连续内存管理的初始化,但是对于2.6.34的ARM架构而言,该函数实际完成的业务非常少. 内存管理的初始化读到此处,我感觉原有的认识存在很大缺陷: (1) ...
- String学习之-深入解析String#intern
引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念.常量池就类似一个JAVA系统级别提供的缓存. 8 ...
- vue获取dom元素注意问题
mounted(){ setTimeout(()=>{ this.contentToggle(); },1000) }, methods:{ contentToggle(){ console.l ...
- smartgit 需要输入序列号解决办法
找到路径: %APPDATA%\syntevo\SmartGit\<main-smartgit-version> 然后删除: settings.xml 再重新打开smartgit 就可以了
- Java实现快速批量移动文件
文件移动是计算机资源管理常用的一个操作,这在操作系统中可以通过文件的剪切与复制或鼠标拖动来实现.但是在Java文件的编程实现中,大多是通过复制文件到目的地,再删除所有文件来实现的.这对于小文件来说看不 ...
- DropDownListFor的种种纠结(禁止转载)
严重禁止转载,好多爬虫软件为了浏览到处抓东西,真缺德 具有键“CorpType”的 ViewData 项属于类型“System.Int64”,但它必须属于类型“IEnumerable<Selec ...