这里实现一个简单的功能在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依赖第三方库的更多相关文章

  1. Android Studio中导入第三方库

    之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...

  2. Android Studio [ImageView/使用第三方库加载图片]

    ImageViewActivity.class package com.xdw.a122; import android.support.v7.app.AppCompatActivity; impor ...

  3. android implementation 依赖第三方库

    依赖第三方库

  4. 【Android Studio探索之路系列】之六:Android Studio加入依赖

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...

  5. [置顶] android利用jni调用第三方库——第三篇——编写库android程序整合第三方库libhello.so到自己的库libhelloword.so

    0:前言: 在第二篇中,我们主要介绍了丙方android公司利用乙方C++公司给的动态库,直接调用库中的方法,但是这样方式受限于: 乙方C++公司开发的动态库是否符合jni的规范,如果不规范,则不能直 ...

  6. [置顶] android利用jni调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so

    0:前言 1:本文主要作为丙方android公司的身份来写 2:作者有不对的地方,请指出,谢谢 [第一篇:android利用jni调用第三方库——编写库libhello.so] [第二篇:androi ...

  7. android调用第三方库——第二篇——编写库android程序直接调用第三方库libhello.so (转载)

    转自:http://blog.csdn.net/jiuyueguang/article/details/9449737 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言 1:本文主要作为 ...

  8. 运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法

    运用NodeJs环境并依赖第三方库,框架等实现网站前后端分离报错问题及处理方法 问题一: SyntaxError: missing ) after argument list in .....\vie ...

  9. Android Studio中依赖第三库导致support版本冲突解决方案

    1.今天在Android Studio的app/gradle文件中依赖文件选择器的第三方库:“com.leon:lfilepickerlibrary:1.8.0” 时,github地址:https:/ ...

随机推荐

  1. SDUT 1488 数据结构实验:连通分量个数

    数据结构实验:连通分量个数 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description  在无向图中,如 ...

  2. scala中枚举

    scala没有从语法的角度来支持枚举,而是通过定义了一个接口Enumeration来支持的 object ExecutorState extends Enumeration{ type Executo ...

  3. Oracle数据库之多表查询一

    上一篇给大家介绍了数据的单表查询,相信大家对于单表的查询应该都有了一些了解.单表查询在数据库中的使用会有一些,但并不是很多,但是作为初学者,我们需要学习单表查询的思路.今天呢,我们为大家介绍一下数据库 ...

  4. Delphi和C#数据类型对应表

    Delphi DataType C# datatype ansistring string boolean bool byte byte char char comp double currency ...

  5. Openwrt单独编译某一个模块而不是整个固件

    make package/rt2860v2/compile 就是在make menuconfig那个目录下执行此命令就会编译rt2860v2这个模块

  6. Notification通知代码简洁使用

    1.自定义发送 Notification 的使用 1.1 通知(消息)的创建 ---------------详细介绍篇 // 不带消息内容 NSNotification *notification1 ...

  7. 51nod 1350 斐波那契表示(递推+找规律)

    传送门 题意 分析 我们发现该数列遵循下列规律: 1 1,2 1,2,2 1,2,2,2,3 1,2,2,2,3,2,3,3 我们令A[i]表示f[i]开始长为f[i-1]的i的最短表示和 那么得到A ...

  8. codevs1068(dp)

    题目链接: http://codevs.cn/problem/1068/ 题意: 中文题诶~ 思路: dp 用 dp[i][j][k][l] 表示取 i 个 1, j 个 2, k 个 3, l 个 ...

  9. 本地化ASP.NET core模型绑定错误消息

    默认错误消息: MissingBindRequiredValueAccessor A value for the '{0}' property was not provided. MissingKey ...

  10. EXPEDI - Expedition 优先队列

    题目描述 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rathe ...