内容简介:使用cmake解决Android中对第三方库的依赖
接着我们新建一个module layer1,这个module用来生成一个so库作为第三方的sdk。创建接口文件Layer.h,在接口我们定义了两个方法
//
// Created by Li Yanshun on 2017/6/6.
//
#ifndef SINGLELIB_LAYER1_H
#define SINGLELIB_LAYER1_H
#include <stddef.h>
#include <android/log.h>
#include <jni.h>
#include <string>
#define LOG_E(...) __android_log_print(ANDROID_LOG_ERROR,"Netclient",__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
std::string (*getResponse)();
std::string (*getRequest)();
} NET_API_FUNCTIONS_TYPE_LAYER1;
extern __attribute__ ((visibility ("default"))) NET_API_FUNCTIONS_TYPE_LAYER1 net_client_layer1;
#ifdef __cplusplus
}
#endif
#endif //SINGLELIB_LAYER1_H
创建文件Layer.cpp来实现这个接口,每个方法都返回一个对应的字符串
//
// Created by Li Yanshun on 2017/6/6.
//
#include "Layer1.h"
std::string get_response() {
LOG_E("layer1 get response");
return "layer1:Response";
}
std::string get_request() {
LOG_E("layer1 get request");
return "layer1:Request";
}
__attribute__ ((visibility ("default"))) NET_API_FUNCTIONS_TYPE_LAYER1 net_client_layer1 = {
get_response,
get_request,
};
最后创建CmakeLists.txt来配置一下工程,通过add_library方法将我们源代码添加进来,生成的so文件命名为layer1,但是系统会自动在名字前面添加lib前缀。
cmake_minimum_required(VERSION 3.4.1)
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp)
set(jnilibs "${CMAKE_SOURCE_DIR}/src/main/jniLibs")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${jnilibs}/${ANDROID_ABI})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -fexceptions -frtti -lstdc++")
add_library(layer1 SHARED src/main/cpp/Layer1.cpp )
find_library(log-lib log )
target_link_libraries(layer1 ${log-lib} )
编译运行后,会在jniLibs目录下的对应ABI目录里生成liblayer1.so
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- CMake 系列(二):第三方依赖管理
- 内联第三方依赖到自己的包中
- 如何查找第三方库(Gradle引用)的依赖?
- demo09 使用 SplitChunksPlugin 分离第三方依赖包以及异步包
- t-io 更新到 3.1.6,缩减第三方依赖
- IDEA对使用了第三方依赖jar包的非Maven项目打jar包
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Persuasive Technology
B.J. Fogg / Morgan Kaufmann / 2002-12 / USD 39.95
Can computers change what you think and do? Can they motivate you to stop smoking, persuade you to buy insurance, or convince you to join the Army? "Yes, they can," says Dr. B.J. Fogg, directo......一起来看看 《Persuasive Technology》 这本书的介绍吧!