Can't import androidx. Error: 'Package androidx.core.app does not exist.'
-
I would like to integrate Android Auto compatibility in my mobile app. For this I would like to use the androidx library. However when I try to import anything from androidx I get an error.
NotificationRegistry.java:17: error: package androidx.core.app does not exist :-1: error: ninja: build stopped: subcommand failed.
For example currently I am trying to import "import androidx.core.app.RemoteInput;" in my java class. I found a topic on this forum dated back in 2020 where it said to enableJetifier I did this, however that did not help me.
Below is my build.gradle
buildscript { repositories { google() mavenCentral() } dependencies { //noinspection AndroidGradlePluginVersion classpath 'com.android.tools.build:gradle:8.9.0' } } allprojects { repositories { google() mavenCentral() } } repositories { google() mavenCentral() } apply plugin: qtGradlePluginType dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) //noinspection GradleDependency implementation 'androidx.core:core:1.15.0' } android { /******************************************************* * The following variables: * - androidBuildToolsVersion, * - androidCompileSdkVersion * - qtAndroidDir - holds the path to qt android files * needed to build any Qt application * on Android. * - qtGradlePluginType - whether to build an app or a library * * are defined in gradle.properties file. This file is * updated by QtCreator and androiddeployqt tools. * Changing them manually might break the compilation! *******************************************************/ namespace androidPackageName compileSdkVersion androidCompileSdkVersion buildToolsVersion androidBuildToolsVersion ndkVersion androidNdkVersion // Extract native libraries from the APK packagingOptions.jniLibs.useLegacyPackaging true sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = [qtAndroidDir + '/src', 'src', 'java'] aidl.srcDirs = [qtAndroidDir + '/src', 'src', 'aidl'] res.srcDirs = [qtAndroidDir + '/res', 'res'] resources.srcDirs = ['resources'] renderscript.srcDirs = ['src'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } tasks.withType(JavaCompile) { options.incremental = true } compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } lintOptions { abortOnError false } // Do not compress Qt binary resources file aaptOptions { noCompress 'rcc' } defaultConfig { resConfig "en" minSdkVersion qtMinSdkVersion targetSdkVersion qtTargetSdkVersion ndk.abiFilters = qtTargetAbiList.split(",") } }
I then tried to manually set the minimum sdk & target sdk version, etc but this gets overriden with a new value set by Qt Creator during every build. Below is a short snippet of my gradle.properties file. the code commented out is what I manually add but then is overridden by Qt and as you see I have enabled jetifier and the use of AndroidX.
gradle.properties
android.useAndroidX=true android.enableJetifier=true #androidNdkVersion=29.0.13113456 androidNdkVersion=26.1.10909125 #qtMinSdkVersion=29 qtTargetAbiList=arm64-v8a #qtTargetSdkVersion=35 qtMinSdkVersion=28 qtTargetSdkVersion=34
When I open my project in android studio using Debug APK I see that androidx exists however I can't import it in any of my java classes.
I am new to QT and would really appreciate some help. If you need any other information please do let me know. Thanks in advance !
-
-
1.15.0 is the latest stable version, its possible its not part of your repo
I'm using implementation 'androidx.core:core:1.13.1' and classpath 'com.android.tools.build:gradle:8.6.0' and I'm using this repo, specified in the cradle-wrapper.properties:
distributionUrl=https://services.gradle.org/distributions/gradle-8.10-bin.zipI do not use Android auto, so I don't know if these settings would help you ?
-
@J-Hilk Yes, that's what I initially had. These settings weren't working for me this is why I updated the versions thinking that maybe I need a latest version. Are you able to import " import androidx.core.app.NotificationCompat; " into any of your java classes? This is just one of the imports but can be a good example to test.
-
I'm actively using:
import androidx.core.content.FileProvider;
import androidx.core.app.ShareCompat;the the files are there for me :D
-
@J-Hilk Oh that's interesting, may I know If you use Qt 6.8.2 or not? As that's what I am using. It would also be helpful if you could compare the below gradle.properties to yours so that I can try changing the values. android.useAndroidX=true
android.enableJetifier=true
androidBuildToolsVersion=35.0.0
androidCompileSdkVersion=android-35
androidNdkVersion=26.1.10909125
buildDir=build
qt5AndroidDir=/Users/abcd/Qt/6.8.2/android_arm64_v8a/./src/android/java
qtAndroidDir=/Users/abcd/Qt/6.8.2/android_arm64_v8a/./src/android/java
qtGradlePluginType=com.android.application
qtTargetAbiList=arm64-v8a
qtMinSdkVersion=28
qtTargetSdkVersion=34 -
@VicDm
there's nothing special to it, here it is in full. I did remove commented out stuff for readability:buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.6.0' } } repositories { google() mavenCentral() } apply plugin: qtGradlePluginType dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) implementation 'androidx.core:core:1.13.1' } android { /******************************************************* * The following variables: * - androidBuildToolsVersion, * - androidCompileSdkVersion * - qtAndroidDir - holds the path to qt android files * needed to build any Qt application * on Android. * - qtGradlePluginType - whether to build an app or a library * * are defined in gradle.properties file. This file is * updated by QtCreator and androiddeployqt tools. * Changing them manually might break the compilation! *******************************************************/ namespace androidPackageName compileSdkVersion androidCompileSdkVersion buildToolsVersion androidBuildToolsVersion ndkVersion androidNdkVersion // Extract native libraries from the APK packagingOptions.jniLibs.useLegacyPackaging true sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = [qtAndroidDir + '/src', 'src', 'java'] aidl.srcDirs = [qtAndroidDir + '/src', 'src', 'aidl'] res.srcDirs = [qtAndroidDir + '/res', 'res'] resources.srcDirs = ['resources'] renderscript.srcDirs = ['src'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } tasks.withType(JavaCompile) { options.incremental = true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } lintOptions { abortOnError false } // Do not compress Qt binary resources file aaptOptions { noCompress 'rcc' } defaultConfig { resConfig "en" minSdkVersion qtMinSdkVersion targetSdkVersion qtTargetSdkVersion ndk.abiFilters = qtTargetAbiList.split(",") } }
I spend a lot of time the last couple of weeks trying to compile with Qt6. The the .gradle file might be a bit messy.
-
6.8.1
# Project-wide Gradle settings. # For more details on how to configure your build environment visit # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. org.gradle.jvmargs=-Xmx2500m -XX:MaxMetaspaceSize=768m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # Enable building projects in parallel org.gradle.parallel=true # Gradle caching allows reusing the build artifacts from a previous # build with the same inputs. However, over time, the cache size will # grow. Uncomment the following line to enable it. #org.gradle.caching=true #org.gradle.configuration-cache=true # Allow AndroidX usage android.useAndroidX=true
-
@J-Hilk I checked my dependencies in my project and it shows androidx.core. It's actually really difficult to pin point the issue. I tried going through my files and documentation online. I'm not sure, do I need to mention explicitly somewhere about the usage of androidx or do I need some additional statements in my AndroidManifest ?
-
In my case I actually have to reference it in the AndroidManifest
<provider android:name="androidx.core.content.FileProvider" android:authorities="redacted" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/> </provider>
But thats so that the file provider from the androidx library actually gets the needed permissions to function correctly.
Shouldn't matter for compilation
-
@J-Hilk This is what I have in my AndroidManifest.
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.qtprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/qtprovider_paths"/> </provider>
this is newly generated in every build.
qtprovider_paths.xml
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="files_path" path="/"/> </paths>
Is there something missing here?
-
ok, maybe I should have started with this.
Do you provide QtCreator & qmake/cmake - I presume you use QtC - with your own android & gradle settings/files ? And if you think you do, how exactly ?
-
@J-Hilk Yes I use CMake and the android & gradle settings/files are generated by QT on build.
/Users/abcd/Workspace/App/build/Android_Qt_6_8_2_Clang_arm64_v8a-Debug/android-build-LiftboyApp/build.gradle
android_bundle.cmake
if (ANDROID) set(LFSP_ANDROID_DIR ${CMAKE_CURRENT_SOURCE_DIR}/android) option(QT_ANDROID_BUILD_ALL_ABIS ON) set_target_properties(${LFSP_APP_BUNDLE} PROPERTIES QT_ANDROID_PACKAGE_SOURCE_DIR ${LFSP_ANDROID_DIR} RESOURCE ${LFSP_ANDROID_DIR}/AndroidManifest.xml QT_ANDROID_TARGET_SDK_VERSION ${LFSP_ANDROID_TARGET_SDK} QT_ANDROID_VERSION_NAME ${CMAKE_PROJECT_VERSION} QT_ANDROID_VERSION_CODE ${LFSP_ANDROID_VERSION_CODE} QT_ANDROID_BUILD_ALL_ABIS ${LFSP_ANDROID_ALL_ABIS}) message("android: ${LFSP_APP_BUNDLE} v${CMAKE_PROJECT_VERSION}, code ${LFSP_ANDROID_VERSION_CODE}, target-sdk: ${LFSP_ANDROID_TARGET_SDK}, all abis: ${LFSP_ANDROID_ALL_ABIS}") endif()
short snippet of CMakeLists.txt
set(LFSP_BASE ${CMAKE_CURRENT_LIST_DIR}/..) set(LFSP_ANDROID_VERSION_CODE 87) set(LFSP_ANDROID_TARGET_SDK 34) set(LFSP_ANDROID_JAVA_VERSION 17) if(CMAKE_BUILD_TYPE STRLESS_EQUAL "Debug") set(LFSP_ANDROID_ALL_ABIS OFF) else() set(LFSP_ANDROID_ALL_ABIS ON) endif() set(LFSP_APP_BUNDLE App CACHE STRING "" FORCE) include(android_bundle)
-
ok, my cmake experience is nearly non existent, but it seems to be fine. Mmh.
I have no idea why it doesn't work for you.