You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Qt安卓项目中引入.jar文件?遇QAndroidJniObject链接错误求助

Fixing Jar Integration & QAndroidJniObject Error for Epson BT-300 in Qt Android

Hey there! Let’s work through this issue together—integrating Java libraries into Qt Android projects can be tricky when you’re not deep into Java, but we’ll get this sorted.

Step 1: Properly Import the Epson Jar into Your Qt Project

First, let’s make sure the jar file is correctly added to your project so Qt can package it with your Android app:

  • Create a libs folder in your Qt project root (if you don’t have one already) and place the Epson brightness control jar inside it.
  • Open your .pro project file and add these lines (replace epson_display_control.jar with the actual name of your jar):
    # Enable Android extras module (required for QAndroidJniObject)
    QT += androidextras
    
    # Specify Android package source directory (create this folder if missing)
    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
    
    # Add the jar to extra libraries to be packaged with the app
    ANDROID_EXTRA_LIBS += $$PWD/libs/epson_display_control.jar
    
    The ANDROID_PACKAGE_SOURCE_DIR points to a folder for Android-specific files like the Manifest—you can generate a default one using Qt Creator’s "Add Android Manifest" option under your project settings.

Step 2: Fix the QAndroidJniObject Undefined Reference Error

That linker error usually pops up if either the Android extras module isn’t enabled, or you’re calling the JNI object incorrectly. We already enabled androidextras in Step 1, so let’s fix the JNI call:

Epson’s DisplayControl is likely a singleton class (common for device control APIs), so you shouldn’t instantiate it directly with the constructor. Instead, fetch the singleton instance using the static field, then call methods on it. Here’s the correct approach:

#include <QAndroidJniObject>
#include <QAndroidJniEnvironment>

// Fetch the DisplayControl singleton instance
QAndroidJniObject displayControl = QAndroidJniObject::getStaticObjectField(
    "com/epson/moverio/btcontrol/DisplayControl",  // Use slashes instead of dots for JNI class paths
    "INSTANCE",                                     // Name of the static singleton field (check Epson docs if this differs)
    "Lcom/epson/moverio/btcontrol/DisplayControl;" // Field type signature
);

// If the instance is valid, call the brightness control method
if (!displayControl.isValid()) {
    // Handle error: couldn't access the DisplayControl class
    qWarning() << "Failed to get DisplayControl instance!";
    return;
}

// Example: Set brightness to 50 (adjust value based on Epson's API specs)
// The method signature "(I)V" means: takes an integer (I) and returns void (V)
displayControl.callMethod<void>(
    "setBrightness",  // Name of the brightness method from Epson's jar
    "(I)V",           // Method signature (match what's in the Java docs)
    50                // Brightness value
);

Key notes here:

  • Use slashes (/) instead of dots (.) for JNI class paths.
  • Double-check the method and field names against Epson’s documentation—if INSTANCE isn’t the right singleton field name, adjust it accordingly.
  • The method signature must exactly match the Java method’s parameter and return types. For example, if the brightness method takes a float, use "(F)V" instead.

Step 3: Add Required Android Permissions

Epson’s display control API will need specific permissions to modify device settings. Open your android/AndroidManifest.xml and add these permissions inside the <manifest> tag:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

Note: The WRITE_SETTINGS permission is a sensitive one—you’ll need to prompt the user to enable it manually in their device settings, as it can’t be granted via the standard runtime permission request flow.

Step 4: Debug & Verify

If you still run into issues:

  • Check Qt Creator’s Logcat output (under the "Android Debug" tab) for errors like ClassNotFoundException—this means the jar wasn’t properly packaged with the app. Double-check the ANDROID_EXTRA_LIBS path in your .pro file.
  • Ensure the Epson jar doesn’t have hidden dependencies. Some vendor jars require additional Android libraries—check Epson’s documentation for any required dependencies and add them to your libs folder and ANDROID_EXTRA_LIBS as well.

内容的提问来源于stack exchange,提问作者Andrea Romano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:00:06