Last active
April 13, 2024 18:17
-
-
Save Steellgold/f1be9952d8ff3b66a2781a01bbb988fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NativeModules } from "react-native"; | |
const { ApkInstaller } = NativeModules; | |
export default ApkInstaller; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.author.appname | |
import android.content.Intent | |
import androidx.core.content.FileProvider | |
import com.facebook.react.bridge.ReactApplicationContext | |
import com.facebook.react.bridge.ReactContextBaseJavaModule | |
import com.facebook.react.bridge.ReactMethod | |
import java.io.File | |
class ApkInstallerModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { | |
override fun getName(): String { | |
return "ApkInstaller" | |
} | |
@ReactMethod | |
fun installApk(apkFilePath: String) { | |
val intent = Intent(Intent.ACTION_VIEW).apply { | |
val apkUri = FileProvider.getUriForFile( | |
reactApplicationContext, | |
"${BuildConfig.APPLICATION_ID}.provider", | |
File(apkFilePath) | |
) | |
setDataAndType(apkUri, "application/vnd.android.package-archive") | |
flags = Intent.FLAG_ACTIVITY_NEW_TASK | |
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) | |
} | |
reactApplicationContext.startActivity(intent) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.author.appname | |
import com.facebook.react.ReactPackage | |
import com.facebook.react.bridge.NativeModule | |
import com.facebook.react.bridge.ReactApplicationContext | |
import com.facebook.react.uimanager.ViewManager | |
import java.util.Collections.emptyList | |
class ApkInstallerPackage : ReactPackage { | |
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> { | |
return listOf(ApkInstallerModule(reactContext)) | |
} | |
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> { | |
return emptyList() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.author.appname | |
import android.app.Application | |
import com.facebook.react.PackageList | |
import com.facebook.react.ReactApplication | |
import com.facebook.react.ReactHost | |
import com.facebook.react.ReactNativeHost | |
import com.facebook.react.ReactPackage | |
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load | |
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost | |
import com.facebook.react.defaults.DefaultReactNativeHost | |
import com.facebook.react.flipper.ReactNativeFlipper | |
import com.facebook.soloader.SoLoader | |
class MainApplication : Application(), ReactApplication { | |
override val reactNativeHost: ReactNativeHost = | |
object : DefaultReactNativeHost(this) { | |
override fun getPackages(): List<ReactPackage> = | |
PackageList(this).packages.apply { | |
// Code added for initalize the ApkInstaller | |
add(ApkInstallerPackage()) | |
} | |
override fun getJSMainModuleName(): String = "index" | |
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG | |
override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED | |
override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED | |
} | |
override val reactHost: ReactHost | |
get() = getDefaultReactHost(this.applicationContext, reactNativeHost) | |
override fun onCreate() { | |
super.onCreate() | |
SoLoader.init(this, false) | |
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) { | |
// If you opted-in for the New Architecture, we load the native entry point for this app. | |
load() | |
} | |
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment