文章部分内容转自:https://blog.csdn.net/u013564742/article/details/86512271
我的AS版本:3.4.1
简介:
涉及到一些算法或者底层驱动的时候,往往需要使用jni来开发。现在官方推荐使用CMake工具来开发jni。
使用CMake开发Jni其实挺简单的,要求不高的话只需要简单配置一下就可以使用了。
配置环境
使用CMake进行Jni开发需要使用CMake插件、LLDB插件、NDK插件,这些都可以通过Android Studio很快地安装。
打开SDK Manager,找到Android SDK->SDK Tool选项,安装CMake、LLDB、NDK插件。
创建支持C++代码的工程
勾选Include C++ support选项,勾选后Android Studio可以更好地支持及帮助我们检查jni代码,之后一路next即可。
后面有个地方,我选的是C++11
创建完成后会发现Android Studio会自动为我们生成一个Jni调用示例。我们把工各切换至project视图,来看一看目录结构。
看目录可以知道,相比普通工程,AndroidStudio主动为我们生成了一个cpp的目录,该目录主要存放调用的c++头文件,源代码及jni代码,还有一个CMakeLists.txt的文件,cpp目录下面包含一个native-lib.cpp文件(稍后会讲解),这里需要关注的主要文件有3个,native-llib.cpp、MainActivity和CMakeList.txt。下面重点讲解一下这几个文件的作用。
native-lib.cpp文件
该文件是之前勾选支持c++后Android Studio主动为我们生成的一个示例调用文件,里面包含了jni代码。
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_jnicmake_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
MainActivity.java文件
package com.example.jnicmake;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
这里的代码主要注意2点,一是
static {
System.loadLibrary("native-lib");
}
这里主要加载native-lib库,这里加载库的名称需要跟CMakeLists.txt里面配置的一样。
二是public native String stringFromJNI(),
方法,主要提供java本地jni接口,java层代码就是调用这个接口来通过jni调用C++函数的。
CMakeLists.txt文件
这个文件的作用是通过配置将C++代码编译到共享对象库中,具体功能项直接通过注释来说明吧。
# 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构造本地库所需要的最低版本
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.
# 创建并命名库,可以将库的类型设置为静态
# 或共享,并提供该库源代码的相对路径。
# 你可以定义多个library库,并使用CMake来构建。
# Gradle会自动打包库集成到apk中。
add_library( # Sets the name of the library.
#库的名称
native-lib
#库的分享类型
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# 库所在位置的相对路径
native-lib.cpp )
# 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.
# NDK提供了一套实用的原生API和库,可以使用find_library搜索NDK中存在的库,只需要加入所需要使用库的名称即可,如下面的日志库log-lib。
# 更多的库可参考https://developer.android.com/ndk/guides/stable_apis
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.
# 指定CMake连接到目标库中,可以链接多个库,如上面的native-lib库以及NDK中自带的log-lib库
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
到目前为止,文章说现在就可以运行起来了。但是这里显然还不行
这里应该是有资源没有下载下来,,并且还报了如下错误,查到网上方法是说google的源被屏掉了
* What went wrong:
A problem occurred configuring project ':app'.
修改E:\androidTest\JniCMake\build.gradle文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
// maven库
def cn = "http://maven.aliyun.com/nexus/content/groups/public/"
def abroad = "http://central.maven.org/maven2/"
// 先从url中下载jar若没有找到,则在artifactUrls中寻找
maven {
url cn
artifactUrls abroad
}
maven { url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
google()
// jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// maven库
def cn = "http://maven.aliyun.com/nexus/content/groups/public/"
def abroad = "http://central.maven.org/maven2/"
// 先从url中下载jar若没有找到,则在artifactUrls中寻找
maven {
url cn
artifactUrls abroad
}
maven { url "http://maven.aliyun.com/nexus/content/repositories/jcenter"}
google()
// jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
但是还是报这个错,我回查sdk目录下的NDK,发现有三个版本:
SDK的根目录下还有一个:ndk-bundle目录,这个应该是22.0.7026061的版本
我试着把E:\androidTest\JniCMake\local.properties文件内容
## 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.
#Wed Feb 24 14:33:08 CST 2021
ndk.dir=D\:\\androidSDK\\ndk-bundle
sdk.dir=D\:\\androidSDK
修改成20 和19版本位置
## 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.
#Wed Feb 24 14:33:08 CST 2021
#ndk.dir=D\:\\androidSDK\\ndk-bundle
#sdk.dir=D\:\\androidSDK
ndk.dir=D\:\\androidSDK\\ndk\\20.1.5948944
sdk.dir=D\:\\androidSDK
#ndk.dir=D\:\\androidSDK\\ndk\\19.2.5345600
#sdk.dir=D\:\\androidSDK
都能够编译成功,并运行起来。但22版本的不可以 。本项目的sdk=28,真机用的是红米8A
后来又仔细查了一下报错信息,说是没有platforms目录,又对比了一下几个文件夹里的内容,发现22的和系统用的确实没有。
又把platforms目录及内容拷到D:\androidSDK\ndk-bundle目录下
再次运行,可以了。
后来还有一个小的提示性错误:The following project options are deprecated and have been removed:
应该是下项目选项已弃用并已移除,我的项目里还有。我查了一下,E:\androidTest\JniCMake\gradle.properties文件
把以下内容去掉就可以了。
# 兼容老的ndk
#android.useDeprecatedNdk=true