您现在的位置是:网站首页> C/C++
qt android 引用不带界面的aar包
- C/C++
- 2023-08-25
- 523人已阅读
as生成aar步骤:前边已详细说明步骤,这里大体一说。
1、在应用上右键,new-module-android library,名字mylib
2、在mylib-src-main-java-com.example.mylib上右键new-java class。名字Test
package com.example.mylib;
public class Test {
public Test(int i)
{
System.out.println("test实例化");
}
public static int fun1(){
System.out.println("静态函数");
return 1;
}
public int fun2(){
System.out.println("实例函数");
return 2;
}
}
3、build-rebuild project,生成aar
qt引用步骤:
1、新建安卓应用test,pro文件增加:QT += androidextras
点项目->build android apk-> create templates。
2、将mylib-debug.aar拷贝到D:\cx\test\testandroid\test\android\libs目录。
3、修改build.gradle,增加repositories { flatDir { dirs 'libs' }},dependencies增加compile(name:'mylib-debug', ext:'aar')
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'mylib-debug', ext:'aar')
}
4、窗口增加个按钮测试
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtAndroid>
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>
#include<QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QAndroidJniObject testobj("com/example/mylib/Test","(I)V",10);
if(testobj.isValid())
{
jint funret1=testobj.callMethod<jint>("fun2","()I");
QString str1=QString("%1").arg(funret1);
QMessageBox::about(NULL,"okts","ret="+str1);
jint funret2=QAndroidJniObject::callStaticMethod<jint>("com/example/mylib/Test","fun1","()I");
QString str2=QString("%1").arg(funret2);
QMessageBox::about(NULL,"okts","ret="+str2);
}
else
{
QMessageBox::about(NULL,"ts","no");
}
}