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

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

Open
YuriOlepir opened this issue Aug 15, 2024 · 39 comments
Labels

Comments

@YuriOlepir
Copy link

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
@YuriOlepir YuriOlepir added the bug label Aug 15, 2024
@YuriOlepir YuriOlepir changed the title Error: cannot inherit from final ImageSource Error: cannot inherit from final ImageSource after update react-native to 0.75.1 Aug 15, 2024
@YuriOlepir YuriOlepir changed the title Error: cannot inherit from final ImageSource after update react-native to 0.75.1 [Error: cannot inherit from final ImageSource after update] react-native to 0.75.1 Aug 15, 2024
@YuriOlepir YuriOlepir changed the title [Error: cannot inherit from final ImageSource after update] react-native to 0.75.1 [Error: cannot inherit from final ImageSource] after update react-native to 0.75.1 Aug 15, 2024
@ahmaddehnavi
Copy link

Same here

@Koykoy200078
Copy link

same issue

@divyangkhatri
Copy link

same here

@ramonxm
Copy link

ramonxm commented Aug 16, 2024

same here, any solutions?

1 similar comment
@aviral1518
Copy link

same here, any solutions?

@Boscotec
Copy link

Boscotec commented Aug 17, 2024

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
Copy link

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

@hpanwar521
Copy link

hpanwar521 commented Aug 18, 2024

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

@aviral1518
Copy link

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
Copy link

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

@mohsenfl2022
Copy link

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

huextrat added a commit to appchoose/react-native-fast-image that referenced this issue Aug 19, 2024
@aviral1518
Copy link

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
  1. Install patch-package.

  2. 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

huextrat added a commit to appchoose/react-native-fast-image that referenced this issue Aug 19, 2024
huextrat added a commit to appchoose/react-native-fast-image that referenced this issue Aug 19, 2024
@NensiKasundra
Copy link

NensiKasundra commented Aug 20, 2024

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
Copy link

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

@NensiKasundra
Copy link

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

@deepanshushuklad11
Copy link

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
Copy link

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
Copy link

ramonxm commented Aug 20, 2024

0.75.2 is fixed this issues

not fixed for me

@NensiKasundra
Copy link

it's not working with 0.75.2 also

@NensiKasundra
Copy link

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
Copy link

+1

@gonzalorocha
Copy link

+1

@Kolahzary
Copy link

This PR on react-native will solve this issue:

@SpawN005
Copy link

+1

@liptonzuma
Copy link

Any progress on this fam

@kodeandoec
Copy link

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

@kodeandoec
Copy link

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

@Hao-yiwen
Copy link

same question.Hope fix this error coming soon.

@lukedanielson
Copy link

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
Copy link

Thioby commented Sep 2, 2024

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
Copy link

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 [email protected]

@JohnSmall
Copy link

Any idea when [email protected] will be released?

@JohnSmall
Copy link

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
Copy link

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
Copy link

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

@Gautham495
Copy link

@deepanshushuklad11 can we use your library in 73.8 version. Please provide some social media links so I can DM you there.

@deepanshushuklad11
Copy link

@deepanshushuklad11 can we use your library in 73.8 version. Please provide some social media links so I can DM you there.

@Gautham495 it should work , latest version is using fabric renderer too.
Connect with me on linkedin https://www.linkedin.com/in/deepanshu-s-48227062/

@Gautham495
Copy link

@deepanshushuklad11 Sent a connection request.

@lucianolopezz
Copy link

Updated react-native to 0.75.4, worked for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests