I found snippet for Java. How can I write such a code in C# Unity?
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File("link to downloaded file")),"application/vnd.android.package-archive");
startActivity(intent);
You can build a jar/aar plugin and call it from C#. That's more easier to do.
Another solution is to use
AndroidJavaObject
andAndroidJavaClass
to do this directly without a plugin. Doing it withAndroidJavaObject
andAndroidJavaClass
requires lots of testing to get it right. Below is what I use to do that. It downloads an APK then installs it.First of all create a UI text called "TextDebug" so it you will see what's going on during the download/install. If you don't do this you must comment out or remove all the
GameObject.Find("TextDebug").GetComponent<Text>().text...
line of code.For Android API 24 and above, this requires a different code since the API changed. The C# code below is based on the this Java answer.
EDIT:
If you get the Exception:
You have to do few things.
1.Copy "android-support-v4.jar" from your "AndroidSDK/extras/android/support/v4/android-support-v4.jar" directory to your "UnityProject/Assets/Plugins/Android" directory.
2.Create a file called "AndroidManifest.xml" in your UnityProject/Assets/Plugins/Android directory and put the code below into it.
Make sure to replace "com.company.product" with your own package name. There are 2 instances where this appeared. You must replace both of them:
These are found in package="com.company.product" and android:authorities="com.company.product.fileprovider". Don't change or remove the "fileprovider" and don't change anything else.
Here is the "AndroidManifest.xml" file:
3.Create a new file called "provider_paths.xml" in your "UnityProject/Assets/Plugins/Android/res/xml" directory and put the code below in it. As you can see, you have to create a res and then an xml folder.
Make sure to replace "com.company.product" with your own package name. It only appeared once.
Here is what you should put into this "provider_paths.xml" file:
I just wanted to add an update to the fantastic answer given by @Programmer, to reflect changes to the Android SDK and Unity. I hope it can be useful to someone else. I am working with SDK version 26 and Unity 2017.3.1. I am new to almost all of this so please do correct me if I've missed or misunderstood anything!
You need to add android.permission.REQUEST_INSTALL_PACKAGES to your AndroidManifest.xml
Note that I've also changed the first instance of com.company.product to com.company.productsomethingelse
That should now build ok and work, although during the build process Unity is giving the warning OBSOLETE - Providing Android resources in Assets/Plugins/Android/res is deprecated, please move your resources to an AAR or an Android Library.. To get round this create a new zip file and put your AndroidManifest.xml at the top level of the zip. Then add the provider_paths.xml detailed by @Programmer into the zip in the folder structure res/xml/provider_paths.xml. Rename the zip to give it a .aar file extension and then drag it into your Unity project Assets/Plugins folder. The reason I changed the AndroidManifest.xml entry to com.company.productsomethingelse is that when I used com.company.product the Unity build process was throwing up a naming conflict. I think because the manifest is now in a separate aar it needs to have a distinct package name.