Android Studio 3.1.2调用C++子程序报错:No implementation found
Let's break down the possible issues causing this error and fix them step by step:
1. You're not loading the native library in your Java code
This is the most common culprit. Your Tab1 class doesn't include code to load the native-lib library you built with CMake. Without loading it, the JVM has no way to locate the native implementation.
Add this static block inside your Tab1.java class:
static { System.loadLibrary("native-lib"); }
Note: Use the exact library name you set in CMake (native-lib) — no file extensions like .so or .dll are needed here.
2. Double-check your JNI function signature (name and parameters)
While your signature looks correct at first glance, let's verify every detail to rule out typos:
- Package name:
com.labyrinthsys.crltextmatches perfectly between Java and C++ - Class name:
Tab1is correctly capitalized in both files - Method name:
crlTCheckkeyhas the exact same casing and spelling - Parameters: Your C++ function accepts
JNIEnv*,jobject,jstring— which aligns with the non-static native method in Java (non-static methods receive the class instance as the second parameter)
Also, confirm the entire JNIEXPORT function declaration is wrapped in extern "C" to avoid C++ name mangling. Your code shows this, but make sure there are no syntax errors here. A complete, valid implementation example would look like:
#include <jni.h> #include <string> using namespace std; extern "C" JNIEXPORT jstring JNICALL Java_com_labyrinthsys_crltext_Tab1_crlTCheckkey( JNIEnv *env, jobject /* this */, jstring keyFilePath) { // Extract the string from Java to native const char* nativeKeyPath = env->GetStringUTFChars(keyFilePath, nullptr); string result = "Processed path: " + string(nativeKeyPath); // Clean up resources env->ReleaseStringUTFChars(keyFilePath, nativeKeyPath); // Return result to Java return env->NewStringUTF(result.c_str()); }
3. Verify your CMake configuration builds the library correctly
- Ensure the path to your cpp file (
src/main/cpp/crlTCheckkey.cpp) is accurate relative to your CMakeLists.txt. If the CMake file is in your module root, this path should work — double-check that the file exists at that location. - Check your build output to confirm
native-libcompiles successfully (look for logs referencingcrlTCheckkey.cppbeing compiled).
4. Clean and rebuild your project
Stale build artifacts can sometimes cause mismatched libraries. Do a full clean to ensure you're using the latest compiled code:
- In Android Studio: Go to
Build > Clean Project, thenBuild > Rebuild Project
After trying these steps, your JNI call should locate the native implementation without errors.
内容的提问来源于stack exchange,提问作者John F Donigan

