Capacitor 4: package android.content does not exist import android.content.Context

I have an angular 13 app and an Ionic 6 app in a monorepo

After migrate from capacitor 3.5.1 to 4.0.1, I got this message when trying to run the application on Android Studio 2021.2.1 Patch 1

…\getcapacitor\AndroidProtocolHandler.java:7: error: package android.content does not exist import android.content.Context;

this is a screenshot capacitor and ionic entries on monorepo’s package.json

The process I follow to test the app is this:

  1. remove node_modules & package-lock.json
  2. npm i
  3. npx build ionic-app
  4. npx jetify
  5. npx cap sync android
  6. npx cap open android

I have already tried this:
deleting android project and adding it again,
manual migration modifying package.lock
migration using ionic plugin “Ionic v1.9.4” through recomendations section

How can I fix it?

that happens when some of your plugins are still using an old version of gradle/gradle plugin, or you didn’t update gradle/gradle plugin in your app.
How did you migrate to capacitor 4? manually? vs extension? migrate command?
what’s your capacitor plugins list?

I’m running into this exact same issue. The errors are happening in the capacitor-android module itself. I upgraded “manually”, by updating all @capacitor-related dependencies.

For me, the issue was that I overlooked the @capacitor/cli dependency. After I updated that dependency to ~4.2.0, and then deleted the /android folder (rm -rf android), and re-added it (npx cap add android) it works!

Current capacitor dependencies for my project:

"dependencies": {
    //...
    "@capacitor/android": "~4.2.0",
    "@capacitor/app": "~4.0.1",
    "@capacitor/browser": "~4.0.1",
    "@capacitor/core": "~4.2.0",
    "@capacitor/haptics": "~4.0.1",
    "@capacitor/ios": "~4.2.0",
    "@capacitor/keyboard": "~4.0.1",
    "@capacitor/status-bar": "~4.0.1"
   // ...
},
  "devDependencies": {
       // ...
    "@capacitor/cli": "4.2.0",
    "@ionic/angular-toolkit": "^7.0.0",
    "@ionic/cli": "^6.20.1",
}

We had the same problem. Upgraded to cap 4 via the migration tool and everything went smoothly for ios. on android that’s where it started to blow up.

We are still using cordova plugins and the majority of them need to be updated to support the new stuff. Checked a few repos of the problematic plugins and they are all out of date (3+ years old). Some have fixes from members but the plugin developers themselves have not merged those fixes or don’t intend to.

So we have 2 options:

  1. Remove those cordova plugins and switch over to the capacitor version which would take some time.

  2. Hold off on upgrading to cap 4 until 2 weeks before mandatory update.

We decided to go with option 2. There is still 2 whole months to give the plugin owners time to update.

In my case I opened Android Studio and clicked on the “Android gradle plugin can be upgraded” notification to solve the issue