#标题:Android Studio 3.6.3 搭建NDK环境,并新建工程

=

背景:本人初次学习Android studio 上搭建NDK环境,按照很多博客方法搭建,最终不能生成.so文件,搞了整整一个下午,终于看到一篇博客,是在Android studio3.4平台搭建,按照该方法尝试成功。因此,写该博客记录一下。参考博文:https://www.cnblogs.com/xujunjia/p/11470622.html

注意:Android studio 得版本不同,NDK环境配置方式不同,因此在学习时一定确认自己得平台版本。该博文得平台为Android studio 3.6.3.

下面为正文,搭建NDK和创建工程得方式和参考博文基本一致,为了巩固一下流程及注意事项,在这里重新梳理一下。

第一步:打开Android studio平台,点击工具栏“SDKManager”按钮,选择“Android SDK”选项->“SDK Tools”标签,选中“LLDB”(用于JNI代码调试)、“CMake”(编译jni程序,生成.so文件)、“NDK”(NDK环境)。点击“OK”,开始下载。

android studio上配置ndk版本 android studio设置ndk版本_cmake

注:下载文件到SDK根目录。我得路径为:E:\install\android-sdk

android studio上配置ndk版本 android studio设置ndk版本_cmake_02


(注意:1)AS3.6.3版本可能没有LLDB复选框,可以不选这个是调试用得,不影响搭建环境。怎么弄出来还不清楚,可能最新版本就有了。2)下载下来可能没有ndk-bundle文件,下面选择ndk配置文件时,选择ndk文件夹下面得一串数字得文件加即可)

第二步:配置NDK。选择“FILe”->“Project Stucture”->“SDK Location”,“AndroidNDK Location”选择刚刚下载“ndk-bundle”路径。点击“Apply”按钮,点击“OK”。

android studio上配置ndk版本 android studio设置ndk版本_android_03

此时查看配置文件“local.properties”中自动生成NDK配置路径。即NDK配置完成。

android studio上配置ndk版本 android studio设置ndk版本_android_04

第三步:新建工程。选择“File”->“New”->"New Project"选择“***Native C++”模板。***依次“next”、“finish”,工程创建成功。新创建得工程中新增cpp文件夹,包含CMakeLists.txt及.cpp文件,说明该项目包含有C++编译环境。

android studio上配置ndk版本 android studio设置ndk版本_c++_05

同时,工程报错Caused by: org.gradle.api.InvalidUserDataException:
NDK not configured. Download it with SDK manager. Preferred NDK version is
‘20.0.5594570’. Log:
E:\project\ndk\test\MyApplication\app.cxx\ndk_locator_record.json

说明该工程无NDK环境,按照上述NDK配置方法,配置NDK环境。配置好环境后,工程目录新增.cxx文件夹,打开“ndk_locator_record.json”文件,可以看到NDK配置路径。说明ndk环境配置成功。

android studio上配置ndk版本 android studio设置ndk版本_c++_06

第四步:编写代码,生成.so文件。

1、在工程下新建类

android studio上配置ndk版本 android studio设置ndk版本_cmake_07

选择提示信息,在native-lib.cpp文件中自动生成C++方法接口,补全实现方法。

#include <jni.h>

#include <string>



extern "C" JNIEXPORT jstring JNICALL

Java_com_example_myapplication_MainActivity_stringFromJNI(

JNIEnv* env,

jobject /* this */) {

std::string hello = "Hello from C++";

return env->NewStringUTF(hello.c_str());

}

extern "C"

JNIEXPORT jstring JNICALL

Java_com_example_myapplication_myjni_getHelloWord(JNIEnv *env, jobject thiz) {

return env -> NewStringUTF("Hello every,this is my first jni
project");

}

2、生成.h文件 (1)编译工程,在build路径下生成myjni类 …\build\intermediates\javac\debug\classes\com\example\myapplication\myjni.class

在Android studio控制台Terminal 定位到***main文件夹(定位到哪里生成得jni文件就在哪个路径)***下输入命令

javah -d jni -classpath classes路径 包名.类名,生成jni文件夹,生成.h文件。

android studio上配置ndk版本 android studio设置ndk版本_android_08


此时,main文件夹下面新增“jni”文件夹包含.h文件。

android studio上配置ndk版本 android studio设置ndk版本_android_09


3、.cpp文件夹添加.h文件得引用

#include <jni.h>
#include <string>
#include "../jni/com_example_myapplication_myjni.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_myapplication_myjni_getHelloWord(JNIEnv *env, jobject thiz) {
    return env -> NewStringUTF("Hello every,this is my first jni project");
}

4、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文件名)
             firstjni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s)(生成.so对应得.cpp文件).
             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.

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(生成.so文件得文件名).
                       firstjni

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

5、新建Android调用.so文件。

package com.example.myapplication;

import androidx.appcompat.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.


    @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(new myjni().getHelloWord());
    }


}

6、模拟器编译。成功。

android studio上配置ndk版本 android studio设置ndk版本_ndk_10


以上为Androidstudio3.6.3平台搭建NDK及创建native工程得全部内容。第一次写博文内容可能有点乱,请包含。下一边篇介绍在原有Android工程得基础上添加NDK 环境,编译nativity程序。

git地址:https://github.com/wildlily1989/androidndk

7、新建一个工程,调用.so库方法
1)再app build.gradle添加

android{
  sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    }

2)新建类,该类与native具有相同得包名类名(重要)。
3)在调用处调用。