user2473015
user2473015

Reputation: 1442

React Native Android module conflicting with Andorid Studio and React native builder

I have a React Native Android Native module which implements ActivityEventListener

In Android studio it generates a methods:

onActivityResult(int,int,IntentData)

But when I import it to react native and try to run android it asks for a onNewIntent(Intent intent) needs to implemented and

CLass is not abstract and does not override abstract method onActivityResult(Activity,int,int,Intent) in ActivityEventListener

How to get Android Studio and React Native to respect same requirements?

System:
OS: macOS 10.14.5
CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Memory: 33.43 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.15.3 - /usr/local/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.11.3 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
  Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
Android SDK:
  API Levels: 18, 21, 22, 23, 24, 25, 26, 27, 28
  Build Tools: 21.1.2, 23.0.1, 24.0.1, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.0, 28.0.1, 28.0.2, 28.0.3
  Android NDK: 18.1.5063045
IDEs:
Android Studio: 3.3 AI-182.5107.16.33.5314842
Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.6 => 16.8.6 
react-native: 0.60.5 => 0.60.5

My Dependancy for React Native inside build.gradle is like

dependencies {
    implementation 'com.facebook.react:react-native:+'
}

Native module is scaffolded using react-native-create-library which was in react native documentation at that time. Seems it got updated to new recommended library.

Upvotes: 3

Views: 1030

Answers (1)

JensV
JensV

Reputation: 4544

It seems the problem is that gradle isn't pulling the react native dependency from the node_modules folder. If one takes a look at the /android/build.gradle file, you can see that it registers url("$rootDir/../node_modules/react-native/android") as a maven repository.

Gradle should be receiving a react-native library at least with version 0.6+.

Troubleshooting steps:

  • Make sure that the /android/build.gradle looks similar to this one.
    Especially the repositories part.
  • Make sure the node dependencies are up to date with yarn install or npm install (depending on which you use).
  • Try setting the dependency to an exact version number in android/app/build.gradle (See this link for reference). The current build seems to be 0.60.5

Other troubleshooting steps

It seems you have the wrong version of react-native in android studio. If react-native is provided via gradle you might need to clean your build environment and see if it reloads the dependencies.

In Android Studio: Build -> Clean Project

You can also check the External Libraries in the Project View in Android Studio to confirm that you are using the latest library.

Also, Android Studio is a bit weird at times, often various problems are solved by File -> Invalidate Caches / Restart -> Invalidate and Restart

To delve a bit deeper, since react-native version 0.33.x it should require the new method signature:

https://github.com/facebook/react-native/commit/fbd2e139103e3d520f6dfc009d6200f8b8168e35#diff-8b63add5f85adc06c226d8a36c4c2f88

This is how the current file in the master branch looks like:

public interface ActivityEventListener {

  /** Called when host (activity/service) receives an {@link Activity#onActivityResult} call. */
  void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data);

  /** Called when a new intent is passed to the activity */
  void onNewIntent(Intent intent);
}

If all else fails, check your gradle dependencies file to make sure you are using the correct version and/or delete the gradle cache from:

  • C:\Users\<user>\.gradle\caches
  • /home/<user>/.gradle/caches

Upvotes: 2

Related Questions