[Interest] (JNI ERROR) jbytearray -> java.lang.String[] - What am I doing wrong here?
Jason H
jhihn at gmx.com
Sat Jan 26 01:01:57 CET 2019
So I think i got that completely wrong. I'm more used to using androidextras. They have a nice API that cleans up for you. I gotta that's not an option? Maybe they JNI stuff should get it's own module separate from Android?
> Sent: Friday, January 25, 2019 at 9:00 PM
> From: "Jason H" <jhihn at gmx.com>
> To: "René Hansen" <renehh at gmail.com>
> Cc: interest <interest at qt-project.org>
> Subject: Re: [Interest] (JNI ERROR) jbytearray -> java.lang.String[] - What am I doing wrong here?
>
>
>
> I'm not familiar with that, jbteArray, but in attemping your challenge, I saw:
>
> qjniEnv->SetByteArrayRegion(jdata, 0, _size, reinterpret_cast<jbyte *>(foo));
>
> but saw this in the docs:
> void Set<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize start, jsize len, NativeType *buf);
>
>
> is jdata your jni env?
>
> Sent: Friday, January 25, 2019 at 1:13 PM
> From: "René Hansen" <renehh at gmail.com>
> To: interest <interest at qt-project.org>
> Subject: [Interest] (JNI ERROR) jbytearray -> java.lang.String[] - What am I doing wrong here?
>
> All I want to do is send some bytes from Qt to Java, but I'm running into an annoying problem, where my jbyteArray gets turned into java.lang.String[] in mid-flight:
>
>
> E art : JNI ERROR (app bug): attempt to pass an instance of java.lang.String[] as argument 1 to int Foo.bar(byte[])
> F art : art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: bad arguments passed to int Foo.bar(byte[]) (see above for details)
> F art : art/runtime/java_vm_ext.cc:410] from void org.qtproject.qt5.android.QtNative.runPendingCppRunnables()
> F art : art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
> ...
>
> Here's a minimal example; just a simple auto test project:
>
> TestJNI.pro:
>
>
> QT += testlib androidextras
> QT -= gui
>
> CONFIG += qt console warn_on depend_includepath testcase
> CONFIG -= app_bundle
>
> TEMPLATE = app
>
> SOURCES += tst_test_jni.cpp
>
> ANDROID_PACKAGE_SOURCE_DIR = $$PWD
>
> DISTFILES += \
> src/Foo.java
>
> tst_test_jni.cpp:
>
> #include <QtTest>
> #include <QAndroidJniEnvironment>
> #include <QtAndroid>
>
> class test_jni : public QObject
> {
> Q_OBJECT
>
> private slots:
> void test_case1();
> };
>
> void test_jni::test_case1()
> {
> QAndroidJniEnvironment qjniEnv;
> jsize _size = static_cast<jsize>(5);
> jbyteArray jdata = qjniEnv->NewByteArray(_size);
> jint result;
>
> if (jdata == nullptr)
> QFAIL("Failed creating new jbyteArray");
>
> if (qjniEnv->GetArrayLength(jdata) != _size)
> {
> qjniEnv->DeleteLocalRef(jdata);
> QFAIL("Failed to allocate entire array through JNI");
> }
>
> // Lorem
> char foo[] = { 0x4c, 0x6f, 0x72 ,0x65, 0x6d };
>
> qjniEnv->SetByteArrayRegion(jdata, 0, _size, reinterpret_cast<jbyte *>(foo));
>
> QtAndroid::runOnAndroidThread([jdata] {
> QAndroidJniObject::callStaticMethod<jint>(
> "Foo",
> "bar",
> "([B)I",
> jdata);
> });
>
> QCOMPARE(result, 42);
> }
>
> QTEST_APPLESS_MAIN(test_jni)
>
> #include "tst_test_jni.moc"
>
> Foo.java:
>
> public class Foo {
> static int bar(byte baz[]) {
> return 42;
> }
> }
>
> Have I missed something very basic here?
>
>
> Best regards,
>
> René Hansen_______________________________________________ Interest mailing list Interest at qt-project.org https://lists.qt-project.org/listinfo/interest_______________________________________________ Interest mailing list Interest at qt-project.org https://lists.qt-project.org/listinfo/interest
More information about the Interest
mailing list