Question

I have a button in an simple android app that I am using to call a native function. The button in main activity calls a function of HADriver.java. This function in turn calls on a JNI function in DriverAdapter.cpp. That JNI function then in turn calls on a native function in Driver.cpp. The following are the parts of each of those files that comes into play when the button testCout is clicked in MainActivity.

Button in MainActivity:

private HADriver driver = new HADriver();
....

testCout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            driver.testCout();

        }
    });

HADriver.java

package com.ihearhtpi;

public class HADriver {

    static {  
        System.loadLibrary("gnustl_shared");
        System.loadLibrary("driveradapter");  
    } 

    public native void testCout();
}

DriverAdapter.cpp

#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <Driver/driver.h>
#define DEBUG_TAG "NativeCalls"

void Java_com_ihearhtpi_HADriver_testCout(JNIEnv * env, jobject thiz)
{
    Driver* driver = new Driver();
    driver->testCoutFunc();
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", "Testing... this best effing work!");
    delete driver;
}

Driver.cpp

#include <Driver/driver.h>
#include <iostream>
...
//bunch of custom irrelevant functions and includes

Driver::Driver()
{
    CustomFuncs::initBitMasks(); // call global namespace function of CustomFuncs
}

void Driver::testCoutFunc()
{
    std::cout << "These are the droids you're looking for." << std::endl;
}

However, I keep getting the following error and my app crashes each time I click the testCout button.

04-13 02:22:46.130: W/dalvikvm(17228): No implementation found for native Lcom/ihearhtpi/HADriver;.testCout:()V
04-13 02:22:46.130: D/AndroidRuntime(17228): Shutting down VM
04-13 02:22:46.130: W/dalvikvm(17228): threadid=1: thread exiting with uncaught exception (group=0x40e0c300)
04-13 02:22:46.130: E/AndroidRuntime(17228): FATAL EXCEPTION: main
04-13 02:22:46.130: E/AndroidRuntime(17228): java.lang.UnsatisfiedLinkError: Native method not found: com.ihearhtpi.HADriver.testCout:()V
04-13 02:22:46.130: E/AndroidRuntime(17228):    at com.ihearhtpi.HADriver.testCout(Native Method)

What gives? I can't figure out why this native call is not working!

Était-ce utile?

La solution

Apparently in my JNI file I needed to wrap all functions with

#ifdef __cplusplus
extern "C" {
#endif

...JNI functions here...

#ifdef __cplusplus
}
#endif

After this, everything is working fine

Autres conseils

try not calling testCout through driver. try to make a wrapper function so something like driver.testCoutHelper(); in the onclick.

then in testCoutHelper() (within HADriver.java) can you call testCout(); Lets try this first and tell me the results;

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top