由于需调用底层代码申请系统内存就开发了调用底层的so 创建对应的java调取接口jni
ndk开发
申请内存主要两个
//com.aa.factorylibrary.jni.MemOpJni public class MemOpJni { static { System.loadLibrary("memory_malloc"); } public static native int malloc(int size); public static native void free(); }
生成对应的.h文件
…mainjava> javah -jni com.aa.factorylibrary.jni.MemOpJni//memory_malloc.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_aa_factorylibrary_jni_MemOpJni */ #ifndef _Included_com_aa_factorylibrary_jni_MemOpJni #define _Included_com_aa_factorylibrary_jni_MemOpJni #ifdef __cplusplus extern "C" { #endif /* * Class: com_aa_factorylibrary_jni_MemOpJni * Method: malloc * Signature: (I)I */ JNIEXPORT jint JNICALL Java_com_aa_factorylibrary_jni_MemOpJni_malloc (JNIEnv *, jclass, jint); /* * Class: com_aa_factorylibrary_jni_MemOpJni * Method: free * Signature: ()I */ JNIEXPORT void JNICALL Java_com_aa_factorylibrary_jni_MemOpJni_free (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
// //根据memory_malloc.h生成memory_malloc.cpp // #include "memory_malloc.h" #include "stdlib.h" #include "stdio.h" #include "string.h" #include <unistd.h> #include "android/log.h" #define MEMOP_LOG_TAG "libjnimemory" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, MEMOP_LOG_TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, MEMOP_LOG_TAG, __VA_ARGS__) #define MAX_SIZE 2000 static int *p[MAX_SIZE]; static int count = 0; /* * Class: com_aa_factorylibrary_jni_MemOpJni * Method: malloc * Signature: ()I */ extern "C" JNIEXPORT jint JNICALL Java_com_aa_factorylibrary_jni_MemOpJni_malloc (JNIEnv *, jclass, jint size) { if (count < MAX_SIZE) { p[count] = (int *) malloc(256 * 1024 * size * sizeof(int)); memset(p[count], 2, 256 * 1024 * size * sizeof(int)); count++; LOGI("memfill, malloced memory size is %d", size); return size; } else { LOGE("memfill, count is MAX_SIZE"); } return 0; } /* * Class: com_aa_factorylibrary_jni_MemOpJni * Method: free * Signature: ()I */ extern "C" JNIEXPORT void JNICALL Java_com_aa_factorylibrary_jni_MemOpJni_free (JNIEnv *, jclass) { for (int i = 0; i < MAX_SIZE; i++) { if (NULL != p[i]) { free(p[i]); p[i] = NULL; } } count = 0; LOGI("memfree, free memory"); return; }
由java到c的接口输出完成
cmake_minimum_required(VERSION 3.4.1) aux_source_directory(src/main/cpp/memory_malloc DIR_SRCS) add_library( # Sets the name of the library. memory_malloc # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). #src/main/cpp/memory/memory_malloc.cpp ) ${DIR_SRCS} ) 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. memory_malloc # Links the target library to the log library # included in the NDK. ${log-lib} )
在build.gradle添加CMakeLists.txt路径即可生成的对应so即可(对应build的目录取出)
apply plugin: 'com.android.library' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { minSdkVersion 23 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' externalNativeBuild { cmake { // apply plugin: 'announce' cppFlags "" //生成多个版本的so库 // abiFilters 'armeabi-v7a', 'arm64-v8a' } } // ndk { // abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64" // } } externalNativeBuild { cmake { path "CMakeLists.txt" } } } ...
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算