Capacitor can't build due to AndroidX conflicts, at a total loss on how to fix

I’ve been banging my head against this for days now. I have an Ioinc v5 app that won’t load in android studio. I think the issue is related to the AndroidX stuff that cropped up last year but none of the solutions help me. I’ve deleted and rerun the steps to generate the Android build several times and I’m on my latest version and again stuck at this error in Android Studio:

ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
	is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
	Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-43:19 to override.

I’ve tried doing that but then the AStudio complains with:

The prefix "tools" for attribute "tools:replace" associated with an element type "application" is not bound.

I have a suspicion that the AdMobFree plugin might be the source of the issue as that is the only thing that hooks in to the Firebase/Google Play world and it built fine a few weeks ago before I included that.

I’m at a loss where to go next, I’ve developed for iOS in ObjC and Swift and WebDev is my day job but I’m at a total loss with Android.

I’m building with Capacitor. This is my package.json file below. Please help

{
  "name": "CalWOD",
  "version": "0.0.1",
  "author": "Simon Barker",
  "homepage": "",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/router": "~8.2.14",
    "@capacitor/android": "^1.5.0",
    "@capacitor/core": "1.5.1",
    "@capacitor/ios": "^1.5.0",
    "@ionic-native/admob-free": "^5.22.0",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/media": "^5.22.0",
    "@ionic-native/native-audio": "^5.22.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^5.0.4",
    "cordova-admob-sdk": "^0.24.1",
    "cordova-plugin-admob-free": "^0.27.0",
    "cordova-plugin-androidx": "^1.0.2",
    "cordova-plugin-file": "^6.0.2",
    "cordova-plugin-media": "^5.0.3",
    "cordova-plugin-nativeaudio": "^3.0.9",
    "cordova-promise-polyfill": "0.0.2",
    "core-js": "^2.5.4",
    "moment": "^2.24.0",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.20",
    "@angular/cli": "~8.3.23",
    "@angular/compiler": "~8.2.14",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@capacitor/cli": "1.5.1",
    "@ionic/angular-toolkit": "^2.2.0",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-ionic-webview": "^4.1.3",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "jetifier": "^1.6.5",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "AirBike and Rower workouts to build your cardio engine.",
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-media": {},
      "cordova-plugin-admob-free": {}
    },
    "platforms": []
  }
}

If you need AndroidX support, checkout capacitor’s beta release which adds support for this.

Thank you will try that, I assume just use that tag (2.0.0-beta.0 )in the package.json?

To be honest I don’t even know that I need AndroidX support, I included AdMobFree and that seems to require it? What surprised me was that a plugin listed on the main list doesn’t seem to work? I’ve put a question in the Slack but I feel like I’ve misunderstood something fundamental about how all this works as everything was perfect on the iOS side and the Android stuff has been a right old palava

So I changed my package.json capacitor dependencies to:

    "@capacitor/android": "2.0.0-beta.0",
    "@capacitor/core": "2.0.0-beta.0",
    "@capacitor/ios": "2.0.0-beta.0",

everything else kept the same.

I then ran an npm i

and have some peer dependency warnings:

npm WARN @capacitor/android@2.0.0-beta.0 requires a peer of @capacitor/core@^1.5.0 but none is installed. You must install peer dependencies yourself.
npm WARN @capacitor/ios@2.0.0-beta.0 requires a peer of @capacitor/core@^1.5.0 but none is installed. You must install peer dependencies yourself.
npm WARN karma-jasmine-html-reporter@1.5.2 requires a peer of jasmine-core@>=3.5 but none is installed. You must install peer dependencies yourself.

The first two are new.
I then ran

ionic capacitor sync android
ionic capacitor run android

and then I get the following error in AStudio:

error: cannot find symbol class NonNull

in capacitor-cordova-android-plugins/src/main/java/name/ratson/cordova/admob/Admob.java

so I’m assuming the AndroidX issues are out of the way and this is now a plugin issue?

Direction would be really appreciated

Reviewed the cordova-plugin-admob-free repo, it doesn’t seem to require android x support. I’d highly suggest removing cordova-plugin-androidx as it will probably cause issues.

If you’re already using capacitor, it honestly doesn’t make sense to mix the cordova plugins in there, especially since there is a capacitor ad mob plugin

As for you error, I honestly am not sure, since I do not have your project, I cannot look

Thanks, will swap to that plugin and see where I get to - i dread to think the mess I’ve made of my repo over the last 2 days over this.

Have removed cordova-plugin-androidx, I added that as part of trying to solve the issue of it not compiling. I can’t think what else would have caused the AndroidX issue to appear if not the AdMobFree Plugin.

For the safest results, I’d highly suggest making a new git branch and testing things out first before further messing with a stable branch.

1 Like

For those who pass this in the future with similar issues it seems that I was battling two issues.

AdMobFree was trying to use a class (I think, my Java is non existent) called NonNull that the system wasn’t happy with.

The other was indeed related to AndroidX and I think was related to the FileProvider/Storage API. Removing AdMobFree isolated that issue. Using beta version 2 as suggested above and then including

android.useAndroidX=true
android.enableJetifier=true

in my gradle.properties got the app to finally run in an emulator.

I can’t believe how much hard this was to get running on Android that iOS, moral of the story here is that every time you add a plugin test on both platforms, don’t assume like I did that it works on one it will work on both.

Now I just need to workout why NativeAudio isn’t playing any sound files on Android.

Thanks for the help Mike

@mhartington I’ve implemented the admob plugin you suggested, no problems on iOS but again won’t compile on Android.

I’m getting a handful of errors out of that AdMob plugin:

error: package android.support.design.widget does not exist

and

error: package CoordinatorLayout does not exist

I’m using the v2 beta build, I have the following lines set in my gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

which was needed to get it to build when I swapped to V2 due to the storage plugin appearing to rely on AndroidX.

any suggestions on where to go next? this is feeling like a real uphill struggle with Android

adding this

implementation 'com.android.support:design:28.0.0'

to the app/build.gradle files hasn’t helped and that seems to be the main suggestion from the internet

This ended up being a total pain but with the help of someone looking at the Capacitor AdMob plugin GitHub i’ve got it compiling and running finally.

I really can’t believe how hard it was to get this working on Android, iOS could not have been easier.

For people facing problems on the beta migration, read the migration guide
https://capacitor.ionicframework.com/docs/android/updating

TLDR: use android studio android x migration tool and jefitier to patch plugins that have not been updated.

2 Likes

hello Tanakasan, did you resolve the capacitor 2.0 migration, i though in made the migration an start all this issues with de java compilation, i put the capacitor/android back to 1.5.1 i was fear to loose the code. what did you do. if i dont se that the beta is compleated relased i dont gonna update. what do you think

yes all fine, however it is now out of beta so you can just change your package json to 2.0.0. If you are concerned about migration issues then create a new git branch in you project, make the changes in there and once you are happy all is fine you can then merge that branch into your master branch

ok, thanks for ypur help

I am facing the exact same error when using cordova-plugin-firebase-analytics with Ionic 5 and Capacitor.

I would suggest working through the help I was given at the above link, it won’t exactly match your issue but the pattern

npm install jetifier
npx jetify
npx cap sync android

3 Likes

@jcesarmobile this fix is working for me but on github CI/CD build is fail, could you please give me some idea.
I’m create hook for replace androidx path. file run successfully but still build is not working.

if (!exists(FILE_OPENER_PATH, false)) {
console.error(Failed to locate ${FILE_OPENER_PATH});
return;
} else {
var fileContent = fs.readFileSync(FILE_OPENER_PATH, { encoding: “utf8” });
var newContent = fileContent.replace(FILE_PROVIDER, X_FILE_PROVIDER);
fs.writeFileSync(FILE_OPENER_PATH, newContent);
}