Search code examples
androidc++android-ndk

Cannot include c++ standard in Android Studio


I have an Android Compose Project. Now I added C++ to it, and it works just fine with the initial file:

eazmath.cpp:

// Write C++ code here.
//
// Do not forget to dynamically load the C++ library into your application.
//
// For instance,
//
// In MainActivity.java:
//    static {
//       System.loadLibrary("eazmath");
//    }
//
// Or, in MainActivity.kt:
//    companion object {
//      init {
//         System.loadLibrary("eazmath")
//      }
//    }

#include <jni.h>
#include <string>

extern "C"
jstring Java_com_example_eazmath_expressionsimplifier_ExpressionSimplifier_test(JNIEnv *env,
                                                                                jobject caller)
{
    std::string str = "Hey There!"; // Just for testing
    return (*env).NewStringUTF(str.c_str());
}

But when I created added a new C++ class to the project, the following lines:

#include <jni.h>
#include <string>

Are showing an error saying:

'filename' file not found

Anyone knows what's up here?


Solution

  • I fixed this issue by adding the source files in CMakeLists.txt:

    add_library( # Sets the name of the library.
                 libname
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 cpp1.cpp cpp2.cpp cpp3.cpp ) # Do not separate these by commas!