Android无法找到我的JNI本机库函数

时间:2017-01-17 01:21:49

标签: java android c++ android-ndk java-native-interface

我使用javah生成本机JNI函数:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_ttm_zpay_zPayTestTool */

#ifndef _Included_com_ttm_zpay_zPayTestTool
#define _Included_com_ttm_zpay_zPayTestTool
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_ttm_zpay_zPayTestTool
 * Method:    KiziStartTransaction
 * Signature: ()[B
 */
JNIEXPORT jbyteArray JNICALL Java_com_ttm_zpay_zPayTestTool_KiziStartTransaction
  (JNIEnv * env, jobject)
{
   return env->NewByteArray(10);
}

#ifdef __cplusplus
}
#endif
#endif

对于以下Java类:

package com.ttm.zpay;

public class zPayTestTool
{
   public native byte[] KiziStartTransaction();
}

我验证了本机功能已成功编译到我的APK包装的最终* .so文件中。我是通过使用readelf -Ws lib.so(由NDK提供的readelf)来实现的:

5: 0015fa15    10 FUNC    GLOBAL DEFAULT    8 Java_com_ttm_zpay_zPayTestTool_KiziStartTransaction

在logcat输出中,我得到以下内容:

01-17 01:06:02.306  7017  7017 W dalvikvm: No implementation found for native Lcom/ttm/zpay/zPayTestTool;.KiziStartTransaction:()[B
01-17 01:06:02.306  7017  7017 D AndroidRuntime: Shutting down VM
01-17 01:06:02.311  7017  7017 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40ccd930)
01-17 01:06:02.316  3556  3758 D AudioHardware: openPcmOut_l() mPcmOpenCnt: 0
01-17 01:06:02.321  7017  7017 E AndroidRuntime: FATAL EXCEPTION: main
01-17 01:06:02.321  7017  7017 E AndroidRuntime: java.lang.UnsatisfiedLinkError: Native method not found: com.ttm.zpay.zPayTestTool.KiziStartTransaction:()[B
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.ttm.zpay.zPayTestTool.KiziStartTransaction(Native Method)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.ttm.zpay.zPayActivity.OnKiziStartTransaction(zPayActivity.java:97)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.ttm.zpay.zPayActivity.access$1(zPayActivity.java:95)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.ttm.zpay.zPayActivity$1.onClick(zPayActivity.java:90)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.view.View.performClick(View.java:4204)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.view.View$PerformClick.run(View.java:17355)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:725)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:137)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:5041)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at java.lang.reflect.Method.invokeNative(Native Method)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Method.java:511)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-17 01:06:02.321  7017  7017 E AndroidRuntime:        at dalvik.system.NativeStart.main(Native Method)
01-17 01:06:02.331  3837  3848 W ActivityManager:   Force finishing activity com.ttm.zpay/.zPayActivity

更奇怪的是,我已经在这个库中使用相同的命名约定使用另一个本机方法,但是对于一个完全正常工作的不同java类。只有上面那个会导致问题。

请参阅下面的工作JNI函数的代码。

爪哇:

package com.ttm.zpay;
public class zPayService extends Service
{
   public native boolean Initialize();
}

C ++:

extern "C"
{
    JNIEXPORT bool JNICALL Java_com_ttm_zpay_zPayService_Initialize(JNIEnv* env, jobject obj)
    {
        // Do stuff
    }
}

所以在一天结束时:为我的zPayTestTool java类映射的本机方法不能工作,但映射到zPayService java类的一个本机方法工作正常

我到底在做什么?这是我的AndroidManifest.xml的问题吗?我现在已经没有想法了,谷歌和其他看似重复的问题的结果也无济于事。

2 个答案:

答案 0 :(得分:1)

您需要在build.gradle中添加它

IBM\WebSphere\Profiles\myproject\installedApps\iap\myproject_exploded.ear\myProjectWeb.war

您可以右键单击android studio右侧项目窗格中的app文件夹 去选项 将c / c ++代码链接到您的项目

答案 1 :(得分:0)

我弄清楚问题是什么。在我的AndroidManifest.xml中,我将process属性设置为<service>元素而不是<application>

<application
    android:name="com.ttm.zpay.zPayApplication"
    android:allowBackup="true"
    android:label="@string/app_name" 
    android:persistent="true" >

    <service
        android:name="com.ttm.zpay.zPayService"
        android:exported="true"
        android:process="com.ttm.zPayService" >
    </service>

    <activity
        android:name="com.ttm.zpay.zPayActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

无论出于何种原因,这会导致活动类中定义的任何JNI方法或它使用的类不起作用。我已将process属性移至<application>,现在似乎可以正常运行:

<application
    android:name="com.ttm.zpay.zPayApplication"
    android:allowBackup="true"
    android:label="@string/app_name" 
    android:persistent="true"
    android:process="com.ttm.zPayService">

    <service
        android:name="com.ttm.zpay.zPayService"
        android:exported="true">
    </service>

    <activity
        android:name="com.ttm.zpay.zPayActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>