4

Is there a way to access a static final field in JNI?

public class TryMe {
    public int a = 1;
    public final int b = 2;
    public static int c = 3;
    public static final int d = 4;
}

The C++ JNI code:

jclass cls = env->FindClass("my/package/TryMe");

jfieldID a = env->GetFieldID(cls, "a", "I"); //OK
jfieldID b = env->GetFieldID(cls, "b", "I"); //OK
jfieldID c = env->GetStaticFieldID(cls, "c", "I"); //OK
jfieldID d = env->GetStaticFieldID(cls, "d", "I"); //Error!

The last call returns NULL and fails. Any idea why?

Tomek
  • 79
  • 1
  • 4

2 Answers2

3

SOLVED: I've been bitten by proguard.

I initially thought it might be a bug in the android runtime but it isn't. I believe that proguard doesn't know and doesn't care about JNI so it decided to optimize out the "constant". After adding this to proguard.cfg:

keep class my.package.TryMe {
    public static final int d;
}

the problem has disappeared.

Tomek
  • 79
  • 1
  • 4
  • No doubt, but you still need to check the result of every JNI call, and the initial problem was that you weren't doing that. – user207421 Feb 02 '15 at 13:35
0

After using javap -p -s TryMe, does the variable d appear like:

...
public static final int d;
descriptor: I
...

?

If so, then I dont really see a reason why it shouldn't work. What platform are you trying this on? I tried your example with the following code on Arch x86_64 and it works for me:

Test.java:

public class Test {
    public int a = 1;
    public final int b = 2;
    public static int c = 3;
    public static final int d = 4;
}

ctest.cpp:

#include <iostream>
#include <jni.h>

using namespace std;

int main(){
     JavaVM *jvm;
     JNIEnv *env;

    JavaVMInitArgs vm_args;
    JavaVMOption options[1];
    options[0].optionString = const_cast<char*>("-Djava.class.path=/home/fishi/Workspace/tmp/");
    vm_args.version = JNI_VERSION_1_8;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    /* Create the Java VM */
    int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args); 

     if (res == JNI_ERR){
         cout << "Fail: Unable to load JVM \t Exit" << endl;
        return 1;
     }
         else if (res == JNI_OK){
         cout << "CreateVM:\t\tJVM loaded successfully!" << endl;
     }

    /* Find class and static field */
    jclass cls = env->FindClass("Test");
    if(cls == NULL)
    {
        cout << "FindClass:\t\tCould not load class!" << endl;
        return 1;
    }
    else
    {
        cout << "FindClass:\t\tLoaded class successfully!" << endl;
    }
    jfieldID field = env->GetStaticFieldID(cls, "d", "I");
    if(field == NULL)
    {
        cout << "GetField:\t\tError!" << endl;
        return 1;
    }

    cout << "Success" << endl;
}

Makefile:

build-jni:
    g++ -g -I/usr/lib/jvm/java-8-openjdk/include/ -I/usr/lib/jvm/java-8-openjdk/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/ ctest.cpp -ljvm -o ctest

run-jni:
    LD_LIBRARY_PATH=/usr/bin/java:/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/ ./ctest

Output:

$ make run-jni
LD_LIBRARY_PATH=/usr/bin/java:/usr/lib/jvm/java-8-openjdk/jre/lib/amd64/server/ ./ctest
CreateVM: JVM loaded successfully!
FindClass: Loaded class successfully!
Success

Hope this is helpful in some way.

fishi0x01
  • 3,579
  • 21
  • 25