Unable to install apk on android 7 programmatically in work profile mode (Unknown sources)

526 Views Asked by At

I'm trying to install apk programmatically in "Work Profile" mode (BYOD). But there is a problem which I cannot solve : No way to install an apk in android versions 7.0(24) and 7.1(25). A pop-up dialog appear with this message :

Your administrator doesn't allow installation of apps obtained from unknown sources

Voila how I do :

val uri = FileProvider.getUriForFile(context,authority,File(filePath))   
val openFileIntent = Intent(Intent.ACTION_VIEW)
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
openFileIntent.data = uri
context.startActivity(openFileIntent)

in Manifest :

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"
        tools:replace="android:resource" />
</provider>

And file_provider_paths.xml :

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>
1

There are 1 best solutions below

0
On BEST ANSWER

This is how I found out the solution :

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
        devicePolicyManager.setSecureSetting(
            componentName,
            Settings.Secure.INSTALL_NON_MARKET_APPS,
            "1"
        )
    } else {
        devicePolicyManager.addUserRestriction(componentName,
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
    }