simpleJNI.hpp
#include <QObject>
{
Q_OBJECT
public:
explicit simpleJNI
(QObject *parent
= nullptr
);
Q_INVOKABLE
QString printHelloWorld
();
};
#include <QObject>
class simpleJNI : public QObject
{
Q_OBJECT
public:
explicit simpleJNI(QObject *parent = nullptr);
Q_INVOKABLE QString printHelloWorld();
};
To copy to clipboard, switch view to plain text mode
simpleJNI.cpp
#include "simpleJNI.hpp"
#include <QtAndroidExtras/QAndroidJniObject>
simpleJNI
::simpleJNI(QObject *parent
) :{
}
QString simpleJNI
::printHelloWorld() {
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
"getString",
"(V)Ljava/lang/String;"
);
return str.toString();
}
#include "simpleJNI.hpp"
#include <QtAndroidExtras/QAndroidJniObject>
simpleJNI::simpleJNI(QObject *parent) :
QObject(parent)
{
}
QString simpleJNI::printHelloWorld()
{
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
"getString",
"(V)Ljava/lang/String;"
);
return str.toString();
}
To copy to clipboard, switch view to plain text mode
appBucksAdv.java
//package adv;
import org.qtproject.qt5.android.bindings.QtApplication;
import org.qtproject.qt5.android.bindings.QtActivity;
//import android.content.Context;
public class appBucksAdv extends QtActivity
{
public static String getString()
{
return "hello world";
}
}
//package adv;
import org.qtproject.qt5.android.bindings.QtApplication;
import org.qtproject.qt5.android.bindings.QtActivity;
//import android.content.Context;
public class appBucksAdv extends QtActivity
{
public static String getString()
{
return "hello world";
}
}
To copy to clipboard, switch view to plain text mode
main.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
Rectangle {
width: 100
height: 62
color: "red"
Text{
id: text
height: 30
width: parent.width
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
text.text = text.text + simpleJNI.printHelloWorld() + "\n"
}
}
}
import QtQuick 2.2
import QtQuick.Controls 1.1
Rectangle {
width: 100
height: 62
color: "red"
Text{
id: text
height: 30
width: parent.width
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
text.text = text.text + simpleJNI.printHelloWorld() + "\n"
}
}
}
To copy to clipboard, switch view to plain text mode
But the program do not print “helloWorld\nâ€
message :
W/dalvikvm( 6727): Bogus method descriptor: (V)Ljava/lang/String;
Layout of the project https://github.com/stereomatchingkis...er/androidTest
Added after 28 minutes:
If I change the signature of the function to
public static String printHelloWorld(int a)
{
return "hello world";
}
public static String printHelloWorld(int a)
{
return "hello world";
}
To copy to clipboard, switch view to plain text mode
and simpleJNI.cpp to
QString simpleJNI
::printHelloWorld() {
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
"printHelloWorld",
//change (V) to (I)
"(I)Ljava/lang/String;",
23);
return str.toString();
}
QString simpleJNI::printHelloWorld()
{
QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod("adv/appBucksAdv",
"printHelloWorld",
//change (V) to (I)
"(I)Ljava/lang/String;",
23);
return str.toString();
}
To copy to clipboard, switch view to plain text mode
The codes work
Bookmarks