这里实现一个简单的功能在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. vim尝试

    http://3502990.blog.51cto.com/3492990/985750

  2. DjVu转PDF

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2009.09.22更新:2012.06.11针对PdfToy的新进展,更新了相关内容. 1 引言2 理论3 实现    3. ...

  3. [转]Passing Managed Structures With Strings To Unmanaged Code Part 1

    1. Introduction. 1.1 Managed structures that contain strings are a common sight. The trouble is that ...

  4. tomcat - 认识

    tomcat - web应用服务器 环境:ubuntu测试 @shell命令(cd到tomcat目录下) 启动: ./bin  startup.sh 关闭:./bin  shutdown.sh @部署 ...

  5. Beautiful Sequence

    Beautiful Sequence 给定一些数(可能相同),将它们随机打乱后构成凹函数,求概率 .N<=60 . 首先,这种题求概率事实上就是求方案.所以现在要求的是用这些数构成凹函数的方案数 ...

  6. P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出格式: 输出一 ...

  7. LB 负载均衡的层次结构(转)

    http://blog.csdn.net/mindfloating/article/details/51020767 作为后端应用的开发者,我们经常开发.调试.测试完我们的应用并发布到生产环境,用户就 ...

  8. Windows Server 2016 IIS10安装URLRewrite 2.0组件方法

    1,打开Regedit> HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ InetStp2,编辑“MajorVersion”并以十进制设置数值数据值为93 ...

  9. linux 内核的 switch_to原理

    switch_to:这是一个宏,有三个参数prev,next,last 局部变量prev,next:指向进程描述符的内存地址 首先明确的是:last和prev是同一个,用last只是为了理解方便,完全 ...

  10. js 联动下拉菜单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...