JNI "undefined reference" 到 c++ 方法

JNI "undefined reference" to c++ method

我正在尝试在 android studio 中使用 JNI 创建扑克应用程序,我想保存一个 c++ class (TexasHoldem) 实例并仅调用它的方法。 在 texasJNI.java 我写道:

public class jniTexasHoldem {
  private long texasHoldm;
  jniTexasHoldem() {
      ConstructNativeTexas();
  }


  // Used to load the 'native-lib' library on application startup.
  static {
      System.loadLibrary("native-lib");
  }

  /**
   * A native method that is implemented by the 'native-lib' native library,
   * which is packaged with this application.
   */
  private native void ConstructNativeTexas();

  public native String getCardXML();

  public native String stringFromJNI();
}

在主要活动中:

ublic class MainActivity extends AppCompatActivity {
  private jniTexasHoldem m_TexasHoldem = new jniTexasHoldem();
  ...
  String cardXml = m_TexasHoldem.getCardXML();
}

在本机库中:

extern "C"
JNIEXPORT jstring JNICALL
Java_android_myapplication_jniTexasHoldem_stringFromJNI(
    JNIEnv* env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jlong JNICALL
Java_android_myapplication_jniTexasHoldem_ConstructNativeTexas(JNIEnv *env, jobject thiz) {
  TexasHoldm* texasHoldmObj = new TexasHoldm();

  return (jlong)texasHoldmObj;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_android_myapplication_jniTexasHoldem_getCardXML(JNIEnv *env, jobject thiz) {
  jclass c = env->GetObjectClass(thiz);
  // J is the type signature for long:
  jfieldID fid_handle = env->GetFieldID(c, "texasHoldm", "J");
  TexasHoldm * nativeObject = (TexasHoldm *) env->GetLongField(thiz, fid_handle);

  return (env)->NewStringUTF(nativeObject->getCard().c_str());
}

我希望获得从 getCard 方法自动生成的 {card}.png 的字符串,但由于 undefined reference to 'TexasHoldm::getCard()'

它无法编译

我的build.gradle:

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
        version "3.10.2"
    }
}
ndkVersion "21.1.6352462"

我在 CmakeList 中写道:

target_link_libraries( # Specifies the target library.
                   native-lib
                   TexasHoldm.h
                   TexasHoldm.cpp
                   PokerTable.h
                   PokerTable.cpp
                   Player.h
                   Player.cpp
                   Hand.h
                   Hand.cpp
                   Card.h
                   Card.cpp


                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )

所有文件之间存在依赖关系,所以我在开始时清理了 CMakeLists 文件(仅使用 native-lib 的基本自动生成文件)我添加了 ndkVersion "21.1.6352462" 行并添加了所有外部 .cpp add_library 部分内的文件 CMakeLists 文件内的部分。

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).
             native-lib.cpp **extra .cpp files here**)

所以它把它全部编译在一个外部库中。