项目作者: null--

项目描述 :
An APK Installer Library for react-native.
高级语言: Java
项目地址: git://github.com/null--/react-native-apk-installer.git
创建时间: 2017-06-19T05:50:11Z
项目社区:https://github.com/null--/react-native-apk-installer

开源协议:GNU General Public License v3.0

下载


React Native APK Installer

Install an android APK from your react-native project.
This project is based on react-native-android-library-boilerplate and react-native-install-apk

Installing ApkInstaller

  1. Add react-native-apk-installer to your project

    • Install directly from npm: npm install --save react-native-apk-installer.
    • Or do npm install --save git+https://github.com/null--/react-native-apk-installer.git in your main project.
  2. Link the library:

    • Add the following to android/settings.gradle:

      1. include ':react-native-apk-installer'
      2. project(':react-native-apk-installer').projectDir = new File(settingsDir, '../node_modules/react-native-apk-installer/android')
    • Add the following to android/app/build.gradle:

      1. ...
      2. dependencies {
      3. ...
      4. compile project(':react-native-apk-installer')
      5. }
    • Add the following to android/app/src/main/java/**/MainApplication.java:

      1. ...
      2. import com.cnull.apkinstaller.ApkInstallerPackage; // add this for react-native-apk-installer
      3. public class MainApplication extends Application implements ReactApplication {
      4. @Override
      5. protected List<ReactPackage> getPackages() {
      6. return Arrays.<ReactPackage>asList(
      7. new MainReactPackage(),
      8. ...
      9. new ApkInstallerPackage() // add this for react-native-apk-installer
      10. );
      11. }
      12. }
  3. Simply import ApkInstaller from 'react-native-apk-installer' (You might also need to install react-native-fs package).

    1. import RNFS from 'react-native-fs';
    2. import ApkInstaller from 'react-native-apk-installer'
    3. try {
    4. var filePath = RNFS.CachesDirectoryPath + '/com.example.app.apk';
    5. var download = RNFS.downloadFile({
    6. fromUrl: 'http://example.com/com.example.app.apk',
    7. toFile: filePath,
    8. progress: res => {
    9. console.log((res.bytesWritten / res.contentLength).toFixed(2));
    10. },
    11. progressDivider: 1
    12. });
    13. download.promise.then(result => {
    14. if(result.statusCode == 200) {
    15. console.log(filePath);
    16. ApkInstaller.install(filePath);
    17. }
    18. });
    19. }
    20. catch(error) {
    21. console.warn(error);
    22. }