QtJenny: 안드로이드 API에 액세스하기 위한 C++ 프록시 클래스 생성하기

QtJenny의 사용법을 보여주는 데모입니다.

개요

이 데모는 QtJenny를 사용하여 C++ 코드에서 안드로이드 API에 액세스하기 위한 C++ 프록시 클래스를 생성하는 방법을 보여줍니다. 생성된 C++ 클래스는 데모에서 볼륨 및 밝기 조정, 깨우기 잠금 활성화 및 비활성화, 알림 전송 및 진동 트리거와 같은 작업을 수행하는 데 사용됩니다. 이러한 작업은 Qt가 구현하지 않는 Android API의 일부입니다.

QtJenny를 사용하여 C++ 클래스를 생성하면 JNI 코드를 수동으로 작성할 필요가 없습니다.

작동 방식

이 데모는 qtjenny_consumer 이라는 Qt 프로젝트와 qtjenny_generator 이라는 안드로이드 스튜디오 프로젝트의 두 부분으로 구성되어 있습니다. qtjenny_consumer 에는 애플리케이션 UI와 생성된 C++ 헤더를 사용한 코드가 포함되어 있습니다. qtjenny_generator 에는 클래스 어노테이션과 QtJenny에 대한 Gradle 구성이 포함되어 있습니다.

데모를 실행하려면 qtjenny_consumer 프로젝트를 실행해야 합니다. qtjenny_consumerCMake 구성 중에 코드 생성은 qtjenny_generator 프로젝트 디렉터리에서 gradlew 작업을 실행하는 호출에 의해 자동으로 트리거됩니다.

if (ANDROID)
    if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
        set (gradlew_cmd "gradlew.bat")
    else()
         set (gradlew_cmd "./gradlew")
    endif()
    set (gradlew_arg "--rerun-tasks")
    set (gradlew_task "kaptReleaseKotlin")
    execute_process(COMMAND ${gradlew_cmd} ${gradlew_arg} ${gradlew_task}
    WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/qtjenny_generator")
else()
    message(FATAL_ERROR "Example only works on Android")
endif()
C++ 헤더 생성

C++ 헤더 생성은 qtjenny_generator 빌드 시 시작되며, 이는 GenerateCppCode.kt 에서 어노테이션을 처리하는 어노테이션 프로세서를 트리거합니다.

@NativeClass
@NativeProxy(allMethods = false, allFields = false)
@NativeProxyForClasses(namespace = "android::os", classes = [BatteryManager::class, VibratorManager::class, Vibrator::class, VibrationEffect::class, Context::class, PowerManager::class, PowerManager.WakeLock::class])
@NativeProxyForClasses(namespace = "android::view", classes = [Window::class, WindowManager.LayoutParams::class])
@NativeProxyForClasses(namespace = "android::media", classes = [AudioManager::class])
@NativeProxyForClasses(namespace = "android::drawable", classes = [android.R.drawable::class])
@NativeProxyForClasses(namespace = "android::app", classes = [Activity::class, Notification::class, Notification.Builder::class, NotificationChannel::class, NotificationManager::class])
@NativeProxyForClasses(namespace = "android::provider", classes = [Settings.Global::class, Settings.System::class, Settings::class])
@NativeProxyForClasses(namespace = "android::content", classes = [Intent::class])

그러면 어노테이션 프로세서가 qtjenny_output 디렉터리에 C++ 헤더를 생성합니다. 이러한 C++ 헤더에는 이 데모에 사용된 Android API에 액세스하는 데 필요한 JNI 상용구 코드가 포함되어 있습니다.

qtjenny_generator 의 앱 수준 build.gradle 스크립트는 QtJenny가 구현하는 kapt의 인수를 지정합니다. 이러한 인수는 QtJenny 컴파일러에서 파싱되어 생성 프로세스에서 사용됩니다.

kapt {
    arguments {
        // pass arguments to jenny
        arg("jenny.outputDirectory", project.file("../../qtjenny_output"))
        arg("jenny.templateDirectory", project.file("../templates"))
        arg("jenny.headerOnlyProxy", "true")
        arg("jenny.useJniHelper", "false")
        arg("jenny.useTemplates", "true")
    }
}
Qt Quick 애플리케이션에서 생성된 C++ 헤더 사용

qtjenny_consumer Qt Quick 애플리케이션은 에서 생성된 C++ 헤더를 사용합니다. 생성된 헤더는 파일에 포함되며, 에서 다양한 안드로이드 API에 액세스하는 데 사용됩니다. qtjenny_generator backend.h backend.cpp

앱의 UI는 하나의 Main.qml 파일로 구성되며, 이 파일에는 다음과 같은 컨트롤과 동작이 포함되어 있습니다.

깨우기 잠금

Switches 을 사용하여 전체 또는 부분 깨우기 잠금을 활성화 및 비활성화할 수 있습니다. 체크하면 깨우기 잠금을 활성화하고 깨우기 잠금 상태 텍스트를 설정하는 backend.cpp 의 함수가 호출됩니다.

if (checked) {
    myBackEnd.setFullWakeLock()
    if (partialWakeLock.checked)
        partialWakeLock.click()
    mainWindow.wakeLockStatus = "Full WakeLock active"

부분 깨우기 잠금을 설정하려면 PowerManager.WakeLock Android API에 연결되는 WakeLockProxy 클래스를 사용합니다. 전체 깨우기 잠금을 설정하려면 Window Android API에 연결되는 WindowProxy 클래스를 사용합니다.

진동

backend.cppvibrate 함수를 사용하여 진동을 트리거할 수 있으며, VibrationEffectProxy, VibratorManagerProxyVibratorProxy 클래스를 사용하여 진동을 수행합니다.

알림

알림 전송은 backend.cppnotify 함수에서 처리되며 NotificationManagerProxy 클래스를 사용합니다.

알림은 createNotification 함수의 Backend 클래스 초기화 중에 이미 생성됩니다.

밝기 조정하기

밝기 조정은 backend.cppadjustBrightness 함수에서 처리되며 IntentProxy, SettingsProxy, SystemProxy, ContextProxy, LayoutParamsProxyWindowProxy 클래스를 사용합니다.

볼륨 조절하기

볼륨 조절은 backend.cppadjustVolume 함수에서 처리되며 GlobalProxyAudioManagerProxy 클래스를 사용합니다.

예제 프로젝트 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.