Home > Blockchain >  How to install a downloaded .apk with React Native Expo?
How to install a downloaded .apk with React Native Expo?

Time:11-30

I need to install downloaded .apk file from within the Expo app (it's for update functionality). This is my code:

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>
  );
}

It downloads the file, stores it, but then there is an error during 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()

I tried passing uri first to FileSystem.getContentUriAsync() but then there is no error, the intent result is 0 but nothing happens.

My permissions in app.json:

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

Do I need any additional permissions to get it to work? Or is it completely impossible with Expo? Maybe I should save the file to different location to be able to use this intent?

I also tried android.intent.action.VIEW with no luck.

I test it on Android 13, on physical device. App is built with EAS.

CodePudding user response:

Maybe you can use this command to build release build.

expo:build android

For that you have to signup in Expo's website.

After that you can get apk in Expo's server.

CodePudding user response:

If you want to add auto-update functionality or OTA updates then I would strongly suggest you to try react-native-code-push but it is supported only in bare react-native project so you might face issues in migration. Try it out.

  • Related