1

我正在尝试让我的 Amazon In-app Purchases SDK 的 PurchasingObserver 的 java 实现通过本机方法与我的 C++ 代码进行通信。

PurchasingObserver.java //摘录

public class PurchasingObserver
{
    //...

    private native void postEvent(int type, String jsonData);
    // called by each of the four event handler methods, data is non-null
    // also tried it as native synchronized
}

AmazonInAppPurchaseHandler.cpp //摘录

static AmazonInAppPurchaseHandler* AmazonInAppPurchaseHandler::s_pInstance(0); // dumbleton

JNIEXPORT void JNICALL  _NativePurchasingObserverPostEvent(JNIEnv* pEnv, jobject obj, jint type, jstring jsonData)
// friend function
{
  assert(type >= 0);
  assert(type < AmazonInAppPurchaseHandler::kNumEventTypes); // event type is in range
  assert(pEnv != 0); // JNI environment is valid

  printf("Stuff from the native callback: %d, %p\n", type, jsonData); // never gets printed.

  int jsonDataLen(0);
  const char* pJsonDataUtfChars(0);
  if(jsonData != 0)
  {
    jsonDataLen = pEnv->GetStringUTFLength(jsonData);
    pJsonDataUtfChars = pEnv->GetStringUTFChars(jsonData, 0);
  }

  assert(s_pInstance != 0); // got AmazonInAppPurchaseHandler instance
  s_pInstance->DoCallback(type, jsonDataLen, pJsonDataUtfChars);

  pEnv->ReleaseStringUTFChars(jsonData, pJsonDataUtfChars);
}

static const JNINativeMethod karNativeMethod[] =
{
  { 
    "postEvent",
    "(ILjava/lang/String;)V",
    (void*)&_NativePurchasingObserverPostEvent
  }
};


AmazonInAppPurchaseHandler::AmazonInAppPurchaseHandler()
{
  assert(s_pInstance == 0); // is only instance
  s_pInstance = this;

  JNIEnv* pEnv(GetJNIEnv());
  assert(pEnv != 0); // got JNI environment

  jint  result(pEnv->RegisterNatives(cPurchaseObserver, karNativeMethod, 1));
  assert(AmazonInAppPurchaseHandler, result == 0); // successfully registered
}

AmazonInAppPurchaseHandler::~AmazonInAppPurchaseHandler()
{
  s_pInstance = 0;
}

一旦我执行任何生成事件的操作,就会发生以下情况: 1,事件处理程序被正确调用,它会进行日志记录。2,它还记录“发布事件...”,这是调用 postEvent() 之前的最后一件事。3,程序在 libc 中崩溃并带有 SIGSEGV。(它指的是我的应用程序,但请注意,反向 .com 名称被截断。我不确定这是否异常。) 4,C++ _NativePurchasingObserverPostEvent 中的跟踪永远不会到达。

日志:

12-05 10:24:47.380: D/com.mycompany.amazoninapp.PurchasingObserver@41970368(4604): onGetUserIdResponse: (com.amazon.inapp.purchasing.GetUserIdResponse@4196cf98, requestId: "dcf8e712-078b-4d47-9533- ee9ae544f53d”,getUserIdRequestStatus:“SUCCESSFUL”,userId:“DefaultTestUser”)

12-05 10:24:47.380: D/com.mycompany.amazoninapp.PurchasingObserver@41970368(4604): 发布事件...

12-05 10:24:47.380: A/libc(4604): 致命信号 11 (SIGSEGV) 在 0x00000008 (code=1), 线程 4604 (y.amazoninapp)

12-05 10:24:47.390: I/AmazonSDKTester(3529): 发送购买更新响应广播 ({"revokedSkus":[],"offset":"1354703087397","status":"SUCCESSFUL","re​​questId": "b9aee42e-4f50-42c4-8a12-ba9eb1d19155","isMore":false,"receipts":[{"sku":"com.mycompany.amazoninapp.ENTI01","token":"eyJ0eXBlIjoiTk9OQ09OU1VNQUJMRSIsInNrdSI6ImNvbS5wbGF5ZXJ0aHJlZS5hbWF6b25pbmFw\ncC5FTlRJMDEifQ\n", "itemType":"ENTITLED"}],"userId":"DefaultTestUser"})

我经历了正确获取 JNI 类名和方法签名字符串的迭代(我们已经过去了 UnsatisfiedLinkErrors);我正在仔细检查空字符串。我的 JNI 的其余部分工作正常,亚马逊应用内购买 UI 正确显示。保证在注册本机方法之前不会生成事件。

是什么导致了崩溃?

感谢您提前输入。

4

2 回答 2

0

您需要将您的函数声明为使用C调用约定。声明extern "C"如下:

extern "C" void _NativePurchasingObserverPostEvent(JNIEnv* pEnv, jobject obj, jint type, jstring jsonData)
于 2012-12-05T11:32:00.117 回答
0

尝试使用在 C++ 代码javah中创建一个标头#include,而不是自己编写函数原型。

于 2014-07-29T23:25:29.190 回答