CMakeLists.txt
build script, or uses ndk-build and includes an Android.mk
build script, you can skip this step. ndkCompile
tool, you should migrate to using either CMake or ndk-build. To learn more, go to the section about how to Migrate from ndkCompile.build.gradle
are built with CMake 3.10.2. To use the former included version, specify CMake version 3.6.0 in your module's build.gradle
file: While not a best practice, you may specify the CMake version as the minimum version by adding a + to the end of the build.gradle
entry, such as 3.10.2+
.build.gradle
file: PATH
environment variable or include it in your project's local.properties
file, as shown below. If Gradle is unable to find the version of CMake you specified in your build.gradle
file, you get a build error. PATH
environment variable. native-lib.cpp
, and places it in the src/main/cpp/
directory of your app module. This sample code provides a simple C++ function, stringFromJNI()
, that returns the string 'Hello from C++'. You can learn how to add additional source files to your project in the section about how to Create new native source files.build.gradle
files tell Gradle how to build your app, CMake and ndk-build require a build script to know how to build your native library. For new projects, Android Studio creates a CMake build script, CMakeLists.txt
, and places it in your module’s root directory. To learn more about the contents of this build script, read Configure CMake.CMakeLists.txt
. native-lib.cpp
, into a shared object library and names it libnative-lib.so
, which Gradle then packages into the APK. MainActivity
loads the native library using System.loadLibrary()
. The library’s native function, stringFromJNI()
, is now available to the app. MainActivity.onCreate()
calls stringFromJNI()
, which returns 'Hello from C++', and uses it to update the TextView
. app/build/outputs/apk/
directory and click OK. libnative-lib.so
in the APK Analyzer window under lib/<ABI>/
. cpp/
directory in the main source set of your app, create one as follows:cpp
as the directory name and click OK. cpp/
directory and select New > C/C++ Source File. native-lib
. .cpp
. .cxx
or .hxx
, by clicking Edit File Types . In the C/C++ dialog box that pops up, select another file extension from the Source Extension and Header Extension drop-down menus and click OK.