React Native APK Installer
Install an Android APK from your react-native project.
This project is based react-native-install-apk
with enhancements to support current RN versions.
Installing ApkInstaller
- Add
rn-android-apk-install
to your project
- Install directly from npm:
npm install --save rn-android-apk-install
. - Or do
npm install --save git+https://github.com/walterleong95/rn-android-apk-install.git
in your main project.
- Link the library:
-
Add the following to
android/settings.gradle
:include ':rn-android-apk-install' project(':rn-android-apk-install').projectDir = new File(settingsDir, '../node_modules/rn-android-apk-install/android')
-
Add the following to
android/app/build.gradle
:...dependencies {...implementation project(':rn-android-apk-install')} -
Add the following to
android/app/src/main/java/**/MainApplication.java
:...// add this for rn-android-apk-install
- Add the following to your
AndroidManifest.xml
file. Take note of the value ofandroid:authorities
. It will be used in the last step. For example,(APP_BUNDLE_ID).provider
.
<!-- you might need the tools:replace thing to workaround rn-fetch-blob or other definitions of provider --> <!-- just make sure if you "replace" here that you include all the paths you are replacing *plus* the cache path we use -->
- Create a file named
filepaths.xml
in theres/xml
directory & append it with the following content.
<!-- Select one of the following based on your apk location --> <!-- cache dir is always available and requires no permissions, but space may be limited --> <!-- <files-path name="name" path="/" /> --> <!-- External cache dir is maybe user-friendly for downloaded APKs, but you must be careful. --> <!-- 1) in API <19 (KitKat) this requires WRITE_EXTERNAL_STORAGE permission. >=19, no permission --> <!-- 2) this directory may not be available, check Environment.isExternalStorageEmulated(file) to see --> <!-- 3) there may be no beneifit versus cache-path if external storage is emulated. Check Environment.isExternalStorageEmulated(File) to verify --> <!-- 4) the path will change for each app 'com.example' must be replaced by your application package --> <!-- <external-cache-path name="external-cache" path="/data/user/0/com.example/cache" /> --> <!-- Note that these external paths require WRITE_EXTERNAL_STORAGE permission --> <!-- <external-path name="some_external_path" path="put-your-specific-external-path-here" /> --> <!-- <external-files-path name="external-files" path="/data/user/0/com.example/cache" /> --> <!-- <external-media-path name="external-media" path="put-your-path-to-media-here" /> -->
- Lastly, add
import ApkInstaller from 'rn-android-apk-install'
to the list of imports. (You might also need to installreact-native-fs
package). Note: Your file provider ID must be the same as the one you've defined in step 4
; try var filePath = RNFSCachesDirectoryPath + '/com.example.app.apk'; var download = RNFS; downloadpromise; catcherror console;