react-native-gradle-plugin icon indicating copy to clipboard operation
react-native-gradle-plugin copied to clipboard

unable to locate React Native android sources

Open nokite opened this issue 5 years ago • 11 comments
trafficstars

Edit: I've updated the issue with the latest info.

I'm getting this error when syncing my project on CI (Github Actions), but it builds on my machine:

* Where:
Build file '/home/runner/work/My-Project/My-Project/node_modules/@react-native-firebase/app/android/build.gradle' line: 91

* What went wrong:
A problem occurred evaluating project ':react-native-firebase_app'.
> :react-native-firebase_app: unable to locate React Native android sources. Ensure you have you installed React Native as a dependency in your project and try again.

Caused by:

at io.invertase.gradle.build.ReactNativeModule.applyReactNativeDependency(ReactNativeModule.groovy:123)
at build_8wkkl947llapcsjzwcqohvh32.run(/home/runner/work/My-Project/My-Project/node_modules/@react-native-firebase/app/android/build.gradle:91)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:91)

On my machine it prints the reactNativeAndroidDir, but on the CI it crashes instead, when printing the configuration values:

> Configure project :react-native-firebase_app
...
:react-native-firebase_app:reactNativeAndroidDir /Users/user/projects/My-Project/node_modules/react-native/android

Context: I'm in the process of migrating from Fabric to Firebase Crashlytics SDK, so from react-native-fabric to @react-native-firebase (I'm not using any other firebase modules). Most of my project is native, native Crashlytics works. I upgraded React Native to 0.61.5 a few months ago.

I've tried cleaning the build, deleting node_modules, .gradle folder etc. (I also searched for a solution online and found a similar issue from the end of 2018 that was supposedly fixed)

Dependencies:

    "react-native": "0.61.5"
    "@react-native-firebase/app": "^8.4.5",
    "@react-native-firebase/crashlytics": "^8.4.9",
native:
    googleServicesPlugin = "4.3.3"
    crashlyticsPlugin = "2.3.0"
    firebase = "17.5.0"
    crashlytics = "17.2.2"
    gradle-6.5.1-all
    com.android.tools.build:gradle:4.0.1

My project structure is flatter than the default react-native one, as the project is primarily native:

root
- node_modules
- app
- settings.gradle
(...)

I've followed the guide for migrating from v5 to v6 (https://rnfirebase.io/migrating-to-v6). But I had to make one change - I'm manually registering the 2 ReactPackage's, as I get an error if I don't:

                .addPackage(new MainReactPackage())
                .addPackage(new RNRecyclerviewListPackage())
                .addPackage(new MyAppReactPackage())
                .addPackage(new ReactNativeFirebaseAppPackage())
                .addPackage(new ReactNativeFirebaseCrashlyticsPackage())

I also declare these dependencies explicitly in my build.gradle (the project is primarily native):

implementation("com.google.firebase:firebase-crashlytics:${Versions.crashlytics}") { version { strictly Versions.crashlytics } }
constraints {implementation("com.google.firebase:firebase-crashlytics-ndk") {version {strictly Versions.crashlytics } } }

react-native.config.js

const android = require('@react-native-community/cli-platform-android');

module.exports = {
  project: {
    android: {
      sourceDir: '.',
    },
  },
  platforms: {
    android: {
      linkConfig: android.linkConfig,
      projectConfig: android.projectConfig,
      dependencyConfig: android.dependencyConfig,
    },
  },
};

nokite avatar Feb 12 '20 16:02 nokite

@nokite did you solve this problem?

vladkosinov avatar Jul 15 '20 13:07 vladkosinov

@vladkosinov No. If you don't see what the problem is, I suggest closing this issue.

What happened: I realized that @react-native-firebase used the Fabric SDK at the time. My assumption was that it was using the Firebase Crashlytics SDK, which stands to reason, but alas... So I abandoned the migration for a few months. Therefore, I haven't investigated this issue further.

Btw, recently I saw that they've migrated to the new SDK, so I'll give it another shot. https://github.com/invertase/react-native-firebase/blob/master/packages/crashlytics/CHANGELOG.md

nokite avatar Jul 15 '20 13:07 nokite

I have the same issue. We are in a mono repo, probably that's why. The ReactNative.module.applyReactNativeDependency("api") should probably search on different level for node_modules/react-native. Any ideas how to change it or have a workaround?

paladin952 avatar Sep 22 '20 08:09 paladin952

@paladin952 in my experience, very very few people have exactly the same issue. Please post a new issue with details on your case plus the text of the error received, plus a simple reproduction

mikehardy avatar Sep 22 '20 13:09 mikehardy

Edit: this comment is not exactly true 🙂 I can't reproduce this anymore, the issue can be closed from my point of view.

nokite avatar Sep 27 '20 13:09 nokite

@mikehardy does this look a bug, or am I doing something silly? Thanks for having a look in advance! P.S. I've reopened the issue and updated the info. It looks like the same issue, I shouldn't have closed it.

nokite avatar Oct 02 '20 10:10 nokite

I also tried overriding the reactNativeAndroidDir but I didn't succeed. (and maybe that's just a symptom anyway, not core problem) The value I set here does not override the default one, it just gets appended to it. In project-level build.gradle:

ext {
set('react-native', [
        options: [
                reactNativeAndroidDir: "..."
        ]
])}

nokite avatar Oct 02 '20 10:10 nokite

I apologize for not having any idea why this is happening. Your project layout structure being non-react-native-standard is likely the culprit but why? All I can offer is that the module is 100% open source and the groovy files aren't super complicated, plus there is console output available as a language feature in groovy, perhaps just tracing through quickly with some console output in the groovy as it searches for and makes assumptions about the react native dir will lead to the root cause quickly? That's how I debugged a problem I was having in here before #1

mikehardy avatar Oct 02 '20 14:10 mikehardy

I've solved my issue by setting a custom reactNativeAndroidDir. Still not completely sure why it behaved differently on CI (both Linux and Mac OS) than on my machine.

Top-level build.gradle:

project.ext {
    set('react-native', [
            options: [
                    reactNativeAndroidDir: "/../../../../node_modules/react-native/android",
            ]
    ])
}

In addition, I've created a PR that possibly improves the search for the react-native dir: https://github.com/invertase/react-native-gradle-plugin/pull/3

Some thoughts

In my case the search seems to start from /node_modules/@react-native-firebase/app/android/ So the number of ../../ when specifying reactNativeAndroidDir doesn't seem to depend on my project's structure, but on the plugins in node_modules.

So if I understand it correctly, I may have to specify a different value for reactNativeAndroidDir for different plugins, if multiple ones try to resolve the react-native dir automatically and fail (and need it explicitly specified the way I have now). It's only possible to specify one value now, so I suppose the workaround is not scalable.

(Of course, this "workaround" was not designed for my use case in the first place, but for cases where you have a fork of react-native in a custom directory.)

nokite avatar Oct 09 '20 12:10 nokite

sorry for late reply, in my case we have a hibrid app and the structure is different than normal RN apps. We don't have "root/ios" or "root/android". Ios and Android projects start from "root" and that seems to be the issue.

I managed to make it work for my project here https://github.com/invertase/react-native-firebase/pull/4375/files

@nokite 's PR seems to solve this

paladin952 avatar Oct 09 '20 12:10 paladin952

Really sorry this sat forever, we just merged the PR, I'll leave this open until there is a release of the main branch, should be 1.5, not sure when release will happen I don't know how the release machinery works here

mikehardy avatar Jan 09 '21 18:01 mikehardy