nitro icon indicating copy to clipboard operation
nitro copied to clipboard

fix: CMake not finding headers during Gradle Sync on a fresh install

Open chrispader opened this issue 9 months ago • 12 comments

Adds android/build/headers to the published files list in package.json to fix an error during Gradle Sync -> CMake.

chrispader avatar Mar 06 '25 12:03 chrispader

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
nitro-docs ⬜️ Skipped (Inspect) Mar 18, 2025 0:06am

vercel[bot] avatar Mar 06 '25 12:03 vercel[bot]

This is generated code by Android/Gradle. That should not be part of the npm package - I think. This should be built by the library consumer.

Do you disagree?

mrousavy avatar Mar 06 '25 14:03 mrousavy

This is generated code by Android/Gradle. That should not be part of the npm package - I think. This should be built by the library consumer.

Do you disagree?

I agree that this is generated by Android/Gradle.

In build.gradle we copy headers files from ./android/src/main/cpp and ./cpp/ into the android/build/headers. https://github.com/mrousavy/nitro/blob/ca86d9d296fe2e2fb76c6a4f0087adbb967172fa/packages/react-native-nitro-modules/android/build.gradle#L159-L165

The prefab config in build.gradle currently only allows a single header directory path, so if we want to have the header files split up as is, we'll have to rely on Gradle to copy and add them to the prefab.

This should be built by the library consumer.

The problem is that the shipped prefab is pointing to android/build/headers, therefore it throws an error on Gradle Sync, because it's not in the published files.

I agree this isn't 100% clean so we can either:

  • copy over the header files somewhere else
  • go with this approach (and optionally add the gradle sync command to the prepublish/prepare script?)

chrispader avatar Mar 06 '25 14:03 chrispader

Can't we use the headers from their original source (node_modules/react-native-nitro-modules/cpp/** and ........./android/**)?

That way we don't need to copy headers at all?

mrousavy avatar Mar 06 '25 14:03 mrousavy

Also - which issue does this exactly fix?

mrousavy avatar Mar 06 '25 14:03 mrousavy

Can't we use the headers from their original source (node_modules/react-native-nitro-modules/cpp/** and ........./android/**)?

As described above, the prefab.headers config in build.gradle only supports a single directory, not multiple.

chrispader avatar Mar 06 '25 15:03 chrispader

Also - which issue does this exactly fix?

This one: (only on a clean install before the app was built)

Screenshot 2025-03-06 at 16 11 25

You won't see this in the Nitro example, because react-native-nitro-modules is linked locally there. This is also reproducible in a fresh RN78 app (npx @react-native-community/cli@latest init) with a local Nitro module (npx nitro-codegen init)

Full log

chrispader avatar Mar 06 '25 15:03 chrispader

ok, makes sense. then let's add a prepareHeaders command to the prepublish npm script

mrousavy avatar Mar 06 '25 15:03 mrousavy

@mrousavy updated the scripts

chrispader avatar Mar 06 '25 16:03 chrispader

can we not just make the prepareHeaders task dependent on a gradle sync? instead of a build

mrousavy avatar Mar 18 '25 10:03 mrousavy

can we not just make the prepareHeaders task dependent on a gradle sync? instead of a build

@mrousavy Unfortunately you cannot attach any task/script during or before Gradle Sync (in the configure phase). Just tested it out and you cannot run any methods like from(), into(), etc. there.. I also tried copying the files in CMakeLists.txt, but that won't work either.

The problem is really, that the prefab expects the headers to be in that location, but the are not (yet) at the time of Gradle Sync. The prefab headers setting expects a single directory, so we cannot just replace it with the original header directories either.

I think running the copy script on prepublishOnly will be our only possibility, if we want to ensure, that the header files are present during NPM publish.

chrispader avatar Mar 18 '25 12:03 chrispader

@mrousavy how should we proceed with this?

chrispader avatar Apr 01 '25 12:04 chrispader

@mrousavy do you think we should fix this error with this hacky workaround or just ignore the error during Gradle Sync?

Seems there is no update in the Google IssueTracker issue around allowing multiple prefab.header directories to be defined, which would holistically fix this issue

chrispader avatar Aug 04 '25 09:08 chrispader

closing since I don't think we should ship this. thanks tho

mrousavy avatar Aug 14 '25 13:08 mrousavy