[英]C/C++ with Android Studio version 2.2
With Android Studio 2.2, and the new C++ support they added; 随着Android Studio 2.2的加入,他们添加了新的C ++支持; can I now write and compile inside android studio, or do I need to compile and import my libraries separately
我现在可以在android studio中编写和编译,还是需要分别编译和导入我的库?
Short answer: Yes, you can. 简短的回答:是的,可以。
Here is what you can do 1 这是你可以做的1
1) In Android Studio, right click on your module ==> New ==> Package 1)在Android Studio中,右键单击您的模块==> New ==> Package
2) name the package (folder) cpp
(or you can name it jni
) 2)将包(文件夹)命名为
cpp
(或者您可以将其命名为jni
)
3) you will see the cpp
directory on the left. 3)您将在左侧看到
cpp
目录。
4) You can create .cpp
, .h
and other files within that folder. 4)您可以在该文件夹中创建
.cpp
, .h
和其他文件。
Nowm you have to tell gradle
how to build that. 现在,您必须告诉
gradle
如何构建它。
You need install CMake
. 您需要安装
CMake
。 2 2
1) Go to Preferences ==> Android SDK ==> SDK Tools ==> CMake 1)转到首选项==> Android SDK ==> SDK工具==> CMake
2) Select that and click Apply and Ok 2)选择它,然后单击“应用”并单击“确定”
Now, you need to add a CMakeLists.txt
file to your project. 现在,您需要将
CMakeLists.txt
文件添加到您的项目中。
Path: my_project/app/CMakeLists.txt
路径:
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
: 最后一步:将以下内容添加到
build.gradle
:
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
You should be able to build it now. 您应该现在就可以构建它。
th3pat3l's answer is works fine, but the official documentation for how to add C++ to a project is a little different. th3pat3l的答案很好,但是有关如何将C ++添加到项目中的官方文档则有所不同。 Here it is:
这里是:
https://developer.android.com/studio/projects/add-native-code.html#create-sources https://developer.android.com/studio/projects/add-native-code.html#create-sources
The main difference is the use of file->new->package. 主要区别是使用file-> new-> package。 The package concept is for adding a java package and has a side effect of creating a folder.
包概念用于添加Java包,并且具有创建文件夹的副作用。
You can do the same thing more directly by switching to project view and just creating the folder where you want it in the directory. 您可以直接切换到项目视图,然后在目录中的所需位置创建文件夹,从而更直接地执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.