C/C++ 与 Android Studio 2.2 版 [英] C/C++ with Android Studio version 2.2

查看:21
本文介绍了C/C++ 与 Android Studio 2.2 版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 2.2 以及他们添加的新 C++ 支持;我现在可以在 android studio 中编写和编译,还是需要单独编译和导入我的库

解决方案

简短回答:是的,你可以.

这是您可以执行的操作
1) 转到首选项 ==> Android SDK ==> SDK 工具 ==> CMake
2) 选择它并单击应用和确定

现在,您需要将 CMakeLists.txt 文件添加到您的项目中.
路径:my_project/app/CMakeLists.txt

文件应该是这样的:

# https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script# CMake 的最低版本cmake_minimum_required(版本 3.4.1)# 添加 CEC 库# add_library 结构:add_library(lib_name lib_type_STATIC_or_SHARED source_file_path)add_library(my_lib_name 共享 src/main/jni/my_cpp_file.cpp)# include_directories 是为您提供本地 lib 代码的路径# include_directories 结构:include_directories(native_lib_folder_path)包含目录(src/main/jni/)# 添加Android日志库# find_library 用于查找 NDK API 库(内置 NDK 库)# find_library 结构: find_library(name_you_want_to_call_the_lib lib_name_in_ndk_api)find_library(日志库日志)# 将日志库链接到我们的原生库# 找到库后,必须将该库与本地库链接# target_link_libraries 结构:target_link_libraries(you_native_lib lib_found_using_find_library)target_link_libraries(my_lib_name ${log-lib})

最后一步:将以下内容添加到您的 build.gradle:

externalNativeBuild {制作{路径'CMakeLists.txt'}}

您现在应该可以构建它.

With Android Studio 2.2, and the new C++ support they added; can I now write and compile inside android studio, or do I need to compile and import my libraries separately

解决方案

Short answer: Yes, you can.

Here is what you can do 1
1) In Android Studio, right click on your module ==> New ==> Package
2) name the package (folder) cpp (or you can name it jni)
3) you will see the cpp directory on the left.
4) You can create .cpp, .h and other files within that folder.

Nowm you have to tell gradle how to build that.
You need install CMake. 2
1) Go to Preferences ==> Android SDK ==> SDK Tools ==> CMake
2) Select that and click Apply and Ok

Now, you need to add a CMakeLists.txt file to your project.
Path: my_project/app/CMakeLists.txt

This is what the file should look like:

# https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script


# Minimum version of CMake
cmake_minimum_required(VERSION 3.4.1)


# adding CEC library
# add_library structure:    add_library(lib_name  lib_type_STATIC_or_SHARED  source_file_path)
add_library(my_lib_name SHARED src/main/jni/my_cpp_file.cpp)


# include_directories is to provide the path to you native lib code
# include_directories structure:    include_directories(native_lib_folder_path)
include_directories(src/main/jni/)


# adding Android log library
# find_library is used to find NDK API libraries (built in NDK libs)
# find_library structure:   find_library(name_you_want_to_call_the_lib  lib_name_in_ndk_api)
find_library(log-lib log)


# linking log lib to our native lib
# once you find the library, you have to link that library with your native library
# target_link_libraries structure:  target_link_libraries(you_native_lib  lib_found_using_find_library)
target_link_libraries(my_lib_name ${log-lib})

And final step: add the following to your build.gradle:

externalNativeBuild {
    cmake {
        path 'CMakeLists.txt'
    }
}

You should be able to build it now.

这篇关于C/C++ 与 Android Studio 2.2 版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆