在Android设备上运行代码时,我收到以下错误消息:任务':rn-fetch-blob:compileDebugJavaWithJavac'的执行失败

时间:2019-07-11 06:32:26

标签: react-native react-native-android

我正在使用rn-fetch-blob软件包从url下载文件。但是在运行代码时,在android上会引发以下错误:任务':rn-fetch-blob:compileDebugJavaWithJavac'的执行失败。

在终端中:此版本中使用了不推荐使用的Gradle功能,使其与Gradle 6.0不兼容。

这是我的代码:

import React, { Component } from "react";
import {
  View,
  Text,
  Button,
  Alert,
  ProgressBarAndroid,
  ToastAndroid,
  PermissionsAndroid
} from "react-native";
import RNFetchBlob from "rn-fetch-blob";

export default class componentName extends Component {
  constructor(props) {
    super(props);
    this.state = {
      progress: 0,
      loading: false
    };
  }

  actualDownload = () => {
    this.setState({
      progress: 0,
      loading: true
    });
    let dirs = RNFetchBlob.fs.dirs;
    RNFetchBlob.config({
      // add this option that makes response data to be stored as a file,
      // this is much more performant.
      path: dirs.DownloadDir + "/path-to-file.png",
      fileCache: true
    })
      .fetch(
        "GET",
        "http://www.usa-essays.com/blog/wp-content/uploads/2017/09/sample-5-1024x768.jpg",
        {
          //some headers ..
        }
      )
      .progress((received, total) => {
        console.log("progress", received / total);
        this.setState({ progress: received / total });
      })
      .then(res => {
        this.setState({
          progress: 100,
          loading: false
        });
        ToastAndroid.showWithGravity(
          "Your file has been downloaded to downloads folder!",
          ToastAndroid.SHORT,
          ToastAndroid.BOTTOM
        );
      });
  };

  async downloadFile() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: "Storage Permission",
          message: "App needs access to memory to download the file "
        }
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.actualDownload();
      } else {
        Alert.alert(
          "Permission Denied!",
          "You need to give storage permission to download the file"
        );
      }
    } catch (err) {
      console.warn(err);
    }
  }
  render() {
    return (
      <View>
        <Text> Download Files in Android </Text>
        <Button onPress={() => this.downloadFile()} title="Download" />
        {this.state.loading ? (
          <ProgressBarAndroid
            styleAttr="Large"
            indeterminate={false}
            progress={this.state.progress}
          />
        ) : null}
      </View>
    );
  }
}

3 个答案:

答案 0 :(得分:0)

React native 0.60为Android和iOS带来了一些重大变化。

对于android部分,此处说明了解决方法(解决方法):https://facebook.github.io/react-native/blog/2019/07/03/version-60#androidx-support

1) npm install --save-dev jetifier
2) npx jetify
3) npx react-native run-android
4) npx jetify

每次添加新模块或重置node_modules文件夹时,您都必须执行步骤4。

让我知道这是否解决了您的问题

答案 1 :(得分:0)

使用React Native版本0.59.0和0.60.0突破了,并给出了链接到库的错误。

锻炼:React Native 0.59.9正常工作。

答案 2 :(得分:0)

对于RN 0.60.0,他们正在为AndroidX提供支持, 您可以使用以下命令来解决此问题。

npm i jetifier
npx jetify

您可以参考以下AndroidX Support文档。

参考:https://facebook.github.io/react-native/blog/2019/07/03/version-60