clipboard icon indicating copy to clipboard operation
clipboard copied to clipboard

Task :react-native-community_clipboard:compileDebugJavaWithJavac FAILED

Open Ashik55 opened this issue 1 year ago • 15 comments

Environment

Macbook M1

Platforms

Android

Versions

"react": "18.2.0", "react-native": "0.73.2", "@react-native-community/clipboard": "^1.5.1",

Description

After installing @react-native-community/clipboard Unable to run the project. Error : ClipboardModule.java:14: error: cannot find symbol import com.facebook.react.bridge.ContextBaseJavaModule; ^ symbol: class ContextBaseJavaModule location: package com.facebook.react.bridge

Ashik55 avatar Jan 15 '24 17:01 Ashik55

Any Fix for this??

RanvijayChouhan12 avatar Jan 17 '24 13:01 RanvijayChouhan12

I had the same problem; The environment is the same as our friends above.

1999-xinz avatar Jan 19 '24 07:01 1999-xinz

I've been struggling with the same problem since this morning. It took me 2 hours and I still couldn't find a solution.

burakodabas avatar Jan 19 '24 07:01 burakodabas

Currently I am using "@react-native-clipboard/clipboard": "^1.13.2", as community/clipboard has problem with android.

Ashik55 avatar Jan 19 '24 08:01 Ashik55

@react-native-community/clipboard has been renamed to @react-native-clipboard/clipboard, what means that @react-native-community/clipboard is outdated (last release 3 years ago) and seems not to be compatible with latest react-native anymore.

tomamatics avatar Jan 19 '24 12:01 tomamatics

I had the same problem; The environment is the same as our friends above. My problem is solved. According to the reply from the friend below and the error message I reported, I replaced the Android SDK. The error message finally mentioned that the corresponding version of Android build tools could not be found. Here, I could not find Android build tools 34 and 33. So, I went and reinstalled both versions of the tool and was able to use @react-native clipboard/clipboard as normal. My react and React-native versions are as follows: react: "18.2.0", react-native: "0.73.2"

1999-xinz avatar Jan 22 '24 02:01 1999-xinz

My friends, i did some tests.

Maybe the problem is related to the jdk and sdk versions that is used.

as mentioned here: Setting up the development environment

you should have this:

  • Node >= 18
  • JDK 17
  • SDK 33 (at least)

Using this in my environment solved the problem for the last version (1.13.2)

micheleflammia avatar Feb 09 '24 17:02 micheleflammia

I had the same problem after upgrading RN. I changed path of @react-native-community/clipboard in package.json with react-native-clipboard/clipboard and it worked for me

"@react-native-community/clipboard": "https://github.com/react-native-clipboard/clipboard.git"

nusjose avatar Feb 23 '24 04:02 nusjose

"@react-native-clipboard/clipboard": "^1.13.2", you need this package instead of this "@react-native-community/clipboard": this thing work for me

this package is outdated now the last RN version that was working with this package was 0.72.3 you can use the new renamed package as mentioned above

but if you want a solution for the problem you have you can do the following go to node_modules/@react-native-community/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java

and replace this file content with this code

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.reactnativecommunity.clipboard;

import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.module.annotations.ReactModule;

/**
 * A module that allows JS to get/set clipboard contents.
 */
@ReactModule(name = ClipboardModule.NAME)
public class ClipboardModule extends ReactContextBaseJavaModule {

  public ClipboardModule(Context context) {
    super(new ReactApplicationContext(context));
  }

  public static final String NAME = "RNCClipboard";

  @Override
  public String getName() {
    return ClipboardModule.NAME;
  }

  private ClipboardManager getClipboardService() {
    return (ClipboardManager) getReactApplicationContext().getSystemService(getReactApplicationContext().CLIPBOARD_SERVICE);
  }

  @ReactMethod
  public void getString(Promise promise) {
    try {
      ClipboardManager clipboard = getClipboardService();
      ClipData clipData = clipboard.getPrimaryClip();
      if (clipData != null && clipData.getItemCount() >= 1) {
        ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
        promise.resolve("" + firstItem.getText());
      } else {
        promise.resolve("");
      }
    } catch (Exception e) {
      promise.reject(e);
    }
  }

  @ReactMethod
  public void setString(String text) {
    ClipData clipdata = ClipData.newPlainText(null, text);
    ClipboardManager clipboard = getClipboardService();
    clipboard.setPrimaryClip(clipdata);
  }

  @ReactMethod
  public void hasString(Promise promise) {
    try {
      ClipboardManager clipboard = getClipboardService();
      ClipData clipData = clipboard.getPrimaryClip();
      promise.resolve(clipData != null && clipData.getItemCount() >= 1);
    } catch (Exception e) {
      promise.reject(e);
    }
  }
}

FouadMagdy01 avatar Feb 26 '24 17:02 FouadMagdy01

@FouadMagdy01 Even if we made changes to node_modules we can't push that change to a git repo. So if other team members try to pull my latest changes they again can have the same issue. So how can we handle this?

seetharampullela avatar Mar 15 '24 07:03 seetharampullela

@FouadMagdy01 Even if we made changes to node_modules we can't push that change to a git repo. So if other team members try to pull my latest changes they again can have the same issue. So how can we handle this?

Use the patch-package package to share the changes with your team.

kimjisena avatar Mar 17 '24 08:03 kimjisena

@FouadMagdy01 Even if we made changes to node_modules we can't push that change to a git repo. So if other team members try to pull my latest changes they again can have the same issue. So how can we handle this?

I know that and in most cases it is not the best solution to fix bugs in node modules

But there are some workarounds as @kimjisena mentioned below

FouadMagdy01 avatar Mar 17 '24 09:03 FouadMagdy01

this package is outdated now the last RN version that was working with this package was 0.72.3 you can use the new renamed package as mentioned above

but if you want a solution for the problem you have you can do the following go to node_modules/@react-native-community/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java

and replace this file content with this code

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.reactnativecommunity.clipboard;

import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.module.annotations.ReactModule;

/**
 * A module that allows JS to get/set clipboard contents.
 */
@ReactModule(name = ClipboardModule.NAME)
public class ClipboardModule extends ReactContextBaseJavaModule {

  public ClipboardModule(Context context) {
    super(new ReactApplicationContext(context));
  }

  public static final String NAME = "RNCClipboard";

  @Override
  public String getName() {
    return ClipboardModule.NAME;
  }

  private ClipboardManager getClipboardService() {
    return (ClipboardManager) getReactApplicationContext().getSystemService(getReactApplicationContext().CLIPBOARD_SERVICE);
  }

  @ReactMethod
  public void getString(Promise promise) {
    try {
      ClipboardManager clipboard = getClipboardService();
      ClipData clipData = clipboard.getPrimaryClip();
      if (clipData != null && clipData.getItemCount() >= 1) {
        ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
        promise.resolve("" + firstItem.getText());
      } else {
        promise.resolve("");
      }
    } catch (Exception e) {
      promise.reject(e);
    }
  }

  @ReactMethod
  public void setString(String text) {
    ClipData clipdata = ClipData.newPlainText(null, text);
    ClipboardManager clipboard = getClipboardService();
    clipboard.setPrimaryClip(clipdata);
  }

  @ReactMethod
  public void hasString(Promise promise) {
    try {
      ClipboardManager clipboard = getClipboardService();
      ClipData clipData = clipboard.getPrimaryClip();
      promise.resolve(clipData != null && clipData.getItemCount() >= 1);
    } catch (Exception e) {
      promise.reject(e);
    }
  }
}

This worked for me!

marioecs avatar May 19 '24 04:05 marioecs

/**

  • Copyright (c) Facebook, Inc. and its affiliates.
  • This source code is licensed under the MIT license found in the
  • LICENSE file in the root directory of this source tree. */

package com.reactnativecommunity.clipboard;

import android.content.ClipboardManager; import android.content.ClipData;

import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.Promise; import com.facebook.react.module.annotations.ReactModule;

/**

  • A module that allows JS to get/set clipboard contents. */ @ReactModule(name = ClipboardModule.NAME) public class ClipboardModule extends ReactContextBaseJavaModule {

public static final String NAME = "RNCClipboard"; private final ReactApplicationContext reactContext;

public ClipboardModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; }

@Override public String getName() { return ClipboardModule.NAME; }

private ClipboardManager getClipboardService() { return (ClipboardManager) reactContext.getSystemService(reactContext.CLIPBOARD_SERVICE); }

@ReactMethod public void getString(Promise promise) { try { ClipboardManager clipboard = getClipboardService(); ClipData clipData = clipboard.getPrimaryClip(); if (clipData != null && clipData.getItemCount() >= 1) { ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0); promise.resolve("" + firstItem.getText()); } else { promise.resolve(""); } } catch (Exception e) { promise.reject(e); } }

@ReactMethod public void setString(String text) { ClipData clipdata = ClipData.newPlainText(null, text); ClipboardManager clipboard = getClipboardService(); clipboard.setPrimaryClip(clipdata); }

@ReactMethod public void hasString(Promise promise) { try { ClipboardManager clipboard = getClipboardService(); ClipData clipData = clipboard.getPrimaryClip(); promise.resolve(clipData != null && clipData.getItemCount() >= 1); } catch (Exception e) { promise.reject(e); } } }

me-as-rajesh avatar Sep 15 '25 17:09 me-as-rajesh