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_T
With later versions of Unity, it gets a bit more complicated. Assets/Plugins/Android/res got deprecated. And with Android 9.0 android.support.v4 got merged into androidx.core and everything got renamed and moved around. Fortunately Unity is a bit better about supporting Android plugins now, so with this solution you no longer have to add any jar or aar to your project.
Note that Android has a bunch of nag screens associated with this install process. There's a one-time accept screen for untrusted builds per app, and if you have google play on the device, play protect adds a bunch of screens that make your app look like malware. (Which is a good thing, as you should only be doing this stuff on kiosk-mode style devices you own, where you can turn off Play Protect.)
Adding to Programmer's answer. In Unity 2019 and 2020, you can compile a plugin directly in Unity, and you no longer need the jar file. In 2019, it needs to be in Assets/Plugins/Android and have a couple extra files. In 2020, the folder just needs the suffix .androidlib and can be located anywhere in Assets/.
package.androidlib
src
main
res
xml
provider_paths.xml
AndroidManifest.xml
build.gradle
AndroidManifest.xml (only for 2019)
project.properties (only for 2019)
provider_paths.xml: As explained by Programmer.
AndroidManifest.xml: is mainly there to give this new library a bundle ID. This should not match any other IDs; it can be anything and usually it's com.companyname.packagename. Note that it's within the main/ folder
We can also do the rest of the AndroidManifest.xml stuff here, because all AndroidManifest.xml files will get merged in the final APK nowadays. The FileProvider package has changed to androidx.core.content.FileProvider.
I've noticed you need android.permission.REQUEST_INSTALL_PACKAGES; but I'm not sure whether you need READ_EXTERNAL_STORAGE/WRITE_EXTERNAL_STORAGE like Kaushix suggests. (Maybe on certain devices? I think this is covered by Project Settings->Write Permission.)
build.gradle: This should probably match your project target and min SDK version. It's OK if it's less, but target needs to be at least 28 to use androidx.
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
}
For 2019, the extra AndroidManifest.xml and project.properties files purely exist to tell Unity to build this folder. In 2020, Unity's smart enough to not need them.
AndroidManifest.xml:
project.properties:
android.library=true
AndroidPostProcess.cs:
We also need an editor script to enable AndroidX support in the main gradle file, and turn on Jetifier, which should fix any other plugins compiled against the old android.support libraries.
using UnityEditor.Android;
public class AndroidPostProcess : IPostGenerateGradleAndroidProject
{
public int callbackOrder => 0;
public void OnPostGenerateGradleAndroidProject(string path)
{
string gradlePropertiesPath = path + "/gradle.properties";
string[] lines = File.ReadAllLines(gradlePropertiesPath);
StringBuilder builder = new StringBuilder();
foreach (string line in lines)
{
if (line.Contains("android.useAndroidX"))
{
continue;
}
if (line.Contains("android.enableJetifier"))
{
continue;
}
builder.AppendLine(line);
}
builder.AppendLine("android.useAndroidX=true");
builder.AppendLine("android.enableJetifier=true");
File.WriteAllText(gradlePropertiesPath, builder.ToString());
}
}