thanks for coming back to me.
@kbabilinski, i currently have this (not optimal) user experience that my customer has to go through:
(1) Within the unity application, the user has to "confirm" to install a .apk file that is available through an sas-url that I host.
(2) I then use Application.OpenUrl
to download the file to the clients device. This opens a browser window. The download progress is visible in the bottom left. After the download, the customer has to click on the file in the bottom left
(3) then the user gets prompted to install the apk
(4) and to open it if successful.
My goal: I would like the user to skip steps 2,3, and if possible 4.
@mrushton
Thanks for supporting this case, appreciate it.
To clarify, i would like to start the install process from within Unity, not through adb.
The attempt i made to install the package through intents looked like this:
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
// Create a Uri object for the APK file
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkFilePath);
AndroidJavaObject uri = uriClass.CallStatic<AndroidJavaObject>("fromFile", fileObj);
// For Android N and above, use FileProvider to get URI
AndroidJavaClass fileProviderClass = new AndroidJavaClass("androidx.core.content.FileProvider"); // THIS ONE DOES NOT WORK. Probably have to pass in a JAR file!
string authority = currentActivity.Call<string>("getPackageName") + ".provider";
uri = fileProviderClass.CallStatic<AndroidJavaObject>("getUriForFile", context, authority, fileObj);
// Grant temporary read permission to the content URI
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
int flagGrantReadUriPermission = intentClass.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");
currentActivity.Call("grantUriPermission", currentActivity.Call<string>("getPackageName"), uri, flagGrantReadUriPermission);
// Create an intent to install the APK
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", "android.intent.action.VIEW");
intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
intent.Call<AndroidJavaObject>("addFlags", 0x10000000); // FLAG_ACTIVITY_NEW_TASK
intent.Call<AndroidJavaObject>("addFlags", intent.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"));
// Start the activity
currentActivity.Call("startActivity", intent);
I realized that in Android the FileProvider is no longer available and the script errors out on line:
AndroidJavaClass fileProviderClass = new AndroidJavaClass("androidx.core.content.FileProvider");
(Run on ML2, Unity2023.2.2f1, Development build with script debugging)
Until now i tried to open the file, but i have not used REQUEST_INSTALL_PACKAGES
.
How would i approach that?