Just putting this here so I have it when I need it. I haven’t cleaned this up so it’s easy to understand, but there’s not a lot to it. Just need to have the pieces below and you’ve got everything you need to quickly and easily bold cpp into an Android Java app.
Handy links I used to figure it out: –
here
and here
and maybe here too
Prereqs: Android Studio 3.4.1, NDK version 16.1 (but I doubt it matters which NDK)
Files you’ll need: –
DeltaJNI.cpp: Simple cpp file with a method that returns a String to Java. Must be in a cpp directory
#include <jni.h> // JNI header provided by JDK
#include <stdio.h> // C Standard IO Header
#include <iostream>
#include "DeltaJNI.h" // Generated
#include <string>
// Implementation of the native method sayHello()
JNIEXPORT jstring JNICALL Java_com_ancientcoder_recog_MainActivity_sayHello(JNIEnv *env, jobject thisObj) {
std::string hello = "-------------------------------- Hello from C++";
return env->NewStringUTF(hello.c_str());
}
DeltaJNI.h: Header file to go with the above cpp. Must be with the cpp file above in a cpp directory (see CMakeLists.txt for where I put it)
#include <jni.h>
#ifndef RECOG_DELTAJNI_H
#define RECOG_DELTAJNI_H
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jstring JNICALL Java_com_ancientcoder_recog_MainActivity_sayHello(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif //RECOG_DELTA_H
CMakeLists.txt: Added to the app directory to facilitate building of the ‘native-lib’ lib
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
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/DeltaJNI.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries 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 this
# 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} )
build.gradle (in the ‘app’ directory): Add this at the bottom of (and inside of ) the “android {}” section:
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
…and this at the bottom of (and inside of) of the “defaultConfig {}” section: –
externalNativeBuild {
cmake {
cppFlags ""
}
}
Java: Add this to the java class that’s calling the C++. This loads the lib that’s built by the above CMakeLists.txt (includes the cpp file(s)) and declares a prototype for the sayHello method.
static {
System.loadLibrary("native-lib"); // Load native library at runtime
// This library contains a native method called sayHello()
}
private native String sayHello();
More Java: …and finally, add this to actually call the cpp from the java code. This will log out the returned string from the cpp code: –
Log.e(TAG, sayHello());