Could not resolve project :capacitor-android

Hi, i want to include capacitor in an nx Monorepo. Frontend is Nextjs.
In apps/mobile is the project i want to create a mobile app with.

The project build, with the index.html is built in dist/apps/mobile, while the android folder is located in apps/mobile. I don’t know if this may be relevant for solving the problem.

I can open the app in Android studio with nx run mobile:open:android and it builts there also successfully. But when i want to run the app in Android studio i get following error:

Could not determine the dependencies of task ‘:app:compileDebugJavaWithJavac’.

Could not resolve all task dependencies for configuration ‘:app:debugCompileClasspath’.
Could not resolve project :capacitor-android.
Required by:
project :app
No matching configuration of project :capacitor-android was found. The consumer was configured to find a component for use during compile-time, preferably optimized for Android, as well as attribute ‘com.android.build.api.attributes.BuildTypeAttr’ with value ‘debug’, attribute ‘com.android.build.api.attributes.AgpVersionAttr’ with value ‘8.0.2’ but:
- None of the consumable configurations have attributes.

I don’t know which exact configuration he means and why he does not find it. Maybe someone can give me a hint what i should try next

1 Like

I semi-solved the problem.
I found that in the capacitor.settings.gradle within the android project, the projectDir pointed to nothing.

capacitor.settings.gradle:
include ‘:capacitor-android’
project(‘:capacitor-android’).projectDir = new File(‘…/node_modules/@capacitor/android/capacitor’)

i changed the file in Android Studio to correctly point to the node_modules folder:

edited capacitor.settings.gradle:
include ‘:capacitor-android’
project(‘:capacitor-android’).projectDir = new File(‘…/…/…/node_modules/@capacitor/android/capacitor’)

The Android project is located in /apps/mobile/android.
Now my problem is, that the file says at the beginning it should not be overwritten, as it will be generated with every capacitor update is run.
So when i run nx run “mobile:sync:android” it generates the first format of the file.
How do I solve this behaviour to generate correctly?

Hello !

I had the same problem after migrating my nx monorepo and capacitor to the latest version (Nx 16 and Capacitor 5).

There is a temporary solution: You can use the --preserveProjectNodeModules=true option to preserve the node_modules folder after the sync and open commands:

  • nx run app-name:sync:android --preserveProjectNodeModules=true
  • nx run app-name:open:android --preserveProjectNodeModules=true

I assume you are using the @nxext/capacitor package, the latest version uses the npm install command at the beginning of nx run app-name:sync:android and nx run app-name:open:android, this will generate the node_modules folder in the application folder, and at the end the command deletes this folder.

I’ve created an issue on the nx-extension GitHub: Capacitor: Bug after sync and open commands · Issue #978 · nxext/nx-extensions · GitHub

1 Like

The --preserveProjectNodeModules=true flag does not work for me. It still changes the path after a sync

In the github issue, somebody give another solution instead of use the command option.
In your root package.json, add this :

*****rest of your root package.json *****
  },
  "workspaces": [
    "./apps/app-name1",
    "./apps/app-name2",
    ...
  ]
}

Now, my app sync and open command work.

Will try this and give feedback then :slight_smile:

It’s been some months now, but as i run again into this problem i managed to detect my issue, so i thought it might be helpful to share.
It looks like i ran “yarn” once within the apps/app-name directory in the nx Directory. This created a yarn.lock file, which i didn’t pay attention to. After deleting the yarn.lock, capacitor started to generate the paths correct. I guess it was aware of the file and assumed that the node_modules folder would be there.

Has anybody found a working solution for this?

I have created another forum post with more details on the same issue I am facing.