clipboard: Execution failed for task ':react-native-clipboard_clipboard:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.

Environment

Platforms

Versions

  • Android:
  • iOS:
  • react-native-netinfo:
  • react-native:
  • react:

Description

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@invertase/react-native-apple-authentication/android/src/main/java/com/RNAppleAuthentication/AppleAuthenticationAndroidModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/react-native-android-location-enabler/android/src/main/java/com/heanoria/library/reactnative/locationenabler/RNAndroidLocationEnablerModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:136: error: cannot find symbol if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ ^ symbol: variable R location: class VERSION_CODES /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:137: error: cannot find symbol bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 100, outputStream); ^ symbol: variable WEBP_LOSSLESS location: class CompressFormat Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 2 errors

Reproducible Demo

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 15
  • Comments: 32 (3 by maintainers)

Most upvoted comments

I followed below steps which solved my problem.

  1. go to this file. node_modules@react-native-community\clipboard\android\src\main\java\com\reactnativecommunity\clipboard\ClipboardModule.java
  2. import this : import com.facebook.react.bridge.ReactApplicationContext;
  3. convert extened package from ContextBaseJavaModule to ReactContextBaseJavaModule everywhere in file(maybe used at two places)
  4. conver this : from: public ClipboardModule(Context context) { super(context); } private ClipboardManager getClipboardService() { return (ClipboardManager) getcontext().getSystemService(getcontext().CLIPBOARD_SERVICE); } to: public ClipboardModule(Context context) { super(new ReactApplicationContext(context)); }

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

this worked for me . Hope this will guide you thanks

you need to just remove this package and @react-native-community\clipboard and install this reinstall this react-native-clipboard/clipboard one

Apologies to all who encountered this issue, please upgrade your compileSdkVersion to 30. The previous Bitmap.CompressFormat WEBP enum was deprecated in API level 30 hence WEBP_LOSSLESS enum was introduced. I’ll make a PR to update this requirement in the docs.

Environment

Platforms

Versions

  • Android:
  • iOS:
  • react-native-netinfo:
  • react-native:
  • react:

Description

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@invertase/react-native-apple-authentication/android/src/main/java/com/RNAppleAuthentication/AppleAuthenticationAndroidModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/react-native-android-location-enabler/android/src/main/java/com/heanoria/library/reactnative/locationenabler/RNAndroidLocationEnablerModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:136: error: cannot find symbol if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ ^ symbol: variable R location: class VERSION_CODES /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:137: error: cannot find symbol bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 100, outputStream); ^ symbol: variable WEBP_LOSSLESS location: class CompressFormat Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 2 errors

Reproducible Demo

Environment

Platforms

Versions

  • Android:
  • iOS:
  • react-native-netinfo:
  • react-native:
  • react:

Description

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@invertase/react-native-apple-authentication/android/src/main/java/com/RNAppleAuthentication/AppleAuthenticationAndroidModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/react-native-android-location-enabler/android/src/main/java/com/heanoria/library/reactnative/locationenabler/RNAndroidLocationEnablerModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:136: error: cannot find symbol if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ ^ symbol: variable R location: class VERSION_CODES /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java:137: error: cannot find symbol bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 100, outputStream); ^ symbol: variable WEBP_LOSSLESS location: class CompressFormat Note: /Users/extended/Downloads/vibesocialmusic-82830-react-native copy/src/node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard/ClipboardModule.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 2 errors

Reproducible Demo


The issue with its ClipboardModule.java lies in the use of the ContextBaseJavaModule class and the getContext() method, which are no longer available in the latest versions of React Native.


Steps to Fix =>>


  1. Replace ContextBaseJavaModule with ReactContextBaseJavaModule: ContextBaseJavaModule has been deprecated and removed. Replace it with ReactContextBaseJavaModule.

  2. Update the Constructor: Modify the constructor to accept a ReactApplicationContext instead of a Context.

  3. Use getReactApplicationContext() Instead of getContext(): Replace calls to getContext() with getReactApplicationContext().


Here is the updated version of ClipboardModule.java


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 static final String NAME = “RNCClipboard”;

public ClipboardModule(ReactApplicationContext reactContext) { super(reactContext); }

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

private ClipboardManager getClipboardService() { return (ClipboardManager) getReactApplicationContext().getSystemService(Context.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 = clipData.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); } } }


After Applying the Fix Rrbuild the project do below steps :::::


cd android ./gradlew clean cd … npm start --reset-cache npx react-native run-android

you need to just remove this package and @react-native-community\clipboard and install this reinstall this react-native-clipboard/clipboard one

this worked for me.

you need to just remove this package and @react-native-community\clipboard and install this reinstall this react-native-clipboard/clipboard one

actually i’m using react-native-otp-model by using @twotalltotems/react-native-otp-input library. but i’m getting this error.Cannot read property ‘getString’ of null. have you resolved this error earlier?

+1 Same

I have tried uninstalling and Re-installing Try 1: npm install @twotalltotems/react-native-otp-input react-native-clipboard/clipboard Try 2: npm install --save @react-native-community/react-native-clipboard Try 3: Tried togeling minSdkVersion from 21 to 24 and vise versa.

i have also raised the same https://github.com/tttstudios/react-native-otp-input/issues/227

+1 Have same issue. It was introduced in v1.11.0 with support for WEBP. Version 1.10.0 works fine.

I also encountered this issue and after some investigation it was caused as I was forcing dependencies to run on Android API version 29 as I needed it at the time for StethoWrapper to work with my react native project for Chrome debugging. API version 29 does not have the enum WEBP_LOSSLESS in the graphics package ( I believe it was introduced in API 30) hence this error.

I use Flipper now so I just removed these lines from my android/build.gradle and removed StethoWrapper

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 29     // have to match with build.gradle and app/build.gradle ones
                buildToolsVersion '29.0.3'  // have to match with build.gradle and app/build.gradle ones
            }
        }
    }
}

Hey there, I really wanted to use the new arch for my app, so I used a patch as a workaround and manually fixed it for now. A patch helps keep the change despite installing/changing dependencies. Here is a practical guide for those still struggling with it. I hope it helps.

  1. Install patch-package: First, you must install patch-package if you haven’t already. Run the following command in your project directory:

    npm install patch-package
    
  2. Edit the file causing the compilation error: Manually edit the ClipboardModule.java file in your node_modules/@react-native-clipboard/clipboard/android/src/main/java/com/reactnativecommunity/clipboard directory

New content to place in the file

/**
 * 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.annotation.TargetApi;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;

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.bridge.ReadableArray;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

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

  public static final String CLIPBOARD_TEXT_CHANGED = "RNCClipboard_TEXT_CHANGED";
  private ReactApplicationContext reactContext;
  private ClipboardManager.OnPrimaryClipChangedListener listener = null;

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

  public static final String NAME = "RNCClipboard";
  public static final String MIMETYPE_JPEG = "image/jpeg";
  public static final String MIMETYPE_JPG = "image/jpg";
  public static final String MIMETYPE_PNG = "image/png";
  public static final String MIMETYPE_WEBP = "image/webp";
  public static final String MIMETYPE_HEIC = "image/heic";
  public static final String MIMETYPE_HEIF = "image/heif";

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

  private ClipboardManager getClipboardService() {
    return (ClipboardManager) reactContext.getSystemService(Context.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 = clipData.getItemAt(0);
        promise.resolve("" + firstItem.getText());
      } else {
        promise.resolve("");
      }
    } catch (Exception e) {
      promise.reject(e);
    }
  }

  @Override
  public void getStrings(Promise promise) {
    promise.reject("Clipboard:getStrings", "getStrings is not supported on Android");
  }

  @Override
  public void getImagePNG(Promise promise) {
    promise.reject("Clipboard:getImagePNG", "getImagePNG is not supported on Android");
  }

  @Override
  public void getImageJPG(Promise promise) {
    promise.reject("Clipboard:getImageJPG", "getImageJPG is not supported on Android");
  }

  @Override
  public void setImage(String content, Promise promise) {
    promise.reject("Clipboard:setImage", "setImage is not supported on Android");
  }

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

  @Override
  public void setStrings(ReadableArray content) {

  }

  @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);
    }
  }

  @Override
  public void hasImage(Promise promise) {
    promise.reject("Clipboard:hasImage", "hasImage is not supported on Android");
  }

  @Override
  public void hasURL(Promise promise) {
    promise.reject("Clipboard:hasURL", "hasURL is not supported on Android");
  }

  @Override
  public void hasNumber(Promise promise) {
    promise.reject("Clipboard:hasNumber", "hasNumber is not supported on Android");
  }

  @Override
  public void hasWebURL(Promise promise) {
    promise.reject("Clipboard:hasWebURL", "hasWebURL is not supported on Android");
  }

  @ReactMethod
  public void getImage(Promise promise){
    ClipboardManager clipboardManager = getClipboardService();
    if (!(clipboardManager.hasPrimaryClip())){
      promise.resolve("");
    }
    else if (clipboardManager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
      promise.resolve("");
    }
    else {
      ClipData clipData = clipboardManager.getPrimaryClip();
      if(clipData != null){
        ClipData.Item item = clipData.getItemAt(0);
        Uri pasteUri = item.getUri();
        if (pasteUri != null){
          ContentResolver cr = reactContext.getContentResolver();
          String mimeType = cr.getType(pasteUri);
          if (mimeType != null){
            try {
              Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr, pasteUri);
              ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
              switch(mimeType){
                case MIMETYPE_JPEG:
                case MIMETYPE_JPG:
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                  break;
                case MIMETYPE_PNG:
                case MIMETYPE_HEIC:
                case MIMETYPE_HEIF:
                  bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                  break;
                case MIMETYPE_WEBP:
                  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q){
                    bitmap.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 100, outputStream);
                    break;
                  }
                  bitmap.compress(Bitmap.CompressFormat.WEBP, 100, outputStream);
                  break;
                default:
                  return;
              }
              byte[] byteArray = outputStream.toByteArray();
              String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);
              promise.resolve("data:" + mimeType + ";base64," + encodedString);
            } catch (IOException e) {
              promise.reject(e);
              e.printStackTrace();
            }
          }
        }
      }
      promise.resolve("");
    }
  }

  @ReactMethod
  public void setListener() {
    try {
      ClipboardManager clipboard = getClipboardService();
      listener = new ClipboardManager.OnPrimaryClipChangedListener() {
        @Override
        public void onPrimaryClipChanged() {
          reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(CLIPBOARD_TEXT_CHANGED, null);
        }
      };
      clipboard.addPrimaryClipChangedListener(listener);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @ReactMethod
  public void removeListener() {
    if(listener != null){
      try{
        ClipboardManager clipboard = getClipboardService();
        clipboard.removePrimaryClipChangedListener(listener);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  
  @ReactMethod
  public void addListener(String eventName) {
    // Keep: Required for RN built in Event Emitter Calls.
  }

  @Override
  public void removeListeners(double count) {
    // Keep: Required for RN built in Event Emitter Calls.
  }
}

  1. Create the Patch: After making the changes, create a patch file by running the following command:

    npx patch-package @react-native-clipboard/clipboard
    

    This will create a .patch file inside a patches directory at the root of your project.

  2. Apply the Patch: To apply the patch automatically whenever you install dependencies (e.g., running npm install or yarn), add a postinstall script to your package.json:

    "scripts": {
      "postinstall": "patch-package"
    }
    
  3. Clean and try to build the app: Clean your project and rebuild it to ensure the patch is applied correctly and that the issue is resolved.

    npx react-native clean
    npx react-native run-android
    

what about this

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ‘:react-native-community_clipboard:compileReleaseJavaWithJavac’.

Compilation failed; see the compiler error output for details.

  • Try:

Run with --info option to get more log output. Run with --scan to get full insights.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use ‘–warning-mode all’ to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 38s 183 actionable tasks: 169 executed, 14 up-to-date

info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor

error Failed to build the app. Error: Command failed with exit code 1: ./gradlew app:bundleRelease at makeError (/home/manojjyani/Public/BUY/node_modules/execa/lib/error.js:60:11) at module.exports.sync (/home/manojjyani/Public/BUY/node_modules/execa/index.js:194:17) at build (/home/manojjyani/Public/BUY/node_modules/react-native/node_modules/@react-native-community/cli-platform-android/build/commands/buildAndroid/index.js:68:22) at Object.buildAndroid [as func] (/home/manojjyani/Public/BUY/node_modules/react-native/node_modules/@react-native-community/cli-platform-android/build/commands/buildAndroid/index.js:60:10) at Command.handleAction (/home/manojjyani/Public/BUY/node_modules/@react-native-community/cli/build/index.js:118:23) at Command.listener [as _actionHandler] (/home/manojjyani/Public/BUY/node_modules/commander/lib/command.js:482:17) at /home/manojjyani/Public/BUY/node_modules/commander/lib/command.js:1283:65 at Command._chainOrCall (/home/manojjyani/Public/BUY/node_modules/commander/lib/command.js:1177:12) at Command._parseCommand (/home/manojjyani/Public/BUY/node_modules/commander/lib/command.js:1283:27) at /home/manojjyani/Public/BUY/node_modules/commander/lib/command.js:1081:27