I am getting this error.I am new to android studio and i need to create this plugin for unity to install an apk at runtime
Error - Attempt to invoke virtual method \'and
The Contect from Java plugin is null
. If you need it, you have to send Unity's context to the Java plugin.
Add a another parameter that receives the Context
then use that Context from the parameter not the one from Android.content.Context.getApplicationContext()
.
Java:
public static String InstallApp(Context context, String ApkPath){
try {
errMessage = "test";
File toInstall = new File(ApkPath);
Uri apkUri = FileProvider.getUriForFile(context,
context.getPackageName() +
".fileprovider", toInstall);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
catch (Exception e){
errMessage = e.getMessage();
}
return errMessage;
}
C#:
Send the Unity Context
and the path you want to pass to the function.
void Install()
{
try
{
GameObject.Find("TextDebug").GetComponent().text = "Installing...";
//Get Unity Context
AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityClass.GetStatic("currentActivity");
AndroidJavaObject unityContext = unityActivity.Call("getApplicationContext");
AndroidJavaClass plugin = new AndroidJavaClass("com.example.unitypluginappinstall.PluginClass");
string result = plugin.CallStatic("InstallApp", unityContext, savePath);
}
catch (Exception e)
{
GameObject.Find("TextDebug").GetComponent().text = e.Message;
}
}
You may have other unrelated error but the Context
null problem should be gone.
EDIT:
If you get the Exception:
Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.packageItemInfo.loadXmlMetaData(android.content.pm.PackageManager.java.lang.String)'
You have to do few things.
1.Put the ".jar" Plugin your UnityProject/Assets/Plugins/Android directory.
2.Copy "android-support-v4.jar" from your "AndroidSDK/extras/android/support/v4/android-support-v4.jar" directory to your "UnityProject/Assets/Plugins/Android" directory.
3.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:
4.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:
That's it.