【问题标题】:How to install a downloaded .apk with React Native Expo?如何使用 React Native Expo 安装下载的 .apk?
【发布时间】:2022-11-30 01:22:17
【问题描述】:

我需要从 Expo 应用程序中安装下载的 .apk 文件(用于更新功能)。这是我的代码:

import React from "react";
import { Button, View } from "react-native";
import * as FileSystem from "expo-file-system";
import { startActivityAsync } from "expo-intent-launcher";

export function Updater() {
  async function updateApk() {
    const uri = "https://expo.dev/artifacts/eas/my_apk_name.apk";
    const localUri = FileSystem.documentDirectory + "test.apk";

    try {
      await FileSystem.downloadAsync(uri, localUri);
      const apkUri = await FileSystem.getContentUriAsync(localUri);

      await startActivityAsync("android.intent.action.INSTALL_PACKAGE", {
        data: apkUri,
        flags: 1,
      });
    } catch (error) {
      alert(`Error during installing APK: ${error}`);
    }
  }

  return (
    <View>
      <Button title="Reset APK" onPress={updateApk} />
    </View>
  );
}

它下载文件,存储它,但在startActivityAsync期间出现错误:

Encountered an exception while calling native method:
Exception occurred while executing exported method startActivity on module ExpoIntentLauncher:
file://data/user/0/com.my.app.id/files/test.apk exposed beyond app through Intent.getData()

我尝试先将 uri 传递给 FileSystem.getContentUriAsync() 但随后没有错误,意图结果是 0 但没有任何反应。

我在app.json的权限:

"permissions": [
   "READ_EXTERNAL_STORAGE",
   "WRITE_EXTERNAL_STORAGE",
   "CAMERA"
]

我需要任何额外的权限才能让它工作吗?还是世博会完全不可能?也许我应该将文件保存到不同的位置才能使用这个意图?

我也尝试过android.intent.action.VIEW,但没有成功。

我在 Android 13 的物理设备上对其进行了测试。应用程序是用 EAS 构建的。

【问题讨论】:

    标签: android react-native expo


    【解决方案1】:

    也许您可以使用此命令来构建发布版本。

    expo:build android
    

    为此,您必须在 Expo 的website 中注册。

    之后你就可以在 Expo 服务器中获取 apk。

    【讨论】:

    • 我使用这个命令,我正在下载的 APK 已经在他们的服务器上了。我要做的是在下载后安装它。 Xamarin 有可能,因为我们在不同的项目中有这样的解决方案。
    【解决方案2】:

    如果您想添加自动更新功能或 OTA 更新,那么我强烈建议您尝试react-native-code-push,但它仅在裸反应本机项目中受支持,因此您可能会在迁移中遇到问题。试试看。

    【讨论】:

      猜你喜欢
      • 2022-12-10
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多