我的 React Native 应用程序的 Android 版本中的 GIF 没有动画

Sim*_*egn 7 android gif react-native fresco

我正在努力让我的 GIF 在我的 RN 应用程序的 Android 版本上制作动画。iOS 版本按预期制作循环 GIF 动画,但我在 Android 设备上只看到 GIF 中卡住的“单帧”图像。

implementation根据调试和 RN 文档,需要在dependencies/android/app/build.gradle 中添加几行,但即使在清理gradle (./gradlew clean在/android文件夹中运行)并删除 RN 的缓存之后app(react-native start --reset-cache在项目根文件夹中运行),我没有看到我的应用程序有任何区别。

我用谷歌搜索并尝试了很多。根据我的调试冒险,我尝试并仔细检查了这些建议,这似乎对其他人有用,但似乎对我不起作用......

  • 我已经尝试了似乎需要的壁画库的几个版本,并且我还尝试将线条放置在dependencies.
  • 一些答案还建议向android/app/proguard-rules.pro添加一行或多行代码,但这也不会改变任何内容。
  • 我在应用程序中以不同的方式使用 GIF,但它 始终width包含height在.style<Image />
  • 我尝试过使用来自不同 CDN 和域的 GIF-uri。
  • 在我的测试设备上重新安装了该应用程序。
  • 关闭并重新打开我的代码编辑器。

我正在使用以下版本:

  • 爪哇:11.0.12
  • 反应本机:0.65
  • 安卓 SDK:30.0.2
  • npm:6.14.4

这是我的完整/android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"


def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
def jscFlavor = 'org.webkit:android-jsc:+'
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }


    implementation project(':react-native-notifications')
    implementation 'com.google.firebase:firebase-core:16.0.0'
    implementation 'com.google.android.gms:play-services-ads:19.8.0'
    implementation "androidx.appcompat:appcompat:1.0.0"

    implementation 'com.facebook.fresco:fresco:2.4.0'
    implementation 'com.facebook.fresco:animated-gif:2.4.0'
    implementation 'com.facebook.fresco:webpsupport:2.4.0'
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
Run Code Online (Sandbox Code Playgroud)

如果我错过了一些明显的事情,请告诉我。我在 iOS 开发方面绝对更有经验,所以很可能我错过了一些东西:-)

小智 14

我使用react-native 0.66.3遇到了同样的问题,更新com.facebook.fresco:animated-gif到2.6.0inapp/build.gradle对我有用


Muh*_* MT 8

一个更简单的修复方法是使用 FastImage 库,它内置了 Gif 支持,并具有缓存等附加优点