Install Apk inside Unity

Let's take a look at the effect first. . .
insert image description here

upper code

AppUpdate.cs

using System.Collections;
using System.IO;
using UnityEngine;
using System;
 
public class AppUpdate: MonoBehaviour
{
    
    
    AndroidJavaClass androidJavaClass;
    AndroidJavaObject androidJavaObject;
    AndroidJavaClass customToolClass;
    AndroidJavaObject customToolObject;
    public UnityEngine.UI.Image progress_image;
    public UnityEngine.UI.Text  progress_Text;
    // Use this for initialization
    void Start()
    {
    
    
#if UNITY_ANDROID && !UNITY_EDITOR 
        try
        {
    
    
            androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            //获取MainActivity的实例对象
            androidJavaObject = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
            customToolClass = new AndroidJavaClass("com.Unity.Tools.UTool"); //com.Unity.Tools 包名  UTool 类名
            customToolObject = customToolClass.CallStatic<AndroidJavaObject>("instance");
            customToolObject.Call("Init", androidJavaObject);
        }
        catch (Exception e)
        {
    
    
           
        }
 
        customToolClass = new AndroidJavaClass("com.Unity.Tools.UTool");
        customToolObject = new AndroidJavaObject("com.Unity.Tools.UTool");
        customToolObject = customToolClass.CallStatic<AndroidJavaObject>("instance");
        customToolObject.Call("Init", androidJavaObject);
#endif
        StartCoroutine(InstallApk());
    }
    public IEnumerator InstallApk()
    {
    
    
        WWW www = new WWW("http://47.98.156.83/demo.apk");
        while (!www.isDone)
        {
    
    
            Debug.Log("progress:" + www.progress);
            progress_image.fillAmount = www.progress;
            progress_Text.text = (www.progress * 100).ToString("F2")+"%";
            yield return 0;
        }
        if (www.isDone)
        {
    
    
            string path = Application.persistentDataPath + "/test.apk";
            File.WriteAllBytes(path, www.bytes);
            yield return new WaitForSeconds(1);
            customToolObject.Call("installApk", path);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
    
    
            Debug.Log("error:" + www.error);
            yield break;
        }
    }
}

Put the following UTool.java code into Unity's Plugins/Android/ folder
UTool.java

package com.Unity.Tools;
 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import java.io.File;
 
public  class UTool {
    
    
    private static UTool _instance;
    public static UTool instance()
    {
    
    
        if(null == _instance)
            _instance = new UTool();
        return _instance;
    }
    private Context context;
 
    public  void Init(Context context)
    {
    
    
        Toast.makeText(context, "init", Toast.LENGTH_LONG).show();
        this.context = context;
    }
   
    public  void installApk(String apkFullPath)
    {
    
    
        try
        {
    
    
            File file = new File(apkFullPath);
            if (null == file){
    
    
                return;
            }
            if (!file.exists()){
    
    
                return;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
 
            Uri apkUri =null;
            if(Build.VERSION.SDK_INT>=24)
            {
    
    
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                apkUri = FileProvider.getUriForFile(context, context.getPackageName()+".fileprovider", file);
                //intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            }else{
    
    
                apkUri = Uri.fromFile(file);
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            Toast.makeText(context, apkUri.getPath(), Toast.LENGTH_LONG).show();
            context.startActivity(intent);
        }
        catch (Exception e)
        {
    
    
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

The jar package android-support-v4.jar that android needs to use
is placed in the Plugins/Android/libs/ folder of Unity

The android file configuration AndroidManifest.xml
is placed in the Plugins/Android/ folder of Unity
and com.xxx.xxx is set as the package name of your project

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Unity.Tools">
  <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <application>
        <activity android:name="com.unity3d.player.UnityPlayerActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
        </activity>
		<provider
		android:name="android.support.v4.content.FileProvider"
		android:authorities="com.xxx.xxx.fileprovider"
		android:grantUriPermissions="true"
		android:exported="false">
		<meta-data
			android:name="android.support.FILE_PROVIDER_PATHS"
			android:resource="@xml/provider_paths"/>
		</provider>
    </application>
</manifest>

provider_paths.xml file configuration

Put it in the Plugins/Android/res/xml/ folder of Unity

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="publicDirc" path=""/>
</paths>

Project source code:

download.csdn.net/download/a1228267639/12040865

Project Apk:

download.csdn.net/download/a1228267639/12040844

Project v4 jar package

download.csdn.net/download/a1228267639/12040570

Reprinted from: https://blog.csdn.net/a1228267639/article/details/103613681

Guess you like

Origin blog.csdn.net/qq_40028144/article/details/113866076