Android Studio CMake依赖第三方库
这里实现一个简单的功能在APP里调用libnative-lib.so里的add。libnative-lib.so去调用libthird.so里的third_add来实现
JniUtil
public class JniUtil {
static {
System.loadLibrary("native-lib");
System.loadLibrary("third");
}
public static native int add(int x,int y);
}
libnative.cpp
#include <jni.h>
#include <string>
#include "third.h" extern "C"{
JNIEXPORT jint JNICALL
Java_com_test_ndkthirdso_JniUtil_add(JNIEnv *env, jclass type, jint x, jint y) { // TODO
return third_add(x,y);
} JNIEXPORT jstring JNICALL
Java_com_test_ndkthirdso_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
} }
这里编译好一个自己写一个libthird.so库实现一个加法功能
third.h
#ifndef __H_THIRD
#define __H_THIRD #ifdef __cplusplus
extern "C" {
#endif int third_add(int a, int b); #ifdef __cplusplus
}
#endif #endif // __H_THIRD
third.cpp
#include <jni.h>
#include "third.h" int third_add(int a, int b){
return a+b;
};
生成如下

项目工程结构如下:

CMakeLists.txt
cmake_minimum_required(VERSION 3.4.)
add_library(native-lib
SHARED
src/main/cpp/native-lib.cpp ) find_library(log-lib
log )
#动态方式加载 third是libxxxx.so的xxxx部分
add_library(third SHARED IMPORTED) SET(third_path ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libthird.so) #设置要连接的so的相对路径,${ANDROID_ABI}表示so文件的ABI类型的路径,这一步引入了动态加入编译的so
set_target_properties(third PROPERTIES IMPORTED_LOCATION ${third_path}) MESSAGE(STATUS “src third so path= ${third_path}”) #配置加载native依赖
include_directories( ${ProjectRoot}/app/src/main/cpp/external/include ) target_link_libraries(native-lib third ${log-lib} )
gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.test.ndkthirdso"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi', 'armeabi-v7a'
}
}
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:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Android Studio CMake依赖第三方库的更多相关文章
- Android Studio中导入第三方库
之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...
- Android Studio [ImageView/使用第三方库加载图片]
ImageViewActivity.class package com.xdw.a122; import android.support.v7.app.AppCompatActivity; impor ...
- android implementation 依赖第三方库
依赖第三方库
- 【Android Studio探索之路系列】之六:Android Studio加入依赖
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...
- [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so
0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...
- [置顶] android利用jni调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so
0:前言 1:本文主要作为丙方android公司的身份来写 2:作者有不对的地方,请指出,谢谢 [第一篇:android利用jni调用第三方库——编写库libhello.so] [第二篇:androi ...
- android调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so (转载)
转自:http://blog.csdn.net/jiuyueguang/article/details/9449737 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言 1:本文主要作为 ...
- 运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法
运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法 问题一: SyntaxError: missing ) after argument list in .....\vie ...
- Android Studio中依赖第三库导致support版本冲突解决方案
1.今天在Android Studio的app/gradle文件中依赖文件选择器的第三方库:“com.leon:lfilepickerlibrary:1.8.0” 时,github地址:https:/ ...
随机推荐
- 自动化打包资源混淆集成python实践----打包
1.自动化打包方案 1)友盟多渠道多渠道打包 2)gradle productFlavors系统的条件编译 3)美团打包 4)APK文件注释写入渠道号 2.各打包方案简介 1)友盟多渠道多渠道打包(w ...
- fiddler----APP弱网测试
转自:http://www.51testing.com/html/01/n-3727001.html APP弱网模拟测试 移动端测试区别于PC端测试的一点就是网络的多变性:不同的网络环境和网络制式的差 ...
- 【java】小技巧和注意事项
1.字符串反向比较 “abc”.equals(sting) 2.文档注释 /** *注释内容 */ 3.
- 数组最后一个元素的 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁
数组最后一个元素$arr = array( 1 , 2 , 3 , 4 );foreach ( $arr as & $value ) { $value = $value ...
- Memcached Cache
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Memcached ...
- 用递归算法返回该元素id下面的所有子集id
private List<int> listAreaId = new List<int>(); /// <summary> /// 递归获取本区域下面的所有子集 / ...
- 「BZOJ 3270」博物馆「高斯消元」
应该算高斯消元经典题了吧. 题意:一个无向连通图,有两个人分别在\(s,t\),若一个人在\(u\),每一分钟有\(p[u]\)的概率不动,否则随机前往一个相邻的结点,求在每个点相遇的概率 题解: 首 ...
- How to extract pcd from a rosbag? 如何从rosbag中提取pcd
4.1 bag_to_pcd Reads a bag file, saving all ROS point cloud messages on a specified topic as PCD fil ...
- go语言实战教程之管理员查询功能、退出功能
前面第10节课内容中已经学习开发完成了管理员登陆功能.本节课我们将继续学习开发完成管理员信息查询功能.管理员退出功能 管理员信息查询功能 请求及路由映射 管理员信息查询接口 接口名称:获取管理员信息. ...
- 洛谷 P3586 [POI2015]LOG
P3586 [POI2015]LOG 题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它 ...