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
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?
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.
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.