前言
学过的知识点太容易忘记了,做个记录后续方便查询
正题
主要有三个内容
- Cmake链接三方so库
- 文件拆分和合并
- NDK的线程
Cmake链接三方so库
在平常开发项目的时候常见的是引入三方的so库,然后java调用已经给好的api。如果需要在C++或者C中调用呢,Android的NDK开发,提供了CMake,将三方的so库,动态链接到我们的项目。
这边以一个简单的例子在C++代码调用so的api
步骤:
- 将三方so库copy到项目的libs(如果copy到jnilibs下是不需要配置)
如果将so包复制到module下的libs,需要在build.gradle配置
android{
.............省略代码............
sourceSets {
main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
}
}
- 在CMakeList.txt 配置三方so包,并且链接到当前项目
cmake_minimum_required(VERSION 3.4.1)
#设置so库路径
set(my_lib_path ${CMAKE_SOURCE_DIR}/../../../libs)
#将第三方库作为动态库引用
add_library(myTest
SHARED
IMPORTED)
#指定第三方库的绝对路径
set_target_properties(myTest
PROPERTIES IMPORTED_LOCATION
${my_lib_path}/${ANDROID_ABI}/libtest-lib.so)
#设置的本项目生成的so的引用名
add_library( # Sets the name of the library.
asTest
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
#引用的是系统的库
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
#最后都需要链接到项目中
target_link_libraries( # Specifies the target library.
asTest
myTest
# Links the target library to the log library
# included in the NDK.
${log-lib} )
-
将三方的api的头文件引入到cpp的目录下
image.png
可以看到相关的api
#ifndef SOUSE_MY_TEST_H
#define SOUSE_MY_TEST_H
//申明外部 函数 外部属性
extern int sum(int a, int b);
#endif //SOUSE_MY_TEST_H
- 在自己的项目中引用
native-lib.cpp
#include "my-test.h" //引入头文件
extern "C"
JNIEXPORT jint JNICALL
Java_com_ndk_so_use_MainActivity_doAdd(JNIEnv *env, jobject thiz, jint a, jint b) {
return sum(a,b);
}
- java代码调用c++的代码(JNI)
//native方法的定义
public native int doAdd(int a, int b);
//调用
int result = doAdd(99, 66);
tv.setText("result = " + result);
文件拆分和合并
文件拆分一般是因为文件比较大需要上传,所以通过拆分成多个文件再进行上传。
关于C++的文件操作的介绍https://www.jianshu.com/p/422b4df24dad之前有整理过。
这边代码后续的native方法是采用动态注册,关于native方法的动态注册可以查看https://www.jianshu.com/p/3aeabe2b5744
注
:手机读写,6.0以上的机器,需要动态申请权限
撸码
- native方法的定义:
public class FileUtil {
public static native int diffFile(String srcPath, String partPath, int count);
public static native int mergeFile(String mergePath, String partPath, int count);
}
- 文件拆分
C++的实现代码:
/**
* 获取文件大小
*/
long get_file_size(const char* path) {
FILE *fp = fopen(path, "rb"); //打开一个文件, 文件必须存在,只运行读
if(fp == NULL) {
LOG_I("open file failed...");
return 0;
}
fseek(fp, 0, SEEK_END);
long ret = ftell(fp);
fclose(fp);
return ret;
}
/**
* 文件拆分
*/
JNIEXPORT jint JNICALL native_diff(JNIEnv *env, jclass jclz, jstring src_path, jstring part_path, jint count)
{
LOG_I("start begin diff file.");
//jstring 转 char*
// 需要拆分的文件路径
const char* srcPath = env->GetStringUTFChars(src_path, NULL);
// 拆分后的文件路径格式
const char* partPath = env->GetStringUTFChars(part_path, NULL);
char *partPaths[count];
int i;
for (i = 0; i < count; ++i) {
//每个文件名申请地址
LOG_I("char = %d char * = %d", sizeof(char), sizeof(char *));
partPaths[i] = (char*)malloc(sizeof(char) * 200);
// 需要分割的文件 Vibrato.mp4
// 每个子文件名称 Vibrato_n.mp4
sprintf(partPaths[i], partPath, i);
LOG_I("patch path : %s", partPaths[i]);
}
int fileSize = get_file_size(srcPath);
FILE *fpr = fopen(srcPath, "rb");
if(fpr == NULL) {
LOG_I("open file failed...");
return 0;
}
//判断文件大小能够被 count 整除
if(fileSize % count == 0) {
//能整除就平分,获取每一份的大小
int part = fileSize / count;
for (int i = 0; i < count; ++i) {
FILE *fpw = fopen(partPaths[i], "wb"); //文件已经存在 就删除,只运行写
for (int j = 0; j < part; ++j) {
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
} else{
//不能整除就先分 count -1, 剩下的作为单独一份
int part = fileSize / (count -1);
for (int i = 0; i < count - 1; ++i) {
FILE *fpw = fopen(partPaths[i], "wb");
for (int j = 0; j < part; ++j) {
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
FILE *fpw = fopen(partPaths[count - 1], "wb");
for (int j = 0; j < fileSize % (count - 1); ++j) {
fputc(fgetc(fpr), fpw);
}
fclose(fpw);
}
fclose(fpr);
free(partPaths);
env->ReleaseStringUTFChars(src_path, srcPath);
env->ReleaseStringUTFChars(part_path, partPath);
LOG_I("diff file finish.");
return 1;
}
java的调用代码:
public void onDiff(View view) {
(new Thread(){
@Override
public void run() {
String srcPath = SD_CARD_PATH + File.separator + "ZERO.rmvb";
String partPath = SD_CARD_PATH + File.separator + "ZERO_%d.rmvb";
final int result = FileUtil.diffFile(srcPath, partPath, 4);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(result == 1) {
showToast("拆分成功");
}else {
showToast("拆分失败");
}
}
});
}
}).start();
}
- 文件合并
C++的实现代码:
/**
* 文件合并
*/
JNIEXPORT jint JNICALL native_merge(JNIEnv *env, jclass jclz, jstring merge_path, jstring part_path, jint count)
{
LOG_I("start begin merge file.");
//获取合并后生成的文件路径
const char * mergePath = env->GetStringUTFChars(merge_path, NULL);
//获取拆分文件的格式
const char * partPath = env->GetStringUTFChars(part_path, NULL);
char *partPaths[count];
for (int i = 0; i < count; ++i) {
partPaths[i] = (char*) malloc(sizeof(char) * 200);
sprintf(partPaths[i], partPath, i);
LOG_I("partPaths[%d] = %s", i, partPaths[i]);
}
FILE *fpw = fopen(mergePath, "wb");
if(fpw == NULL) {
return 0;
}
for (int i = 0; i < count; ++i) {
FILE *fpr = fopen(partPaths[i], "rb");
int fileSize = get_file_size(partPaths[i]);
for (int j = 0; j < fileSize; ++j) {
fputc(fgetc(fpr), fpw);
}
fclose(fpr);
}
fclose(fpw);
free(partPaths);
env->ReleaseStringUTFChars(merge_path, mergePath);
env->ReleaseStringUTFChars(part_path, partPath);
LOG_I("file merge finish");
return 1;
}
java的调用代码:
public void onMerge(View view) {
(new Thread(){
@Override
public void run() {
String srcPath = SD_CARD_PATH + File.separator + "ZERO_merge.rmvb";
String partPath = SD_CARD_PATH + File.separator + "ZERO_%d.rmvb";
final int result = FileUtil.mergeFile(srcPath, partPath, 4);
runOnUiThread(new Runnable() {
@Override
public void run() {
if(result == 1) {
showToast("合并成功");
}else {
showToast("合并失败");
}
}
});
}
}).start();
}
线程
在C++中开线程调度java的方法
撸码
java代码native方法的定义:
public class ThreadUtil {
public native void createThread();
public native void setJniEnv();
public native void releaseThread();
public void jniThreadCallBack() {
}
public static void formJni( int i) {
Log.d("so_use","form jni : " +i);
};
public static void formJNIAgain(int i) {
Log.v("so_use","form_JNI_Again : "+i);
}
}
C++代码:这边需要使用到新的api,引入pthread的头文件
#include <pthread.h>
JavaVM* g_jvm = NULL;
jobject g_obj = NULL;
//一个进程只有一个jvm
//一个线程一个JniEnv
void* thread_fun(void * args) {
JNIEnv *env;
jclass clz;
jmethodID mid, mid1;
if(g_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
LOG_I("%s AttachCurrentThread error failed ",__FUNCTION__);
return NULL;
}
clz = env->GetObjectClass(g_obj);
if(clz == NULL) {
LOG_I("load class failed.");
goto error;
}
LOG_I("call back begin");
mid = env->GetStaticMethodID(clz, "formJni", "(I)V");
if (mid == NULL) {
LOG_I("GetStaticMethodID error....");
goto error;
}
env-> CallStaticVoidMethod(clz, mid, args);
mid1 = env->GetStaticMethodID(clz, "formJNIAgain", "(I)V");
if (mid1 == NULL) {
LOG_I("GetStaticMethodID error....");
goto error;
}
env-> CallStaticVoidMethod(clz, mid1, args);
error:
if (g_jvm -> DetachCurrentThread() != JNI_OK) {
LOG_I("%s DetachCurrentThread error failed ",__FUNCTION__);
}
pthread_exit(0);
}
JNIEXPORT void JNICALL native_createThread(JNIEnv *env, jobject job)
{
LOG_I("createThread begin");
int i;
pthread_t pt[5];
for (int i = 0; i < 5; ++i) {
pthread_create(&pt[i], NULL, &thread_fun, (void*)i);
}
}
JNIEXPORT void JNICALL native_setJniEnv(JNIEnv *env, jobject job)
{
LOG_I("native_setJniEnv");
if(g_jvm != NULL) {
g_jvm = NULL;
}
//保存JVM
env -> GetJavaVM(&g_jvm);
//保持actvity对象
g_obj = env -> NewGlobalRef(job);
}
JNIEXPORT void JNICALL native_releaseThread(JNIEnv *env, jobject job)
{
LOG_I("native_releaseThread");
if(g_jvm != NULL) {
g_jvm = NULL;
}
env->DeleteGlobalRef(g_obj);
}
java代码中调用
private ThreadUtil threadUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建对象,设置JNI中的全局引用
threadUtil = new ThreadUtil();
threadUtil.setJniEnv();
}
public void onCreateThread(View view) {
threadUtil.createThread();
}
public void onReleaseThread(View view) {
threadUtil.releaseThread();
}
以上就是全部的内容
项目的源码:https://github.com/jasonkevin88/SoUse
网友评论