android cmake 第三方,Android Studio CMake依赖第三方库(示例代码)

这里实现一个简单的功能在APP里调用libnative-lib.so里的add。libnative-lib.so去调用libthird.so里的third_add来实现

JniUtil

public classJniUtil {static{

System.loadLibrary("native-lib");

System.loadLibrary("third");

}public static native int add(int x,inty);

}

libnative.cpp

#include #include#include"third.h"

extern "C"{

JNIEXPORT jint JNICALL

Java_com_test_ndkthirdso_JniUtil_add(JNIEnv*env, jclass type, jint x, jint y) {//TODO

returnthird_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 __cplusplusextern "C"{#endif

int third_add(int a, intb);

#ifdef __cplusplus

}#endif

#endif //__H_THIRD

third.cpp

#include #include"third.h"

int third_add(int a, intb){return a+b;

};

生成如下

3c2ea88b1490009c42ba65f12c82e960.png

项目工程结构如下:

fd7fafe83619cb5cbd0e6712af33c6f9.png

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

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‘

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值