【Android】打开需要NDK的项目的一些报错的问题解决

文章简述

在打开一个新的项目的时候,遇到了一些问题,记录一下问题的解决步骤。

问题1

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> NDK not configured. 
  Download it with SDK manager.

在构建失败之后,出现这个报错,告诉我们需要配置NDK

配置方法如下:
【File】=>【Setting】=>【Android SDK】
在这里插入图片描述在这里插入图片描述
然后我们需要在这个文件里面添加
在这里插入图片描述
在这里插入图片描述
然后编译,报错

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Unable to find CMake with version: 3.10.2 within folder: [C:\WINDOWS\system32, C:\WINDOWS, C:\WINDOWS\System32\Wbem, C:\WINDOWS\System32\WindowsPowerShell\v1.0, C:\WINDOWS\System32\OpenSSH, D:\Bandizip, C:\Program Files\Git\cmd, D:\Android\AndroidStudio\AndroidSdk\platform-tools, C:\Users\mx\AppData\Local\Microsoft\WindowsApps]
  .Please make sure the folder with the CMake binary is added to the PATH environment variable.

这代表我们需要一个CMake版本 3.10.2 我们需要下载这个版本的 CMake

下载
还是刚才的位置
【File】->【Setting】->【Android SDK】

在这里插入图片描述
然后我们还在这个【local.properties】文件里面添加地址
在这里插入图片描述
然后碰见问题

CMake Error: Could not find CMAKE_ROOT !!!
CMake has most likely not been installed correctly.
Modules directory not found in
D:/Android/AndroidStudio/AndroidSdk/cmake/3.10.2.4988404/share/cmake-3.10

这个代表我们这个版本的CMake应该下载失败了,我们需要重新下载一个,然后添加地址
在这里插入图片描述
然后编译,报错

CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.

这个错误消息表示在 Android Studio 中使用 CMake 构建时,CMake 无法找到与 “Ninja” 相对应的构建程序。

CMake版本太高了,但是我们的Android Studio版本有点低,这个时候我们可以更换一下CMake版本,使用低版本的CMake。

在这里插入图片描述
然后修改这个文件
在这里插入图片描述
再次编译之后,报错了

CMake Error at CMakeLists.txt:8 (cmake_minimum_required):
CMake 3.10.2 or higher is required.  You are running version 3.6.0-rc2

在CMakeLists.txt文件终的第八行这个版本里面需要的是3.10.2或者更高版本,但是我们的CMake版本是3.6.0-rc2的版本,不符合要求,需要修改

在这里插入图片描述
同时我们也要检查一下【build】文件
在这里插入图片描述
然后再执行编译,最后发现编译通过了
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Android Studio创建NDK项目的步骤如下: 1. 创建新项目:在Android Studio中选择“Start a new Android Studio project”。 2. 添加NDK支持:在“Add an Activity to Mobile”页面中,选择“Native C++”作为Activity类型,然后单击“Next”。 3. 配置C++支持:在“Customize C++ Support”页面中,选择“C++11”作为C++标准库,并选择“None”作为异常支持。单击“Finish”。 4. 配置gradle文件:在项目build.gradle文件中,添加以下代码: ``` externalNativeBuild { cmake { path "CMakeLists.txt" } } ``` 5. 配置CMakeLists.txt文件:在项目的CMakeLists.txt文件中,添加以下代码: ``` cmake_minimum_required(VERSION 3.4.1) 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). src/main/cpp/native-lib.cpp ) # Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included 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 the # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` 6. 编写C++代码:在src/main/cpp目录下创建native-lib.cpp文件,并添加以下代码: ``` #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()); } ``` 7. 在MainActivity.java文件中添加以下代码: ``` static { System.loadLibrary("native-lib"); } public native String stringFromJNI(); ``` 8. 运行应用程序:构建并运行应用程序,您将看到“Hello from C++”消息。 --相关问题--: 1. 如何在Android Studio中使用OpenCV? 2. 如何在NDK中使用CMake? 3.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值