├── .gitignore ├── android ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ └── strings.xml │ │ │ └── xml │ │ │ │ └── provider_paths.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── superhao │ │ │ └── react_native_apk_manager │ │ │ ├── ApkManagerPackage.java │ │ │ └── ApkManagerModule.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── superhao │ │ │ └── react_native_apk_manager │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── superhao │ │ └── react_native_apk_manager │ │ └── ExampleInstrumentedTest.java ├── build.gradle ├── proguard-rules.pro └── react-native-apk-manager.iml ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /android/build 4 | /react-native-mob-sms/*.iml 5 | 6 | # ios 7 | ios/build -------------------------------------------------------------------------------- /android/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | react-native-apk-manager 3 | 4 | -------------------------------------------------------------------------------- /android/src/main/res/xml/provider_paths.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /android/src/test/java/com/superhao/react_native_apk_manager/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.superhao.react_native_apk_manager; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 27 5 | 6 | defaultConfig { 7 | minSdkVersion 16 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | 14 | } 15 | 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | 23 | } 24 | 25 | dependencies { 26 | compile 'com.facebook.react:react-native:+' 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | } 29 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 14 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /android/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /android/src/main/java/com/superhao/react_native_apk_manager/ApkManagerPackage.java: -------------------------------------------------------------------------------- 1 | package com.superhao.react_native_apk_manager; 2 | 3 | import com.facebook.react.ReactPackage; 4 | import com.facebook.react.bridge.NativeModule; 5 | import com.facebook.react.bridge.ReactApplicationContext; 6 | import com.facebook.react.uimanager.ViewManager; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | public class ApkManagerPackage implements ReactPackage { 13 | @Override 14 | public List createNativeModules(ReactApplicationContext reactContext) { 15 | List modules = new ArrayList<>(); 16 | modules.add(new ApkManagerModule(reactContext)); 17 | return modules; 18 | } 19 | 20 | @Override 21 | public List createViewManagers(ReactApplicationContext reactContext) { 22 | return Collections.emptyList(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /android/src/androidTest/java/com/superhao/react_native_apk_manager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.superhao.react_native_apk_manager; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.superhao.react_native_apk_manager.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-apk-manager", 3 | "version": "1.1.0", 4 | "description": "React Native bridging library for android to manager apk.(install apk, uninstall apk, open apk, check app is installed or not, check app is installed or not with channel)", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/1556173267/react-native-apk-manager.git" 12 | }, 13 | "keywords": [ 14 | "react", 15 | "react-native", 16 | "apk-manager", 17 | "android", 18 | "install-apk", 19 | "unstall-apk", 20 | "check-apk-isinstalled", 21 | "open-apk" 22 | ], 23 | "peerDependencies": { 24 | "react-native": ">=0.40" 25 | }, 26 | "author": "superhao", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/1556173267/react-native-apk-manager/issues" 30 | }, 31 | "homepage": "https://github.com/1556173267/react-native-apk-manager#readme" 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 薛定谔的猫 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | import {NativeModules, Platform, PermissionsAndroid } from 'react-native'; 4 | const apkManagerModule = NativeModules.ApkManagerModule; 5 | 6 | export function uninstallApk(packageName) { 7 | if (Platform.OS === 'android') { 8 | apkManagerModule.uninstallApk(packageName); 9 | } 10 | } 11 | 12 | export function installApk(filePath) { 13 | if (Platform.OS === 'android') { 14 | PermissionsAndroid.request( 15 | PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((data) => { 16 | if (data === 'granted') { 17 | apkManagerModule.installApk(filePath); 18 | } else { 19 | alert('授权被拒绝'); 20 | } 21 | }).catch((err) => { 22 | alert('获取授权失败'); 23 | }); 24 | } 25 | } 26 | 27 | export function isExpChannel(packageName, metaDataName, channel) { 28 | 29 | if (Platform.OS === 'android') { 30 | return new Promise((resolve, reject) => { 31 | apkManagerModule.isExpChannel(packageName, metaDataName, channel).then((data)=>{ 32 | resolve(data); 33 | }).catch((error)=>{ 34 | reject(error); 35 | }); 36 | }); 37 | } 38 | 39 | } 40 | 41 | export function isExpChannels(packageNames, metaDataNames, channels) { 42 | 43 | if (Platform.OS === 'android') { 44 | return new Promise((resolve, reject) => { 45 | apkManagerModule.isExpChannels(packageNames, metaDataNames, channels).then((data)=>{ 46 | resolve(data); 47 | }).catch((error)=>{ 48 | reject(error); 49 | }); 50 | }); 51 | } 52 | 53 | } 54 | 55 | export function openApk(packageName) { 56 | if (Platform.OS === 'android') { 57 | apkManagerModule.openApk(packageName); 58 | } 59 | } 60 | 61 | export function isAppInstalled(packageName) { 62 | 63 | if (Platform.OS === 'android') { 64 | return new Promise((resolve, reject) => { 65 | apkManagerModule.isAppInstalled(packageName).then((data)=>{ 66 | resolve(data); 67 | }).catch((error)=>{ 68 | reject(error); 69 | }); 70 | }); 71 | } 72 | 73 | } 74 | 75 | export function isAppsInstalled(packageNames) { 76 | 77 | if (Platform.OS === 'android') { 78 | return new Promise((resolve, reject) => { 79 | apkManagerModule.isAppsInstalled(packageNames).then((data)=>{ 80 | resolve(data); 81 | }).catch((error)=>{ 82 | reject(error); 83 | }); 84 | }); 85 | } 86 | 87 | } 88 | 89 | export function getAPKInformation(apkFile) { 90 | 91 | if (Platform.OS === 'android') { 92 | return new Promise((resolve, reject) => { 93 | PermissionsAndroid.request( 94 | PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((data) => { 95 | if (data === 'granted') { 96 | apkManagerModule.getAPKInformation(apkFile).then((data)=>{ 97 | resolve(data); 98 | }).catch((error)=>{ 99 | reject(error); 100 | }); 101 | } else { 102 | reject("auth fail"); 103 | alert('授权被拒绝'); 104 | } 105 | }).catch((err) => { 106 | reject("auth fail"); 107 | alert('获取授权失败'); 108 | }); 109 | 110 | }); 111 | } 112 | 113 | } 114 | 115 | export function getAppInformation(packageName) { 116 | 117 | if (Platform.OS === 'android') { 118 | return new Promise((resolve, reject) => { 119 | apkManagerModule.getAppInformation(packageName).then((data)=>{ 120 | resolve(data); 121 | }).catch((error)=>{ 122 | reject(error); 123 | }); 124 | }); 125 | } 126 | 127 | } 128 | 129 | export function getAPKMetaDataByKey(apkFile, key) { 130 | 131 | if (Platform.OS === 'android') { 132 | return new Promise((resolve, reject) => { 133 | 134 | PermissionsAndroid.request( 135 | PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((data) => { 136 | if (data === 'granted') { 137 | apkManagerModule.getAPKMetaDataByKey(apkFile, key).then((data)=>{ 138 | resolve(data); 139 | }).catch((error)=>{ 140 | reject(error); 141 | }); 142 | } else { 143 | reject("auth fail"); 144 | alert('授权被拒绝'); 145 | } 146 | }).catch((err) => { 147 | reject("auth fail"); 148 | alert('获取授权失败'); 149 | }); 150 | 151 | }); 152 | } 153 | 154 | } 155 | 156 | export function getAppMetaDataByKey(packageName, key) { 157 | 158 | if (Platform.OS === 'android') { 159 | return new Promise((resolve, reject) => { 160 | apkManagerModule.getAppMetaDataByKey(packageName, key).then((data)=>{ 161 | resolve(data); 162 | }).catch((error)=>{ 163 | reject(error); 164 | }); 165 | }); 166 | } 167 | 168 | } 169 | 170 | export function getInstalledAppInfo() { 171 | 172 | if (Platform.OS === 'android') { 173 | return new Promise((resolve, reject) => { 174 | apkManagerModule.getInstalledAppInfo().then((data)=>{ 175 | resolve(data); 176 | }).catch((error)=>{ 177 | reject(error); 178 | }); 179 | }); 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-apk-manager 2 | React Native bridging library for android to manager apk.(install apk, uninstall apk, open apk, check app is installed or not, check app is installed or not with channel) 3 | 4 | ## TOC 5 | 6 | * [Installation](#installation) 7 | * [Linking](#linking) 8 | * [Usage](#usage) 9 | * [API](#api) 10 | 11 | ## Installation 12 | 13 | Using npm: 14 | 15 | ```shell 16 | npm install --save react-native-apk-manager 17 | ``` 18 | 19 | or using yarn: 20 | 21 | ```shell 22 | yarn add react-native-apk-manager 23 | ``` 24 | 25 | ## Linking 26 | 27 | ### Automatic 28 | 29 | ```shell 30 | react-native link react-native-apk-manager 31 | ``` 32 | 33 | (or using [`rnpm`](https://github.com/rnpm/rnpm) for versions of React Native < 0.27) 34 | 35 | ```shell 36 | rnpm link react-native-apk-manager 37 | ``` 38 | 39 | ### Manual 40 | 41 |
42 | Android 43 | 44 | * **_optional_** in `android/build.gradle`: 45 | 46 | ```gradle 47 | ... 48 | ext { 49 | // dependency versions 50 | compileSdkVersion = "" // default: 27 51 | targetSdkVersion = "" // default: 27 52 | } 53 | ... 54 | ``` 55 | 56 | * in `android/app/build.gradle`: 57 | 58 | ```diff 59 | dependencies { 60 | ... 61 | compile "com.facebook.react:react-native:+" // From node_modules 62 | + compile project(':react-native-apk-manager') 63 | } 64 | ``` 65 | 66 | * in `android/settings.gradle`: 67 | 68 | ```diff 69 | ... 70 | include ':app' 71 | + include ':react-native-apk-manager' 72 | + project(':react-native-apk-manager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-apk-manager/android') 73 | ``` 74 | 75 | #### With React Native 0.29+ 76 | 77 | * in `MainApplication.java`: 78 | 79 | ```diff 80 | + import com.superhao.react_native_apk_manager.ApkManagerPackage; 81 | 82 | public class MainApplication extends Application implements ReactApplication { 83 | ...... 84 | 85 | @Override 86 | protected List getPackages() { 87 | return Arrays.asList( 88 | + new ApkManagerPackage(), 89 | new MainReactPackage() 90 | ); 91 | } 92 | 93 | ...... 94 | } 95 | ``` 96 |
97 | 98 | ## Usage 99 | 100 | ```js 101 | import * as ApkManager from 'react-native-apk-manager'; 102 | ``` 103 | 104 | ## API 105 | 106 | | Method | Params | Return Type | 107 | | :----- | :------ | :---------- | 108 | | [isAppInstalled()](#isappinstalledpagenamestring-promiseboolean) | `pageName` | `Promise` | 109 | | [isAppsInstalled()](#isappsinstalledpackagenamesarraystringpromisearrayboolean) | `packageNames>` | `Promise>` | 110 | | [installApk()](#installapkfilepathstring) | `filePath` | `void` | 111 | | [uninstallApk()](#uninstallapkpackagenamestring) | `packageName` | `void` | 112 | | [openApk()](#openapkpackagenamestring) | `packageName` | `void` | 113 | | [isExpChannel()](#isexpchannelpackagenamestring-metadatanamestring-channelstringpromiseboolean) | `packageName, metaDataName, channel` | `Promise` | 114 | | [isExpChannels()](#isexpchannelspackagenamesarraystring-metadatanamesarraystring-channelsarraystringpromisearrayboolean) | `packageNames>, metaDataNames>, channels>` | `Promise>` | 115 | | [getAPKInfomation()](#getapkinformationapkfilestring-promiseobject) | `apkFile` | `Promise` | 116 | | [getAppInformation()](#getappinformationpackagenamestring-promiseobject) | `packageName` | `Promise` | 117 | | [getAppMetaDataByKey()](#getappmetadatabykeypackagenamestring-keystring-promisestring) | `packageName, key` | `Promise` | 118 | | [getInstalledAppInfo()](#getinstalledappinfo-promisearrayobject) | `null` | `Promise>` | 119 | 120 | ### isAppInstalled(`pageName`): `Promise` 121 | 122 | Check app isInstalled with packageName. 123 | 124 | **Examples** 125 | 126 | ```js 127 | ApkManager.isAppInstalled('com.lengjing.ktyaokongc').then((data)=> { 128 | // true or false 129 | }); 130 | ``` 131 | 132 | --- 133 | 134 | ### isAppsInstalled(`packageNames>`):`Promise>` 135 | 136 | Check apps isInstalled with packageName List. 137 | 138 | **Examples** 139 | 140 | ```js 141 | ApkManager.isAppInstalled(['com.lengjing.ktyaokongc', 'test.tets', 'xxxx', 'xxxx']).then((data)=> { 142 | // [true, false, false, true] 143 | }); 144 | ``` 145 | 146 | --- 147 | 148 | ### installApk(`filePath`) 149 | 150 | Install apk with apkFilePath. 151 | 152 | **Examples** 153 | 154 | ```js 155 | ApkManager.installApk('storage/emulated/0/renlaifeng_download/test.apk'); 156 | ``` 157 | --- 158 | 159 | ### uninstallApk(`packageName`) 160 | 161 | Uninstall apk with packageName. 162 | 163 | **Examples** 164 | 165 | ```js 166 | ApkManager.uninstallApk('com.lengjing.ktyaokongc'); 167 | ``` 168 | 169 | --- 170 | 171 | ### openApk(`packageName`) 172 | 173 | Open other app with packageName. 174 | 175 | **Examples** 176 | 177 | ```js 178 | ApkManager.openApk('com.lengjing.ktyaokongc'); 179 | ``` 180 | --- 181 | 182 | ### isExpChannel(`packageName, metaDataName, channel`):`Promise` 183 | 184 | Check special channel app is installed with packageName, metaName, special channel. 185 | 186 | * Before use, you should know app's metaName and channel in AndroidManifest.xml. 187 | ```xml 188 | => // here is the packageName 'com.ownmoduletest' 190 | ... 191 | => // here is the metaDataName 'JPUSH_APPKEY' 194 | ... 195 | 196 | 197 | ``` 198 | 199 | **Examples** 200 | 201 | ```js 202 | ApkManager.isExpChannel('com.ownmoduletest', 'JPUSH_APPKEY', '1232444').then((data)=> { 203 | // true 204 | }); 205 | 206 | ApkManager.isExpChannel('com.ownmoduletest', 'JPUSH_APPKEY', '11111').then((data)=> { 207 | // false 208 | }); 209 | 210 | ApkManager.isExpChannel('com.ownmoduletest', 'otherkeyname', '1232444').then((data)=> { 211 | // false 212 | }); 213 | ``` 214 | 215 | --- 216 | 217 | ### isExpChannels(`packageNames>, metaDataNames>, channels>`):`Promise>` 218 | 219 | Check special channel apps is installed with packageNames, metaNames, special channels. 220 | 221 | * Before use, you should know app's metaName and channel in AndroidManifest.xml. 222 | ```xml 223 | => // here is the packageName 'com.ownmoduletest' 225 | ... 226 | => // here is the metaDataName 'JPUSH_APPKEY' 229 | ... 230 | 231 | 232 | ``` 233 | 234 | **Examples** 235 | 236 | ```js 237 | ApkManager.isExpChannels(['com.ownmoduletest', 'com.test', 'xxxxxx'], ['JPUSH_APPKEY', 'test_key', 'xxxxxxx'], ['1232444', 'wanted channel', 'wanted channel']).then((data)=> { 238 | // [true, true, true] 239 | }); 240 | 241 | ApkManager.isExpChannels(['com.ownmoduletest', 'com.test', 'xxxxxx'], ['JPUSH_APPKEY', 'test_key', 'xxxxxxx'], ['11111', 'wanted channel', 'wanted channel']).then((data)=> { 242 | // [false, true, true] 243 | }); 244 | 245 | ApkManager.isExpChannels(['com.ownmoduletest', 'com.test', 'xxxxxx'], ['other_key', 'test_key', 'xxxxxxx'], ['1232444', 'wanted channel', 'wanted channel']).then((data)=> { 246 | // [false, true, true] 247 | }); 248 | ``` 249 | --- 250 | 251 | ### getAPKInformation(`apkFile`): `Promise` 252 | 253 | Get apk information with apk file path. You can get appName, packageName, versionName, versionCode, installLocation, firstInstallTime, lastUpdateTime. 254 | 255 | **Examples** 256 | 257 | ```js 258 | ApkManager.getAPKInfomation('/storage/emulated/0/renlaifeng_download/app-release .apk').then((data)=>{ 259 | console.log(data); 260 | // {appName: "test", packageName: "com.test", versionName: "1.0.0", versionCode: "1", installLocation: 0, firstInstallTime: 0, lastUpdateTime: 0} 261 | }).catch((error)=>{ 262 | console.log(error); 263 | // 400 Get ApkInfomation Fail 264 | }); 265 | ``` 266 | --- 267 | 268 | ### getAppInformation(`packageName`): `Promise` 269 | 270 | Get app information with packageName. You can get appName, packageName, versionName, versionCode, installLocation, firstInstallTime, lastUpdateTime. 271 | 272 | **Examples** 273 | 274 | ```js 275 | ApkManager.getAppInfomation('com.test').then((data)=>{ 276 | console.log(data); 277 | // {appName: "test", packageName: "com.test", versionName: "1.0.0", versionCode: "1", installLocation: 0, firstInstallTime: 123234456, lastUpdateTime: 123234456} 278 | }).catch((error)=>{ 279 | console.log(error); 280 | // 400 Get AppInfomation Fail 281 | }); 282 | ``` 283 | --- 284 | 285 | ### getAppMetaDataByKey(`packageName, key`): `Promise` 286 | 287 | Get app information with packageName and key(metadata name). 288 | 289 | * Before use, you should know app's packageName and key(metadata name) in target AndroidManifest.xml. 290 | ```xml 291 | => // here is the packageName 'com.test' 293 | ... 294 | => // here is the metaDataName 'JPUSH_APPKEY' 297 | => // here is the metaDataName 'JPUSH_CHANNEL' 298 | ... 299 | 300 | 301 | ``` 302 | 303 | **Examples** 304 | 305 | ```js 306 | ApkManager.getAppMetaDataByKey('com.test', 'JPUSH_CHANNEL').then((data)=>{ 307 | console.log(data); 308 | // test 309 | }).catch((error)=>{ 310 | console.log(error); 311 | // Not Found MetaData 312 | }); 313 | 314 | ApkManager.getAppMetaDataByKey('com.test', 'JPUSH_APPKEY').then((data)=>{ 315 | console.log(data); 316 | // 1232444 317 | }).catch((error)=>{ 318 | console.log(error); 319 | // Not Found MetaData 320 | }); 321 | ``` 322 | --- 323 | 324 | ### getInstalledAppInfo(): `Promise>` 325 | 326 | Get all your installed app information. You can get appName, packageName, versionName, versionCode, installLocation, firstInstallTime, lastUpdateTime. 327 | 328 | **Examples** 329 | 330 | ```js 331 | ApkManager.getInstalledAppInfo().then((data)=>{ 332 | console.log(data); 333 | /* [ 334 | {appName: "installedApp", packageName: "com.installedApp", versionName: "1.0.0", versionCode: "1", installLocation: 0, firstInstallTime: 123234456, lastUpdateTime: 123234456}, 335 | {appName: "installedApp1", packageName: "com.installedApp1", versionName: "1.0.0", versionCode: "1", installLocation: 0, firstInstallTime: 123234456, lastUpdateTime: 123234456}, 336 | {appName: "installedApp2", packageName: "com.installedApp2", versionName: "1.0.0", versionCode: "1", installLocation: 0, firstInstallTime: 123234456, lastUpdateTime: 123234456} 337 | ] 338 | */ 339 | }).catch((error)=>{ 340 | console.log(error); 341 | // 400 Get AppInfomation Fail 342 | }); 343 | ``` 344 | --- 345 | -------------------------------------------------------------------------------- /android/react-native-apk-manager.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /android/src/main/java/com/superhao/react_native_apk_manager/ApkManagerModule.java: -------------------------------------------------------------------------------- 1 | package com.superhao.react_native_apk_manager; 2 | 3 | import android.app.Activity; 4 | import android.content.BroadcastReceiver; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.IntentFilter; 8 | import android.content.pm.ApplicationInfo; 9 | import android.content.pm.PackageInfo; 10 | import android.content.pm.PackageManager; 11 | import android.content.pm.PermissionInfo; 12 | import android.net.Uri; 13 | import android.os.Build; 14 | import android.provider.Settings; 15 | import androidx.annotation.RequiresApi; 16 | import androidx.core.content.FileProvider; 17 | import android.util.Log; 18 | 19 | import com.facebook.common.logging.FLog; 20 | import com.facebook.react.bridge.ActivityEventListener; 21 | import com.facebook.react.bridge.Arguments; 22 | import com.facebook.react.bridge.BaseActivityEventListener; 23 | import com.facebook.react.bridge.Dynamic; 24 | import com.facebook.react.bridge.LifecycleEventListener; 25 | import com.facebook.react.bridge.Promise; 26 | import com.facebook.react.bridge.ReactApplicationContext; 27 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 28 | import com.facebook.react.bridge.ReactMethod; 29 | import com.facebook.react.bridge.ReadableArray; 30 | import com.facebook.react.bridge.ReadableMap; 31 | import com.facebook.react.bridge.ReadableMapKeySetIterator; 32 | import com.facebook.react.bridge.ReadableType; 33 | import com.facebook.react.bridge.WritableArray; 34 | import com.facebook.react.bridge.WritableMap; 35 | import com.facebook.react.bridge.WritableNativeArray; 36 | import com.facebook.react.common.ReactConstants; 37 | import com.facebook.react.modules.core.DeviceEventManagerModule; 38 | 39 | import java.io.File; 40 | import java.lang.reflect.Array; 41 | import java.util.HashMap; 42 | import java.util.List; 43 | 44 | public class ApkManagerModule extends ReactContextBaseJavaModule implements LifecycleEventListener { 45 | 46 | final BroadcastReceiver apkInstallListener; 47 | String apkFile; 48 | 49 | private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() { 50 | 51 | @Override 52 | public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) { 53 | if (requestCode == 10086) { 54 | installApk(apkFile); 55 | } 56 | } 57 | }; 58 | 59 | public ApkManagerModule(ReactApplicationContext reactContext) { 60 | super(reactContext); 61 | reactContext.addActivityEventListener(mActivityEventListener); 62 | final ReactApplicationContext ctx = reactContext; 63 | 64 | apkInstallListener = new BroadcastReceiver() { 65 | @Override 66 | public void onReceive(Context context, Intent intent) { 67 | 68 | //接收替换广播 69 | if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) { 70 | //TODO 71 | Log.e("test", "替换成功"); 72 | if (ctx.hasActiveCatalystInstance()) { 73 | ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("apkManagerListener", "替换成功"); 74 | } 75 | } else if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) { 76 | //TODO 77 | //接收安装广播 78 | Log.e("test", "安装成功"); 79 | if (ctx.hasActiveCatalystInstance()) { 80 | ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("apkManagerListener", "安装成功"); 81 | } 82 | } else if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) { 83 | //TODO 84 | //接收卸载广播 85 | Log.e("test", "卸载成功"); 86 | if (ctx.hasActiveCatalystInstance()) { 87 | ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("apkManagerListener", "卸载成功"); 88 | } 89 | } 90 | } 91 | }; 92 | ctx.addLifecycleEventListener(this); 93 | } 94 | 95 | @Override 96 | public String getName() { 97 | return "ApkManagerModule"; 98 | } 99 | 100 | @ReactMethod 101 | public void uninstallApk(String packageName) { 102 | Uri uri = Uri.parse("package:" + packageName); 103 | Intent intent = new Intent(Intent.ACTION_DELETE, uri); 104 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 105 | getReactApplicationContext().startActivity(intent); 106 | } 107 | 108 | @ReactMethod 109 | public void installApk(String filePath) { 110 | apkFile = filePath; 111 | File apkFile = new File(filePath); 112 | Intent intent = new Intent(Intent.ACTION_VIEW); 113 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 114 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 115 | boolean b = getReactApplicationContext().getPackageManager().canRequestPackageInstalls(); 116 | 117 | if (!b) { 118 | //没有权限 119 | startInstallPermissionSettingActivity(); 120 | } else { 121 | intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 122 | Uri contentUri = FileProvider.getUriForFile( 123 | getCurrentActivity() 124 | , this.getReactApplicationContext().getPackageName() + ".provider" 125 | , apkFile); 126 | intent.setDataAndType(contentUri, "application/vnd.android.package-archive"); 127 | // Validate that the device can open the file 128 | PackageManager pm = getCurrentActivity().getPackageManager(); 129 | if (intent.resolveActivity(pm) != null) { 130 | this.getReactApplicationContext().startActivity(intent); 131 | } 132 | } 133 | } else { 134 | intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); 135 | getReactApplicationContext().startActivity(intent); 136 | } 137 | } 138 | 139 | @RequiresApi(api = Build.VERSION_CODES.O) 140 | private void startInstallPermissionSettingActivity() { 141 | Uri packageURI = Uri.parse("package:" + this.getReactApplicationContext().getPackageName()); 142 | //注意这个是8.0新API 143 | Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI); 144 | getReactApplicationContext().startActivityForResult(intent, 10086, null); 145 | } 146 | 147 | @ReactMethod 148 | public void isExpChannel(String packageName, String metaDataName, String channel, final Promise promise) { 149 | 150 | PackageManager manager = getReactApplicationContext().getPackageManager(); 151 | boolean installed = false; 152 | try { 153 | // 设置PackageManager.GET_META_DATA标识a位是必须的 154 | installed = isAppInstalled(packageName); 155 | if (installed) { 156 | PackageInfo info = manager.getPackageInfo(packageName, 157 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_META_DATA); 158 | String myChannel = info.applicationInfo.metaData.get(metaDataName).toString(); 159 | if (myChannel == null) { 160 | installed = false; 161 | } else if (!myChannel.equals(channel)) { 162 | installed = false; 163 | } 164 | } 165 | } catch (PackageManager.NameNotFoundException e) { 166 | installed = false; 167 | e.printStackTrace(); 168 | } 169 | promise.resolve(installed); 170 | } 171 | 172 | @ReactMethod 173 | public void isExpChannels(ReadableArray packageNames, ReadableArray metaDataNames, ReadableArray channels, final Promise promise) { 174 | 175 | PackageManager manager = getReactApplicationContext().getPackageManager(); 176 | WritableArray isInstallArray = new WritableNativeArray(); 177 | 178 | for (int i = 0; i < packageNames.size(); i ++) { 179 | boolean installed = false; 180 | try { 181 | // 设置PackageManager.GET_META_DATA标识a位是必须的 182 | String packageName = packageNames.getString(i); 183 | String metaDataName = metaDataNames.getString(i); 184 | String channel = channels.getString(i); 185 | installed = isAppInstalled(packageName); 186 | if (!installed) { 187 | PackageInfo info = manager.getPackageInfo(packageName, 188 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_META_DATA); 189 | String myChannel = info.applicationInfo.metaData.get(metaDataName).toString(); 190 | if (myChannel == null) { 191 | installed = false; 192 | } else if (!myChannel.equals(channel)) { 193 | installed = false; 194 | } 195 | isInstallArray.pushBoolean(installed); 196 | } 197 | } catch (PackageManager.NameNotFoundException e) { 198 | installed = false; 199 | isInstallArray.pushBoolean(installed); 200 | e.printStackTrace(); 201 | } 202 | } 203 | 204 | promise.resolve(isInstallArray); 205 | } 206 | 207 | @ReactMethod 208 | public void openApk(String packageName) { 209 | String package_name = packageName; 210 | PackageManager packageManager = getReactApplicationContext().getPackageManager(); 211 | Intent it = packageManager.getLaunchIntentForPackage(package_name); 212 | getReactApplicationContext().startActivity(it); 213 | } 214 | 215 | @ReactMethod 216 | private boolean isAppInstalled(String packageName, final Promise promise) { 217 | PackageManager pm = getReactApplicationContext().getPackageManager(); 218 | boolean installed = false; 219 | try { 220 | pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); 221 | installed = true; 222 | } catch (PackageManager.NameNotFoundException e) { 223 | installed = false; 224 | } 225 | promise.resolve(installed); 226 | return installed; 227 | } 228 | 229 | @ReactMethod 230 | private void isAppsInstalled(ReadableArray packageNames, final Promise promise) { 231 | PackageManager pm = getReactApplicationContext().getPackageManager(); 232 | WritableArray isInstallArray = new WritableNativeArray(); 233 | for (int i = 0; i < packageNames.size(); i ++) { 234 | boolean installed = false; 235 | try { 236 | String packageName = packageNames.getString(i); 237 | pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); 238 | installed = true; 239 | isInstallArray.pushBoolean(installed); 240 | } catch (PackageManager.NameNotFoundException e) { 241 | installed = false; 242 | isInstallArray.pushBoolean(installed); 243 | } 244 | } 245 | promise.resolve(isInstallArray); 246 | } 247 | 248 | private boolean isAppInstalled(String packageName) { 249 | PackageManager pm = getReactApplicationContext().getPackageManager(); 250 | boolean installed = false; 251 | try { 252 | pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); 253 | installed = true; 254 | } catch (PackageManager.NameNotFoundException e) { 255 | installed = false; 256 | } 257 | return installed; 258 | } 259 | 260 | @ReactMethod 261 | private void getAPKInformation(String apkFile, final Promise promise) { 262 | PackageManager pm = getReactApplicationContext().getPackageManager(); 263 | PackageInfo info = pm.getPackageArchiveInfo(apkFile, PackageManager.GET_ACTIVITIES); 264 | if(info != null){ 265 | WritableMap map = Arguments.createMap(); 266 | ApplicationInfo appInfo = info.applicationInfo; 267 | String appName = pm.getApplicationLabel(appInfo).toString(); 268 | String packageName = appInfo.packageName; // 得到安装包名称 269 | String versionName = info.versionName; // 得到版本信息 270 | int versionCode = info.versionCode; // 得到版本号 271 | int installLocation = info.installLocation; // 得到安装位置 272 | long firstInstallTime = info.firstInstallTime; // 得到首次安装时间 273 | long lastUpdateTime = info.lastUpdateTime; // 得到最后安装时间 274 | map.putString("appName", appName); 275 | map.putString("packageName", packageName); 276 | map.putString("versionName", versionName); 277 | map.putInt("versionCode", versionCode); 278 | map.putInt("installLocation", installLocation); 279 | map.putDouble("firstInstallTime", firstInstallTime); 280 | map.putDouble("lastUpdateTime", lastUpdateTime); 281 | promise.resolve(map); 282 | } else { 283 | promise.reject("400","Get ApkInfomation Fail"); 284 | } 285 | } 286 | 287 | @ReactMethod 288 | private void getAppInformation(String packageName, final Promise promise) { 289 | 290 | PackageManager manager = getReactApplicationContext().getPackageManager(); 291 | try { 292 | PackageInfo info = manager.getPackageInfo(packageName, 293 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_META_DATA); 294 | if(info != null){ 295 | WritableMap map = Arguments.createMap(); 296 | ApplicationInfo appInfo = info.applicationInfo; 297 | String appName = manager.getApplicationLabel(appInfo).toString(); 298 | String versionName = info.versionName; // 得到版本信息 299 | int versionCode = info.versionCode; // 得到版本号 300 | int installLocation = info.installLocation; // 得到安装位置 301 | long firstInstallTime = info.firstInstallTime; // 得到首次安装时间 302 | long lastUpdateTime = info.lastUpdateTime; // 得到最后安装时间 303 | map.putString("appName", appName); 304 | map.putString("packageName", packageName); 305 | map.putString("versionName", versionName); 306 | map.putInt("versionCode", versionCode); 307 | map.putInt("installLocation", installLocation); 308 | map.putDouble("firstInstallTime", firstInstallTime); 309 | map.putDouble("lastUpdateTime", lastUpdateTime); 310 | promise.resolve(map); 311 | } else { 312 | promise.reject("400","Get AppInfomation Fail"); 313 | } 314 | } catch (PackageManager.NameNotFoundException e) { 315 | promise.reject("400","Get AppInfomation Fail"); 316 | e.printStackTrace(); 317 | } 318 | 319 | } 320 | 321 | @ReactMethod 322 | private void getAPKMetaDataByKey(String apkFile, String key, final Promise promise) { 323 | PackageManager pm = getReactApplicationContext().getPackageManager(); 324 | PackageInfo info = pm.getPackageArchiveInfo(apkFile, PackageManager.GET_ACTIVITIES); 325 | if (info != null){ 326 | ApplicationInfo appInfo = info.applicationInfo; 327 | if (appInfo.metaData != null) { 328 | String metaData = appInfo.metaData.get(key).toString(); 329 | promise.resolve(metaData); 330 | } else { 331 | promise.reject("400","Not Found MetaData"); 332 | } 333 | } else { 334 | promise.reject("400","Not Found MetaData"); 335 | } 336 | } 337 | 338 | @ReactMethod 339 | private void getAppMetaDataByKey(String packageName, String key, final Promise promise) { 340 | PackageManager manager = getReactApplicationContext().getPackageManager(); 341 | try { 342 | PackageInfo info = manager.getPackageInfo(packageName, 343 | PackageManager.GET_CONFIGURATIONS | PackageManager.GET_META_DATA); 344 | if (info != null){ 345 | ApplicationInfo appInfo = info.applicationInfo; 346 | if (appInfo.metaData != null) { 347 | String metaData = appInfo.metaData.get(key).toString(); 348 | promise.resolve(metaData); 349 | } else { 350 | promise.reject("400","Not Found MetaData"); 351 | } 352 | } else { 353 | promise.reject("400","Not Found MetaData"); 354 | } 355 | } catch (PackageManager.NameNotFoundException e) { 356 | promise.reject("400","Not Found MetaData"); 357 | e.printStackTrace(); 358 | } 359 | } 360 | 361 | @ReactMethod 362 | private void getInstalledAppInfo(final Promise promise) { 363 | new Thread(){ 364 | @Override 365 | public void run() { 366 | super.run(); 367 | List packages = getReactApplicationContext().getPackageManager().getInstalledPackages(0); 368 | WritableArray arr = Arguments.createArray(); 369 | for( int i=0; i < packages.size(); i++ ) { 370 | WritableMap map = Arguments.createMap(); 371 | PackageInfo info = packages.get(i); 372 | ApplicationInfo appInfo = info.applicationInfo; 373 | String appName = getReactApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString(); 374 | String packageName = appInfo.packageName; // 得到安装包名称 375 | String versionName = info.versionName; // 得到版本信息 376 | int versionCode = info.versionCode; // 得到版本号 377 | int installLocation = info.installLocation; // 得到安装位置 378 | long firstInstallTime = info.firstInstallTime; // 得到首次安装时间 379 | long lastUpdateTime = info.lastUpdateTime; // 得到最后安装时间 380 | map.putString("appName", appName); 381 | map.putString("packageName", packageName); 382 | map.putString("versionName", versionName); 383 | map.putInt("versionCode", versionCode); 384 | map.putInt("installLocation", installLocation); 385 | map.putDouble("firstInstallTime", firstInstallTime); 386 | map.putDouble("lastUpdateTime", lastUpdateTime); 387 | arr.pushMap(map); 388 | } 389 | promise.resolve(arr); 390 | } 391 | }.start(); 392 | } 393 | 394 | // 注册监听   395 | private void registerSDCardListener(Activity activity) { 396 | IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); 397 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); 398 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); 399 | intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); 400 | intentFilter.addDataScheme("package"); 401 | activity.registerReceiver(apkInstallListener, intentFilter); 402 | } 403 | 404 | @Override 405 | public void onHostResume() { 406 | final Activity activity = getCurrentActivity(); 407 | if (activity == null) { 408 | FLog.e(ReactConstants.TAG, "no activity to register receiver"); 409 | return; 410 | } 411 | registerSDCardListener(activity); 412 | } 413 | 414 | @Override 415 | public void onHostPause() { 416 | 417 | } 418 | 419 | @Override 420 | public void onHostDestroy() { 421 | final Activity activity = getCurrentActivity(); 422 | if (activity == null) return; 423 | try 424 | { 425 | activity.unregisterReceiver(apkInstallListener); 426 | } 427 | catch (java.lang.IllegalArgumentException e) { 428 | FLog.e(ReactConstants.TAG, "receiver already unregistered", e); 429 | } 430 | } 431 | } 432 | --------------------------------------------------------------------------------