DylanVann / react-native-fast-image

🚩 FastImage, performant React Native image component.
MIT License
8.15k stars 1.49k forks source link

[Error: cannot inherit from final ImageSource] after update react-native to 0.75.1 #1043

Open YuriOlepir opened 1 month ago

YuriOlepir commented 1 month ago

Hello, i updated react-native to the latest 0.75.1 version and now have got such errors after executing run android command.

/app/node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:14: error: cannot inherit from final ImageSource
public class FastImageSource extends ImageSource { ^
/app/node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:72: error: isResource() in FastImageSource cannot override isResource() in ImageSource public boolean isResource() { ^ overridden method is final
/app/node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:101: error: getUri() in FastImageSource cannot override getUri() in ImageSource public Uri getUri() { ^ overridden method is final
ahmaddehnavi commented 1 month ago

Same here

Koykoy200078 commented 1 month ago

same issue

divyangkhatri commented 1 month ago

same here

ramonxm commented 1 month ago

same here, any solutions?

aviral1518 commented 1 month ago

same here, any solutions?

Boscotec commented 4 weeks ago

Temporary Workaround.

You can try modifying the library code until the library is officially updated.

Open the file causing issues: /node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java.

Replace the code with the one below.

package com.dylanvann.fastimage;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;

import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import com.facebook.react.views.imagehelper.ImageSource;

import javax.annotation.Nullable;

public class FastImageSource {
    private static final String DATA_SCHEME = "data";
    private static final String LOCAL_RESOURCE_SCHEME = "res";
    private static final String ANDROID_RESOURCE_SCHEME = "android.resource";
    private static final String ANDROID_CONTENT_SCHEME = "content";
    private static final String LOCAL_FILE_SCHEME = "file";
    private final Headers mHeaders;
    private Uri mUri;
    private final ImageSource imageSource; // Composition instead of inheritance

    public static boolean isBase64Uri(Uri uri) {
        return DATA_SCHEME.equals(uri.getScheme());
    }

    public static boolean isLocalResourceUri(Uri uri) {
        return LOCAL_RESOURCE_SCHEME.equals(uri.getScheme());
    }

    public static boolean isResourceUri(Uri uri) {
        return ANDROID_RESOURCE_SCHEME.equals(uri.getScheme());
    }

    public static boolean isContentUri(Uri uri) {
        return ANDROID_CONTENT_SCHEME.equals(uri.getScheme());
    }

    public static boolean isLocalFileUri(Uri uri) {
        return LOCAL_FILE_SCHEME.equals(uri.getScheme());
    }

    public FastImageSource(Context context, String source) {
        this(context, source, null);
    }

    public FastImageSource(Context context, String source, @Nullable Headers headers) {
        this(context, source, 0.0d, 0.0d, headers);
    }

    public FastImageSource(Context context, String source, double width, double height, @Nullable Headers headers) {
        imageSource = new ImageSource(context, source, width, height); // Create ImageSource instance
        mHeaders = headers == null ? Headers.DEFAULT : headers;
        mUri = imageSource.getUri(); // Get URI from ImageSource

        if (isResource() && TextUtils.isEmpty(mUri.toString())) {
            throw new Resources.NotFoundException("Local Resource Not Found. Resource: '" + getSource() + "'.");
        }

        if (isLocalResourceUri(mUri)) {
            // Convert res:/ scheme to android.resource:// so
            // Glide can understand the URI.
            mUri = Uri.parse(mUri.toString().replace("res:/", ANDROID_RESOURCE_SCHEME + "://" + context.getPackageName() + "/"));
        }
    }

    public boolean isBase64Resource() {
        return mUri != null && FastImageSource.isBase64Uri(mUri);
    }

    public boolean isResource() {
        return mUri != null && FastImageSource.isResourceUri(mUri);
    }

    public boolean isLocalFile() {
        return mUri != null && FastImageSource.isLocalFileUri(mUri);
    }

    public boolean isContentUri() {
        return mUri != null && FastImageSource.isContentUri(mUri);
    }

    public Object getSourceForLoad() {
        if (isContentUri()) {
            return getSource();
        }
        if (isBase64Resource()) {
            return getSource();
        }
        if (isResource()) {
            return getUri();
        }
        if (isLocalFile()) {
            return getUri().toString();
        }
        return getGlideUrl();
    }

    public Uri getUri() {
        return mUri;
    }

    public Headers getHeaders() {
        return mHeaders;
    }

    public GlideUrl getGlideUrl() {
        return new GlideUrl(getUri().toString(), getHeaders());
    }

    public String getSource() {
        return imageSource.getSource(); // Delegate to ImageSource
    }
}

Finally you can use patch package (https://github.com/ds300/patch-package) to patch it.

deepanshushuklad11 commented 4 weeks ago

Hello use https://www.npmjs.com/package/@d11/react-native-fast-image It is actively maintained and issue will be resolved soon

hpanwar521 commented 4 weeks ago

I am also getting the same issue, i am using latest 0.75.1 version of react native library

aviral1518 commented 4 weeks ago

Temporary Workaround.

You can try modifying the library code until the library is officially updated.

Open the file causing issues: /node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java.

Replace the code with the one below.

package com.dylanvann.fastimage;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;

import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import com.facebook.react.views.imagehelper.ImageSource;

import javax.annotation.Nullable;

public class FastImageSource {
    private static final String DATA_SCHEME = "data";
    private static final String LOCAL_RESOURCE_SCHEME = "res";
    private static final String ANDROID_RESOURCE_SCHEME = "android.resource";
    private static final String ANDROID_CONTENT_SCHEME = "content";
    private static final String LOCAL_FILE_SCHEME = "file";
    private final Headers mHeaders;
    private Uri mUri;
    private final ImageSource imageSource; // Composition instead of inheritance

    public static boolean isBase64Uri(Uri uri) {
        return DATA_SCHEME.equals(uri.getScheme());
    }

    public static boolean isLocalResourceUri(Uri uri) {
        return LOCAL_RESOURCE_SCHEME.equals(uri.getScheme());
    }

    public static boolean isResourceUri(Uri uri) {
        return ANDROID_RESOURCE_SCHEME.equals(uri.getScheme());
    }

    public static boolean isContentUri(Uri uri) {
        return ANDROID_CONTENT_SCHEME.equals(uri.getScheme());
    }

    public static boolean isLocalFileUri(Uri uri) {
        return LOCAL_FILE_SCHEME.equals(uri.getScheme());
    }

    public FastImageSource(Context context, String source) {
        this(context, source, null);
    }

    public FastImageSource(Context context, String source, @Nullable Headers headers) {
        this(context, source, 0.0d, 0.0d, headers);
    }

    public FastImageSource(Context context, String source, double width, double height, @Nullable Headers headers) {
        imageSource = new ImageSource(context, source, width, height); // Create ImageSource instance
        mHeaders = headers == null ? Headers.DEFAULT : headers;
        mUri = imageSource.getUri(); // Get URI from ImageSource

        if (isResource() && TextUtils.isEmpty(mUri.toString())) {
            throw new Resources.NotFoundException("Local Resource Not Found. Resource: '" + getSource() + "'.");
        }

        if (isLocalResourceUri(mUri)) {
            // Convert res:/ scheme to android.resource:// so
            // Glide can understand the URI.
            mUri = Uri.parse(mUri.toString().replace("res:/", ANDROID_RESOURCE_SCHEME + "://" + context.getPackageName() + "/"));
        }
    }

    public boolean isBase64Resource() {
        return mUri != null && FastImageSource.isBase64Uri(mUri);
    }

    public boolean isResource() {
        return mUri != null && FastImageSource.isResourceUri(mUri);
    }

    public boolean isLocalFile() {
        return mUri != null && FastImageSource.isLocalFileUri(mUri);
    }

    public boolean isContentUri() {
        return mUri != null && FastImageSource.isContentUri(mUri);
    }

    public Object getSourceForLoad() {
        if (isContentUri()) {
            return getSource();
        }
        if (isBase64Resource()) {
            return getSource();
        }
        if (isResource()) {
            return getUri();
        }
        if (isLocalFile()) {
            return getUri().toString();
        }
        return getGlideUrl();
    }

    public Uri getUri() {
        return mUri;
    }

    public Headers getHeaders() {
        return mHeaders;
    }

    public GlideUrl getGlideUrl() {
        return new GlideUrl(getUri().toString(), getHeaders());
    }

    public String getSource() {
        return imageSource.getSource(); // Delegate to ImageSource
    }
}

Finally you can use patch package (https://github.com/ds300/patch-package) to patch it.

@hpanwar521 try this solution. It will work.

hpanwar521 commented 4 weeks ago

This is also not working for me ... is there any other solution

mohsenfl2022 commented 3 weeks ago

I am also getting the same issue. I am using the latest 0.75.1 version of the React Native library. Please heeeeeeeeeeeeeelp!

aviral1518 commented 3 weeks ago

I am also getting the same issue. I am using the latest 0.75.1 version of the React Native library. Please heeeeeeeeeeeeeelp!

@mohsenfl2022 follow these steps:-

  1. Put the attached patch file inside the patches folder. If there is no patches folder, create one and put that file

    image
  2. Install patch-package.

  3. After installation, run npx patch-package react-native-fast-image.

After that try building the app. It will work fine

react-native-fast-image+8.6.3.patch

NensiKasundra commented 3 weeks ago

facing the same issue with react-native 0.75.1 version while running the application in android.

node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:14: error: cannot inherit from final ImageSource public class FastImageSource extends ImageSource { ^

@DylanVann Kindly look into this

deepanshushuklad11 commented 3 weeks ago

This is fixed in https://github.com/facebook/react-native/pull/46092/files You can also try @d11/react-native-fast-image

NensiKasundra commented 3 weeks ago

it's a fork of this. can we upgrade this in the same?

deepanshushuklad11 commented 3 weeks ago

it's a fork of this. can we upgrade this in the same?

react-native is the peer dependency of react-native-fast-image. So when you upgrade your app to the fixed version of react-native , it will automatically work

NensiKasundra commented 3 weeks ago

Here are the versions of libs. still, it's not working.

    "react": "18.3.1",
    "react-native": "0.75.1",
    "react-native-fast-image": "^8.6.3",
ramonxm commented 3 weeks ago

0.75.2 is fixed this issues

not fixed for me

NensiKasundra commented 3 weeks ago

it's not working with 0.75.2 also

NensiKasundra commented 3 weeks ago

Getting below errors with 0.75.2 version

/node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:72: error: isResource() in FastImageSource cannot override isResource() in ImageSource public boolean isResource() { ^ overridden method is final

/node_modules/react-native-fast-image/android/src/main/java/com/dylanvann/fastimage/FastImageSource.java:101: error: getUri() in FastImageSource cannot override getUri() in ImageSource public Uri getUri() { ^ overridden method is final

hamdij0maa commented 3 weeks ago

+1

gonzalorocha commented 3 weeks ago

+1

Kolahzary commented 3 weeks ago

This PR on react-native will solve this issue:

SpawN005 commented 3 weeks ago

+1

liptonzuma commented 3 weeks ago

Any progress on this fam

kodeandoec commented 3 weeks ago

Someone have a solution for this issue, can someone contact with the repo owner?

kodeandoec commented 3 weeks ago

There's also a new alternative, Faster Image, https://github.com/candlefinance/faster-image

Hao-yiwen commented 2 weeks ago

same question.Hope fix this error coming soon.

lukedanielson commented 2 weeks ago

I've setup a patch file for this issue for version 0.75.2 in ./patches/react-native+0.75.2.patch

diff --git a/node_modules/react-native/ReactAndroid/api/ReactAndroid.api b/node_modules/react-native/ReactAndroid/api/ReactAndroid.api
index e647de2..6a14ffd 100644
--- a/node_modules/react-native/ReactAndroid/api/ReactAndroid.api
+++ b/node_modules/react-native/ReactAndroid/api/ReactAndroid.api
@@ -6459,9 +6459,9 @@ public class com/facebook/react/views/imagehelper/ImageSource {
    public final fun getSize ()D
    public final fun getSource ()Ljava/lang/String;
    public static final fun getTransparentBitmapImageSource (Landroid/content/Context;)Lcom/facebook/react/views/imagehelper/ImageSource;
-   public final fun getUri ()Landroid/net/Uri;
+   public fun getUri ()Landroid/net/Uri;
    public fun hashCode ()I
-   public final fun isResource ()Z
+   public fun isResource ()Z
 }

 public final class com/facebook/react/views/imagehelper/ImageSource$Companion {
diff --git a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt
index 583b4a0..947044c 100644
--- a/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt
+++ b/node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ImageSource.kt
@@ -23,12 +23,14 @@ constructor(
 ) {

   /** Get the URI for this image - can be either a parsed network URI or a resource URI. */
-  public val uri: Uri = computeUri(context)
+  public open val uri: Uri = computeUri(context)
   /** Get the area of this image. */
   public val size: Double = width * height
   /** Get whether this image source represents an Android resource or a network URI. */
-  public var isResource: Boolean = false
-    private set
+  public open val isResource: Boolean
+    get() = _isResource
+
+  private var _isResource: Boolean = false

   override fun equals(other: Any?): Boolean {
     if (this === other) {
@@ -58,7 +60,7 @@ constructor(
       }

   private fun computeLocalUri(context: Context): Uri {
-    isResource = true
+    _isResource = true
     return ResourceDrawableIdHelper.instance.getResourceDrawableUri(context, source)
   }

However the same build error happens after:

rm -Rf node_modules/
npm install
cd android && ./gradlew clean && cd ../
watchman watch-del all
npm run start --reset-cache

Is it possible to patch this?

Thioby commented 1 week ago

Hello use https://www.npmjs.com/package/@d11/react-native-fast-image It is actively maintained and issue will be resolved soon

does not work.

deepanshushuklad11 commented 1 week ago

Hello use https://www.npmjs.com/package/@d11/react-native-fast-image It is actively maintained and issue will be resolved soon

does not work.

It will be fixed in react-native@0.75.3

JohnSmall commented 1 week ago

Any idea when react-native@0.75.3 will be released?

JohnSmall commented 1 week ago

https://www.npmjs.com/package/@d11/react-native-fast-image

Yup, I can confirm that the above repo does not fix the problem. Even though the most recent release was less than 24 hours ago.

deepanshushuklad11 commented 1 week ago

https://www.npmjs.com/package/@d11/react-native-fast-image

Yup, I can confirm that the above repo does not fix the problem. Even though the most recent release was less than 24 hours ago.

@JohnSmall The issue has been fixed in react-native. Once they release new version it will automatically gets fixed in fast image. Currently i am triggering a cron based nightly build for everyone to see whats coming. That is reason last publish is less than 24 hours ago. I am working on nightly build optimisation.In future this will be triggered only if changes are there. Let me know if you have more concerns i am happy to help.

deepanshushuklad11 commented 5 days ago

@YuriOlepir Issue has been fixed in @d11/react-native-fast-image