Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidX support #254

Closed
Tamiyadd opened this issue Jun 18, 2019 · 16 comments
Closed

AndroidX support #254

Tamiyadd opened this issue Jun 18, 2019 · 16 comments

Comments

@Tamiyadd
Copy link

Environment

React Native Environment Info:
    System:
      OS: macOS 10.14.5
      CPU: (8) x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
      Memory: 78.93 MB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
      Yarn: 1.16.0 - /usr/local/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/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: 23, 24, 28
        Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.3, 28.0.2, 28.0.3
        System Images: android-26 | Google APIs Intel x86 Atom_64
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5522156
      Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.6 => 16.8.6 
      react-native: 0.59.9 => 0.59.9 

Steps to Reproduce

add
android.useAndroidX=true
android.enableJetifier=true

to gradle.properties

and use

react-native run-android

Expected Behavior

Compile and run app

Actual Behavior

It throws

CalendarEvents.java:14: error: cannot find symbol
import android.support.v4.app.ActivityCompat;
                             ^
  symbol:   class ActivityCompat
  location: package android.support.v4.app

CalendarEvents.java:15: error: package android.support.v4.content does not exist
import android.support.v4.content.ContextCompat;

and other similar errors

@irfanshaikh138
Copy link

irfanshaikh138 commented Jun 18, 2019

I am getting the same error.

Task :react-native-calendar-events:compileDebugJavaWithJavac FAILED
error: cannot find symbol
import android.support.v4.app.ActivityCompat;
^
symbol: class ActivityCompat
location: package android.support.v4.app

error: package android.support.v4.content does not exist
import android.support.v4.content.ContextCompat;

error: cannot find symbol
ActivityCompat.requestPermissions(currentActivity, new String[]{

symbol: variable ActivityCompat
location: class CalendarEvents
error: cannot find symbol
int writePermission = ContextCompat.checkSelfPermission(reactContext, Manifest.permission.WRITE_CALENDAR);

symbol: variable ContextCompat
location: class CalendarEvents

error: cannot find symbol
int readPermission = ContextCompat.checkSelfPermission(reactContext, Manifest.permission.READ_CALENDAR);

symbol: variable ContextCompa

@syntheticencounters
Copy link

Updating the imports in the Java files with the new AndroidX imports resolves the issue.
Here's a link to migrate the old imports to the new ones.
https://developer.android.com/jetpack/androidx/migrate

@roshan-p
Copy link

I am getting the same error.
Any solutions ?

@syntheticencounters
Copy link

I am getting the same error.
Any solutions ?

The AndroidX migration link has the new import names.

android.support.v4.app.ActivityCompat should be replaced with androidx.core.app.ActivityCompat.
android.support does not exist in AndroidX

Repeat for any other missing imports. They're all listed on the migration page.
https://developer.android.com/jetpack/androidx/migrate

@irfanshaikh138
Copy link

irfanshaikh138 commented Jun 20, 2019

Environment

React Native Environment Info:
    System:
      OS: macOS 10.14.5
      CPU: (8) x64 Intel(R) Core(TM) i7-3615QM CPU @ 2.30GHz
      Memory: 78.93 MB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
      Yarn: 1.16.0 - /usr/local/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v8.12.0/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: 23, 24, 28
        Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.3, 28.0.2, 28.0.3
        System Images: android-26 | Google APIs Intel x86 Atom_64
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5522156
      Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.6 => 16.8.6 
      react-native: 0.59.9 => 0.59.9 

Steps to Reproduce

add
android.useAndroidX=true
android.enableJetifier=true

to gradle.properties

and use

react-native run-android

Expected Behavior

Compile and run app

Actual Behavior

It throws

CalendarEvents.java:14: error: cannot find symbol
import android.support.v4.app.ActivityCompat;
                             ^
  symbol:   class ActivityCompat
  location: package android.support.v4.app

CalendarEvents.java:15: error: package android.support.v4.content does not exist
import android.support.v4.content.ContextCompat;

and other similar errors

I have followed the same steps. and got the same error then I did this.

1. Removed these lines

android.useAndroidX=true
android.enableJetifier=true

from gradle.properties

2. Change the version of react-native-device-info from 0.21.5 to 2.1.2.

After changing this I got following error.

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':app:preDebugBuild'.
Android dependency 'com.android.support:support-media-compat' has different version for the compile (27.0.2) and runtime (28.0.0) classpath. You should manually set the same version via DependencyResolution

And then added the following code in android/build.gradle in subprojects section

subprojects { project.configurations.all { resolutionStrategy.eachDependency { details -> if (details.requested.group == 'com.android.support' && !details.requested.name.contains('multidex') ) { details.useVersion "27.0.2" } } }

then it works for me

@shouhuori
Copy link

Use this script migrate to androidx.

@taschik
Copy link

taschik commented Jul 19, 2019

Appreciate you working on this @wmcmahan. But with the release of 0.60 there is a more prominent need for a proper solution. The workaround provided by @irfanshaikh138 didn't really work for my 0.60.3 environment and caused a bunch of Can't find symbol error like this here:

/Users/user/Documents/app/bundle/node_modules/react-native-calendar-events/android/src/main/java/com/calendarevents/CalendarEvents.java:96: error: cannot find symbol
        int readPermission = ContextCompat.checkSelfPermission(reactContext, Manifest.permission.READ_CALENDAR);
                             ^
  symbol:   variable ContextCompat
  location: class CalendarEvents

Is there a proper way to get this going? Thanks a lot!

@yfuks
Copy link
Contributor

yfuks commented Jul 26, 2019

I just created a PR #263

@camposbrunocampos
Copy link

camposbrunocampos commented Oct 9, 2019

It looks like that for some reason react-native calendars is the PackageList file used in Application file here
MainApplication.java

 @Override
    protected List<ReactPackage> getPackages() {
      List<ReactPackage> packages = new PackageList(this).getPackages();
      packages.addAll(Arrays.asList(
              new SharedPreferencesBridgePackage(),
              new AdobeAnalyticsPackage(),
              new NavigateToSettingsPackage(),
              new ForeseePackage(),
              new GeofenceBridgePackage()
      ));
      return packages;
    }

PackageList.java

public ArrayList<ReactPackage> getPackages() {
    return new ArrayList<>(Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new AsyncStoragePackage(),
      new CameraRollPackage(),
      new GeolocationPackage(),
      new NetInfoPackage(),
      new LottiePackage(),
      new CalendarEventsPackage(), <----- HERE!
      new RNGestureHandlerPackage(),
      new KeychainPackage(),
      new ReactNativePushNotificationPackage(),
      new SvgPackage(),
      new FingerprintAuthPackage(),
      new VectorIconsPackage(),
      new RNVersionNumberPackage(),
      new ReactVideoPackage(),
      new RNViewShotPackage(),
      new RNCWebViewPackage()
    ));
  }

This means that altough react-native-calendar-events is not in react-native-community, it is being imported as default for rn version > 0.61

This means that there is no need for all setup of this library in MainActivity and MainApplication, setting.gradle and gradle files. Which would remove these error warnings.

@ldco2016
Copy link

ldco2016 commented Oct 23, 2019

@camposbrunocampos , thanks for clarifying this. As the promise on my RNCalendarEvents.saveEvent(title, settings) is not resolving. I thought it could be a version issue, but same thing in the legacy version.

So perhaps I thought it was because I needed to run react-native link, but if I understand this correctly, with version RN 60ish, I should not have to run a react-native link, nor add anything to MainActivity or MainApplication correct? The linking happens automatically behind the scenes?

I am aware that a lot of third-party libraries are not updating their documentation now that RN 60ish does automatic linking behind the scenes, so its a confusing time to be using so many third-party packages.

@ldco2016
Copy link

ldco2016 commented Nov 7, 2019

@roshan-p , your issue can be resolved by running npm run jetify.

Anyone have a working lib on RN 60+ for Android? Please help. Mine works on iOS but not on Android side.

export async function createCalendarEvent(event) {
  const store = await RNCalendarEvents.authorizeEventStore();
  console.log(store);
  if (store === "authorized") {
    addToCalendar(event);
  } else {
    RNCalendarEvents.authorizationStatus()
      .then(auth => {
        // handle status
        if (auth === "authorized") {
          addToCalendar(event);
        }
      })
      .catch(() => {
        alert("This app needs calendar access");
      });
  }
}

async function addToCalendar(event) {
  try {
    const startDate =
      Platform.OS === "ios"
        ? format(parse(event.StartDateLocal))
        : parse(event.StartDateLocal);
    const endDate =
      Platform.OS === "ios"
        ? format(parse(event.EndDateLocal))
        : parse(event.EndDateLocal);
    const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);

    const calendarEvent = allEvents.find(e => e.title === event.Title);
    if (calendarEvent) {
      alert("You have already added this event to your calendar.");
    } else {
      const title = event.Title;

      const {
        Location: {
          AddressLine1: address,
          City: city,
          StateAbbreviation: state,
          PostalCode: zip
        }
      } = event;

      const location = `${address}, ${city}, ${state}, ${zip}`;

      const settings = {
        location,
        startDate,
        endDate
      };
      RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });
    }
  } catch (e) {
    alert(e.message);
  }
}

For iOS I get Event Saved, but for Android I get Oops! Something has gone wrong. rejectionReason by the way, is calendar is not being set.

@taschik
Copy link

taschik commented Nov 7, 2019

We experienced that on Android you need to find the primary calendar before you can safe in it:

if (Platform.OS === 'android') {
        const calendars = await RNCalendarEvents.findCalendars()
        const primaryCalendar = calendars.find(cal => cal.isPrimary)
        idInfo = { calendarId: get(primaryCalendar, 'id') }
      }

@ldco2016
Copy link

ldco2016 commented Nov 7, 2019

@taschik , I think you are on to a solution here, but where are you getting the variable get() from?

This is how I have it setup and what I have tried:

async function addToCalendar(event) {
  try {
    if (Platform.OS === "android") {
      const calendars = await RNCalendarEvents.findCalendars();
      const primaryCalendar = calendars.find(cal => cal.isPrimary);
      // idInfo = { calendarId: get(primaryCalendar, "id") };
    }
    const startDate =
      Platform.OS === "ios"
        ? format(parse(event.StartDateLocal))
        : parse(event.StartDateLocal);
    const endDate =
      Platform.OS === "ios"
        ? format(parse(event.EndDateLocal))
        : parse(event.EndDateLocal);
    const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);

    const calendarEvent = allEvents.find(e => e.title === event.Title);
    if (calendarEvent) {
      alert("You have already added this event to your calendar.");
    } else {
      const title = event.Title;

      const {
        Location: {
          AddressLine1: address,
          City: city,
          StateAbbreviation: state,
          PostalCode: zip
        }
      } = event;

      const location = `${address}, ${city}, ${state}, ${zip}`;

      const settings = {
        location,
        startDate,
        endDate
      };
      RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });
    }
  } catch (e) {
    alert(e.message);
  }
}

@taschik
Copy link

taschik commented Nov 8, 2019

Oh that’s just loadash that we use.

@ldco2016
Copy link

ldco2016 commented Nov 8, 2019

@taschik , oh, shouldn't it then be _.get()?
Also, I went ahead and added the lodash library and _.get() and I am still getting Alert: Can't find variable: get.

UPDATE

After adding your code snippet, it seemed like my application was starting to work, but then it went back to giving me the Oops something went wrong alert:

RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(rejectionReason => {
          console.log(rejectionReason);
          alert("Oops! Something has gone wrong.");
        });

Could I have missed a step here:

if (Platform.OS === "android") {
      const calendars = await RNCalendarEvents.findCalendars();
      const primaryCalendar = calendars.find(cal => cal.isPrimary);
      idInfo = { calendarId: _.get(primaryCalendar, "id") };
    }

What did you do with the idInfo afterwards?

@MoOx
Copy link
Collaborator

MoOx commented Apr 24, 2020

Android X support has been added in #263

@MoOx MoOx closed this as completed Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants