Hi everyone, does anybody knows how to install a local APK file from within Unity?
Until Android 8 came out I’ve been using this code (replacing “content://” with “file://”), but it’s not working any more:
// Get the main Activity and Context:
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
// Create path object to the apk file (Uri):
AndroidJavaClass uri = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject targetUri = uri.CallStatic<AndroidJavaObject>("parse", new object[] { "content://" + file });
// Create and set the Intent object to be executed:
AndroidJavaClass intent = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject promptInstall = new AndroidJavaObject("android.content.Intent", new object[] { intent.GetStatic<string>("ACTION_VIEW") });
promptInstall = promptInstall.Call<AndroidJavaObject>("setDataAndType", new object[] { targetUri, "application/vnd.android.package-archive" });
promptInstall = promptInstall.Call<AndroidJavaObject>("addFlags", new object[] { intent.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION") });
// Call the installation:
context.Call("startActivity", new object[] { promptInstall });
There is also a permission in the manifest:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
The “file” is the full path to the APK file, no matter if internal or external drive. The application has SDcard permission granted, so from within Unity I’m being able to verify that the APK file effectively exists in the provided location.
Unfortunately the permission seems has no effect, because the installer sends a “File not found” error when connected to Android Logcat, and there is no difference when deleting permission from manifest or from the source code (addFlags method).
If anyone have a solution, please let me know.