Here are the main ways to install an APK from a file path programmatically in Android:
Method 1: Using FileProvider (Recommended for Android 7.0+)
```kotlin
private fun installApk(apkFilePath: String) {
val file = File(apkFilePath)
val intent = Intent(Intent.ACTION_VIEW)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// For Android 7.0 and above, use FileProvider
val apkUri = FileProvider.getUriForFile(
this,
"${applicationContext.packageName}.fileprovider",
file
)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
} else {
// For older versions
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
```
Method 2: Using MediaStore (Alternative for Android 10+)
```kotlin
private fun installApkFromMediaStore(apkFilePath: String) {
val file = File(apkFilePath)
val contentUri = getContentUriForFile(file)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(contentUri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(intent)
}
```
Required Configuration
1. Add FileProvider to AndroidManifest.xml
```xml
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
```
2. Create file_paths.xml in res/xml/
```xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="." />
<files-path name="files" path="." />
<cache-path name="cache" path="." />
<!-- Add other paths as needed -->
</paths>
```
3. Required Permissions
```xml
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<!-- For Android 13+ -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
```
Complete Example with Permission Handling
```kotlin
class MainActivity : AppCompatActivity() {
companion object {
private const val REQUEST_INSTALL_PERMISSION = 1001
}
fun installApkWithPermissions(apkFilePath: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!packageManager.canRequestPackageInstalls()) {
// Request installation permission
val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).apply {
data = Uri.parse("package:$packageName")
}
startActivityForResult(intent, REQUEST_INSTALL_PERMISSION)
return
}
}
installApk(apkFilePath)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_INSTALL_PERMISSION) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
packageManager.canRequestPackageInstalls()) {
// Permission granted, retry installation
installApk(apkFilePath)
} else {
// Permission denied
Toast.makeText(this, "Install permission denied", Toast.LENGTH_SHORT).show()
}
}
}
private fun installApk(apkFilePath: String) {
try {
val file = File(apkFilePath)
if (!file.exists()) {
Toast.makeText(this, "APK file not found", Toast.LENGTH_SHORT).show()
return
}
val intent = Intent(Intent.ACTION_VIEW)
val apkUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
FileProvider.getUriForFile(
this,
"${packageName}.fileprovider",
file
)
} else {
Uri.fromFile(file)
}
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(this, "Installation failed: ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}
```
Important Notes:
1. Android 8.0+: Requires REQUEST_INSTALL_PACKAGES permission
2. Android 7.0+: Must use FileProvider for file sharing
3. File Location: The APK must be in a directory your app can access
4. User Consent: User will always see the installation prompt and must confirm
This approach will successfully launch the APK installation process from your app.