Matteo Mazzarolo
Matteo Mazzarolo

Reputation: 4560

Cannot find symbol getCurrentActivity when building an Android module for React-Native

First of all, here is a test repo to replicate my issue

Hello there, I'm trying to create a simple Android module for React-Native and I'm having some trouble getting the current Activity from the Java code.

This is not the first native module I worked on, but I never had to obtain an Activity reference since today.

The interested module is a bridge to the Estimote SDK, but this is not relevant to the issue.
The module in Android studio works perfectly, but when I try to build it with react-native run android I get the following error:

:react-native-estimote-android:compileReleaseJavaWithJavac
/Users/matteo/dev/react-native-example/react-native-estimote-android/android/src/main/java/com/mmazzarolo/estimoteandroid/EstimoteAndroidModule.java:64: error: cannot find symbol
        Activity currentActivity = this.getCurrentActivity();
                                       ^
  symbol: method getCurrentActivity()
1 error
:react-native-estimote-android:compileReleaseJavaWithJavac FAILED

FAILURE: Build failed with an exception.

From my understanding extending ReactContextBaseJavaModule should be enough to grant me the usage of this.getCurrentActivity() (and Android studio agrees with me).

This is what I already tried to do:
- Implementing ActivityEventListener: same cannot find symbol error when I try to import ActivityEventListener;
- android/.gradlew clean
- watchman watch-del-all && rm -rf node_modules/ && npm cache clean && npm prune && npm i
- Tried the same repo on two different computers...

Any hints? Thank you in advance.

Upvotes: 3

Views: 2918

Answers (2)

Jickson
Jickson

Reputation: 5193

the issue was with the version of react-native in module 'react-native-estimote-android'

Just change compile 'com.facebook.react:react-native:0.12.+' to compile 'com.facebook.react:react-native:+'

This will resolve the cannot find getCurrentActivity symbol.

getCurrentActivity method is not available in react-native 0.12 version. Thats why it was throwing error.

Upvotes: 5

user6380431
user6380431

Reputation: 134

 @ReactMethod
public void start(final Callback callback) {
    Activity currentActivity = getCurrentActivity();
    this.mBeaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            mBeaconManager.startRanging(region);
            callback.invoke();
        }
    });
}

Remove this from start method, if you do this.getCurrentActivity() it will referr to EstimoteAndroidModule context not to the app ReactContextBaseJavaModule context

Upvotes: 1

Related Questions