Android Jni_开发 CMake工具开发

9 篇文章 1 订阅
3 篇文章 0 订阅

1.Android NDK环境配置:

project---- local.properties下面可以直接添加ndk地址:

## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Mon Mar 18 13:40:13 CST 2019
ndk.dir=E\:\\SDK\\ndk-bundle
sdk.dir=E\:\\SDK

也可以选择:
File------Project Structure------SDK Location
在这里插入图片描述添加CMake工具 AS下操作:

2.开发步骤:

2.1:创建本地的Javanative方法方便c代码实现:

package test.com.zh.testcmakedemo;
   类名:MainActivity
 public native String stringFromJNI();

2.2:在app/src/main 下创建一个文件例如 cpp;里面存放要加载执行的C代码:

C代码:

#include <jni.h>
#include<stdio.h>
#include <stdlib.h>

JNIEXPORT jstring

//test.com.zh.testcmakedemo
JNICALL

Java_test_com_zh_testcmakedemo_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject obj) {
    return (*env)->NewStringUTF(env,"Hello from C");
}

2.3用CMake工具执行生成.so文件,需要先创建一个CMakeLists.txt文件;模板如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
// 将要生成的.so文件的名字
             Hello

             # Sets the library as a shared library.
             SHARED
// c代码的本路径
             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.c )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       Hello

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

3.用CMake执行CMakeLists.txt,方法两种:
1: app的gradle添加:


  externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }

添加后的样子:

android {
    compileSdkVersion 28
    defaultConfig {
      ...
    }
    buildTypes {
    }
    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }

}

2.可视化执行的方法:
Project 下 点击app右键--------->link C++Project With Gradle 1:
在这里插入图片描述 2.选择CMake模式指定上面创建好的CMakeLists.txt文件:
在这里插入图片描述指定好后:点击OK:
在这里插入图片描述4.加载库文件执行本地方法:

 static {
        System.loadLibrary("Hello");
    }
    
Toast.makeText(MainActivity.this,stringFromJNI(),Toast.LENGTH_LONG).show();


最后看下生成的目录结构:
这个是多生成文件:
在这里插入图片描述整个目录路径应该是这样的:

在这里插入图片描述
CMake工具方式下libs不要copy.so文件;app gradle下无需指定JNI的libs。
打包生成的apk文件自动添加.so文件;如下图

在这里插入图片描述

生成的apk里面的.so可以看下:

arm64-v8a:机型为 MI Note3在这里插入图片描述
在这里插入图片描述

好了以上就是CMake工具下的JNI开发,原理和ndk-build下的一样,规范都是JNI.h要求的。不同点就是执行.c生成二进制代码的方式不同而已。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值