Android studio3.6.3NDK环境配置

#标题: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”,开始下载。在这里插入图片描述

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

在这里插入图片描述
(注意:1)AS3.6.3版本可能没有LLDB复选框,可以不选这个是调试用得,不影响搭建环境。怎么弄出来还不清楚,可能最新版本就有了。2)下载下来可能没有ndk-bundle文件,下面选择ndk配置文件时,选择ndk文件夹下面得一串数字得文件加即可)
第二步:配置NDK。选择“FILe”->“Project Stucture”->“SDK Location”,“AndroidNDK Location”选择刚刚下载“ndk-bundle”路径。点击“Apply”按钮,点击“OK”。

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

同时,工程报错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环境配置成功。

在这里插入图片描述

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

1、在工程下新建类

在这里插入图片描述

选择提示信息,在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文件。
在这里插入图片描述
此时,main文件夹下面新增“jni”文件夹包含.h文件。
在这里插入图片描述
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、模拟器编译。成功。
在这里插入图片描述
以上为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)在调用处调用。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Android Studio 3.6.3 是一款针对 Android 应用开发的集成开发环境(IDE)。它提供了丰富的功能和工具,方便开发人员构建、测试和调试 Android 应用程序。 首先,Android Studio 3.6.3 提供了一个直观的用户界面,可以轻松地创建新项目。开发人员可以选择适用于各种设备和 Android 版本的项目模板,并配置项目参数。此外,它还提供了一套强大的编辑器工具,如代码自动补全、语法高亮、快速修复等,可帮助开发人员更高效地编写代码。 其次,Android Studio 3.6.3 集成了丰富的调试工具。开发人员可以使用内置的调试器来调试应用程序,并查看变量的值、执行栈追踪等。此外,它还支持模拟器和真机上的实时调试,以及在应用程序崩溃时生成详细的崩溃报告。 此外,Android Studio 3.6.3 还具有良好的兼容性。它支持最新的 Android 版本,并且与 JDK、Gradle 等关键工具的版本保持同步。这使得开发人员可以利用最新的功能和改进,并确保应用程序能够在各种不同的设备上平稳运行。 最后,Android Studio 3.6.3 还提供了强大的构建和测试工具。开发人员可以使用 Gradle 构建工具进行项目构建和依赖管理。此外,它还支持各种类型的测试,包括单元测试、集成测试和 UI 测试,以确保代码的质量和稳定性。 总而言之,Android Studio 3.6.3 是一款功能强大的 Android 应用开发工具。它提供了丰富的功能和工具,帮助开发人员更高效地构建、测试和调试 Android 应用程序。无论是初学者还是有经验的开发人员,都可以从中受益并创造出出色的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值