Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

android - Properly returning a hardcoded byte[] from JNI to Java

I want to hardcode a 16 byte array in JNI and return it with a method.

This isn't working

static jbyteArray JNICALL getKeyBytes(JNIEnv *env, jobject thiz) 
{
    F_LOG;
    Mutex::Autolock _m(sLock);


    jbyteArray result;  
    jbyte* resultType = new jbyte[16];  
    result = (*env)->NewByteArray(env, 16);  //line 214
    resultType = {52, 14, 25, 32, 75, 83, 35, 89, 40, 69, 35, 73, 84, 82, 35, 30};
    (*env)->SetByteArrayRegion(env, result, 0, 16, resultType);
    delete [] resultType;

    return result;
}

I get the following errors

NativeCodeCaller.cpp:214:17: error: base operand of '->' has non-pointer type '_JNIEnv'

NativeCodeCaller.cpp:215:78: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x

NativeCodeCaller.cpp:215:78: error: cannot convert "brace-enclosed initializer list>" to 'jbyte*' in assignment

NativeCodeCaller.cpp:216:8: error: base operand of '->' has non-pointer type '_JNIEnv'

any quick help? :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error base operand of '->' has non-pointer type indicates that you should be using . instead of ->.

So whether you use (*env).NewByteArray(env, 16); or env->NewByteArray(env, 16);. This is the same for line 216.

You also have another error at the following line (215) saying cannot convert "brace-enclosed initializer list>" to 'jbyte*' in assignment, because the brace syntax for assignment is only valid where you declare the array/pointer (and I think it depends on the compiler too, but I'm not that sure).

You should try with:

jbyte resultType[16] = {52, 14, 25, 32, 75, 83, 35, 89, 40, 69, 35, 73, 84, 82, 35, 30};

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...