Qt Android项目编译报错:package android.support.v4.app不存在
Hey there, let's work through this frustrating error step by step—since you're using Qt 5.6+ with CMake (no Android Studio), we need to focus on how Qt handles Android dependencies and build configurations behind the scenes. Here are the key checks and fixes to try:
1. Verify Qt's Android Support Repository Path
Qt needs explicit access to your installed Android Support Repository to find the support-v4 libraries.
- Open Qt Creator (even if you're building via command line, this helps confirm config) and go to
Tools > Options > Devices > Android. - Double-check that the Android Support Repository path points to your actual installation (usually
<SDK_ROOT>/extras/android/m2repository). If this path is missing or incorrect, Qt can't locate the support-v4 packages.
2. Correct Gradle Dependencies (and Make Qt Respect Them)
Qt generates its own Gradle files during build, so you need to add dependencies in a way that Qt won't overwrite:
- Option 1: Use CMake to pass Gradle dependencies
Add this line to your CMakeLists.txt file:
Make sure the version (set(QT_ANDROID_GRADLE_DEPENDENCIES "implementation 'com.android.support:support-v4:26.1.0'")26.1.0) matches your target API level (26) — support library versions should align with your target SDK. - Option 2: Modify Qt's Gradle template
If you have anandroid/gradledirectory in your Qt project, edit thebuild.gradlefile there to include:
Also ensure your Gradle script includes the Google Maven repository (required for support libraries):dependencies { implementation 'com.android.support:support-v4:26.1.0' }repositories { google() mavenCentral() }
3. Ensure CMake Build Flags Are Correct
When running CMake via command line, double-check these critical parameters:
- Specify the Qt Android toolchain:
Adjust-DCMAKE_TOOLCHAIN_FILE=<QT_INSTALL_PATH>/5.6/android_armv7/lib/cmake/Qt5/Qt5AndroidToolchain.cmakeandroid_armv7to your target architecture (e.g.,android_arm64_v8a). - Set SDK/NDK paths explicitly:
-DANDROID_SDK_ROOT=<YOUR_SDK_PATH> -DANDROID_NDK=<YOUR_NDK_10d_PATH> - Confirm target SDK version matches your requirement:
(Support-v4 26.x requires a minimum SDK of 16 or higher.)-DANDROID_TARGET_SDK_VERSION=26 -DANDROID_MIN_SDK_VERSION=16
4. Clean Build Caches
Qt and Gradle love to cache old configurations, which can cause dependency issues:
- Delete your project's
builddirectory entirely. - Remove Qt's temporary Android build files (usually in
~/.cache/QtProjector your project's.androidsubdirectory). - Re-run your CMake configure and build commands from scratch to generate fresh Gradle files.
5. Match Support Repository Version to Target API
Make sure you've installed the correct version of the Android Support Repository for API 26:
- Open the Android SDK Manager and locate Android Support Repository.
- Ensure you have a version that includes the 26.x support-v4 libraries (avoid AndroidX packages, since Qt 5.6+ doesn't support AndroidX natively). If your Support Repository is too new, it might only have AndroidX replacements—you may need to install an older version of the repository that still includes the legacy support libraries.
6. Consider Upgrading Qt (If Possible)
Qt 5.6 was an early release for Android API 26 support, and later versions (like 5.9 LTS) have more robust Android dependency handling. If your project allows, upgrading to a newer Qt version might resolve this issue without extra configuration headaches.
内容的提问来源于stack exchange,提问作者Max Pashkov

