C / C ++ with Android Studio version 2.2

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

+2
source share
2 answers

Short answer: Yes you can.

Here is what you can do 1
1) In Android Studio, right-click on your module ==> New ==> Package
2) specify the cpp package (folder) (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 in this folder.

image # 1

You should now tell gradle how to do this.
You need to install CMake . 2
1) Go to settings ==> Android SDK ==> SDK Tools ==> CMake
2) Select this and click "Apply" and "OK"

CMake Install

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

This is what the file looks 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 the last step: add the following to your build.gradle :

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

Now you can create it.

+4
source

th3pat3l's answer works fine, but the official documentation on how to add C ++ to the project is slightly different. Here he is:

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

The main difference is the use of the file-> new-> package. The concept of the package is to add the java package and has a side effect when creating a folder.

You can do the same thing more directly by switching to the project view and simply creating the folder in which you want to find it in the directory.

0
source

Source: https://habr.com/ru/post/1259259/


All Articles