CMakeLists.txt生成.so ndk开发

ndk开发

由于需调用底层代码申请系统内存就开发了调用底层的so

申请内存主要两个

  • 申请内存
  • 释放内存

创建对应的java调取接口jni

//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文件

  • 切换到java目录
    …\main\java> javah -jni com.aa.factorylibrary.jni.MemOpJni
  • 在main\java目录下取出对应的.h (com_aa_factorylibrary_jni_MemOpJni.h)
  • 重命名.h , 根据.h文件生成对应的c或者cpp
//memory_malloc.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* 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 
#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的接口输出完成

  • 生成.so文件编译
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"
        }
    }

}
...

你可能感兴趣的:(android,app)