package androidx.core.content does not exist

Viewed 1303

I am trying to run Ionic 5 app on android with the below command

ionic cap run android -l --external --verbose

but getting these errors

> Task :capacitor-cordova-android-plugins:compileDebugJavaWithJavac FAILED
[capacitor]         C:\xxx\android\capacitor-cordova-android-plugins\src\main\java\de\appplant\cordova\emailcomposer\Provider.java:22: error: package androidx.core.content does not exist
[capacitor]         import androidx.core.content.FileProvider;
[capacitor]         ^
[capacitor]         C:\xxx\android\capacitor-cordova-android-plugins\src\main\java\de\appplant\cordova\emailcomposer\Provider.java:24: error: cannot find symbol
[capacitor]         public class Provider extends FileProvider {
[capacitor]         ^
[capacitor]         symbol: class FileProvider
[capacitor]         C:\xxx\android\capacitor-cordova-android-plugins\src\main\java\com\pdf\generator\PDFGenerator.java:68: error: cannot access androidx.appcompat.app.AppCompatActivity
[capacitor]         final Context ctx = this.cordova.getActivity().getApplicationContext();
[capacitor]         ^
[capacitor]         class file for androidx.appcompat.app.AppCompatActivity not found
[capacitor]         C:\xxx\android\capacitor-cordova-android-plugins\src\main\java\de\appplant\cordova\emailcomposer\EmailComposer.java:109: error: incompatible types: androidx.appcompat.app.AppCompatActivity cannot be converted 
to android.content.Context
[capacitor]         return cordova.getActivity();
[capacitor]         ^
[capacitor]         C:\xxx\android\capacitor-cordova-android-plugins\src\main\java\de\appplant\cordova\emailcomposer\AssetUtil.java:276: error: cannot find symbol
[capacitor]         return Provider.getUriForFile(ctx, authority, file);
[capacitor]         ^
[capacitor]         symbol:   method getUriForFile(android.content.Context,java.lang.String,java.io.File)
[capacitor]         location: class de.appplant.cordova.emailcomposer.Provider
[capacitor]         Note: Some input files use or override a deprecated API.
[capacitor]         Note: Recompile with -Xlint:deprecation for details.
[capacitor]         5 errors
[capacitor]
[capacitor]         > Task :app:mergeDebugJavaResource
[capacitor]         > Task :app:processDebugResources
[capacitor]
[capacitor]         FAILURE: Build failed with an exception.
[capacitor]
[capacitor]         * What went wrong:
[capacitor]         Execution failed for task ':capacitor-cordova-android-plugins:compileDebugJavaWithJavac'.
[capacitor]         > Compilation failed; see the compiler error output for details.
[capacitor]
[capacitor]         * Try:
[capacitor]
[capacitor]         Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
[capacitor]         Use '--warning-mode all' to show the individual deprecation warnings.
[capacitor]         See https://docs.gradle.org/7.0/userguide/command_line_interface.html#sec:command_line_warnings
[capacitor]         152 actionable tasks: 42 executed, 110 up-to-date
[capacitor]         Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
[capacitor]
[capacitor]         * Get more help at https://help.gradle.org
[capacitor]
[capacitor]         BUILD FAILED in 45s
[capacitor]
[capacitor]         C:\xxx\android>if "1" == "0" goto mainEnd
[capacitor]
[capacitor]         C:\xxx\android>rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
[capacitor]
[capacitor]         C:\xxx\android>rem the _cmd.exe /c_ return code!
[capacitor]
[capacitor]         C:\xxx\android>if not "" == "" exit 1
[capacitor]
[capacitor]         C:\xxx\android>exit /b 1
[capacitor]
[ERROR] An error occurred while running subprocess capacitor.

        capacitor.cmd run android --no-sync --target ce01182192619a1301 exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.
  ionic:utils-process onBeforeExit handler: 'process.exit' received +0ms
  ionic:utils-process onBeforeExit handler: running 6 functions +0ms
  ionic:lib:hooks Looking for ionic:serve:after npm script. +54s
  ionic:utils-process error while killing process tree for 5752: Error: Command failed: taskkill /pid 5752 /T /F
  ionic:utils-process ERROR: The process "5752" not found.
  ionic:utils-process 
  ionic:utils-process     at ChildProcess.exithandler (child_process.js:308:12)
  ionic:utils-process     at ChildProcess.emit (events.js:315:20)
  ionic:utils-process     at maybeClose (internal/child_process.js:1048:16)
  ionic:utils-process     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
  ionic:utils-process   killed: false,
  ionic:utils-process   code: 128,
  ionic:utils-process   signal: null,
  ionic:utils-process   cmd: 'taskkill /pid 5752 /T /F'

I have been trying to find a solution for the last three days with no luck. I have done following to fix it but its still showing same error

in gradle.properties

android.useAndroidX=true
android.enableJetifier=true

in variables.gradle

 minSdkVersion = 23
    compileSdkVersion = 30
    targetSdkVersion = 26
    androidxActivityVersion = '1.2.0'
    androidxAppCompatVersion = '1.2.0'
    androidxCoordinatorLayoutVersion = '1.1.0'
    androidxCoreVersion = '1.3.2'
    androidxFragmentVersion = '1.3.0'
    junitVersion = '4.13.1'
    androidxJunitVersion = '1.1.2'
    androidxEspressoCoreVersion = '3.3.0'
    cordovaAndroidVersion = '10.1.1'

in build.gradle

repositories {
        google()
        mavenCentral()
    }
... 
... 
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

None of these changes fixes the issue. However, if I open android build in Android Studio with

ionic cap open android

and add library 'Gradle:androidx.core:core:1.3.0@aar' to class path as intellisense suggestion then it works and and I can run the project on device from Android studio. enter image description here But if I make changes and rebuild then it shows same error again.

Environment detail

Ionic:

   Ionic CLI                     : 6.16.3 (C:\Users\xxx\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.6.11
   @angular-devkit/build-angular : 12.0.5
   @angular-devkit/schematics    : 12.0.5
   @angular/cli                  : 12.0.5
   @ionic/angular-toolkit        : 4.0.0

Capacitor:

   Capacitor CLI      : 3.1.1
   @capacitor/android : 3.3.2
   @capacitor/core    : 3.3.2
   @capacitor/ios     : not installed

Utility:

   cordova-res : 0.15.3
   native-run  : 1.4.0

System:

   NodeJS : v14.16.0 (C:\Program Files\nodejs\node.exe)
   npm    : 6.14.11
   OS     : Windows 10

Please suggest any solution for this.

Many Thanks

2 Answers

try these steps:

  1. ionic cordova plugin add cordova-plugin-androidx
  2. ionic cordova plugin add cordova-plugin-androidx-adapter

if still not working try step:

npm install jetifier    
npx jetifier    
npx cap sync android

If you are using Capacitor, you cannot "add plugin" like in Cordova...

You have to install it from npm or yarn.

npm i cordova-plugin-androidx
npm i cordova-plugin-androidx-adapter....

Or try to update to the latest Capacitor, Ionic and Android Studio.

Related