Android NDK passing long values to native method

This is my function:

Long Java_my_package_MainActivity_getDistance(JNIEnv* env, jobject obj, Long a_id1,Long a_id2)
        char temp[128];
        sprintf(temp,"ID1: %u , ID2: %u",a_id1, a_id2);
        __android_log_print(ANDROID_LOG_INFO, "From NDK : ", temp);
        return(0);//just to test ... 

Long is a type def:

typedef long long Long;

because i have some values that are grater than the max value of int type

in my java class:

 static {
        // declare the native code function - must match ndkfoo.c
    private native int getDistance(long id1, long id2);

I call the native function with this line:


the output in the logcat is:

ID1: 1234, ID2: 0

I always get ID2 = 0 !!, i don't know why the second parameter always evaluate to 0 !


Or try that:

#include <jni.h>
#include <android/log.h>

#define TAG "Java_my_package_MainActivity"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)

// ...

JNIEXPORT void JNICALL Java_my_package_MainActivity_getDistance(JNIEnv *env, jobject obj, jlong a_id1, jlong a_id2){

    // ...

    LOGI("ID1: %lu , ID2: %lu", a_id1, a_id2);

Your sprintf needs to use %llu to print a 64-bit quantity rather than %u. I believe you're you're actually getting the correct values through JNI, but the sprintf call is being told to expect two 32-bit values and is printing the high word of the first argument.

Also, you should probably be using jlong in JNI declarations rather than your own custom type (it's defined in jni.h).

Need Your Help

MySQL 5.6 individual timezones for each database

mysql sql database rdbms

How can I set individual timezone for each database on MySQL server?

Java: double rounding algorithm

java floating-point rounding

I got curious about a rounding algorithm, because in CS we had to emulate an HP35 without using the Math library. We didn't include a rounding algorithm in our final build, but I wanted to do it a...