Jump to content

How to install apk from code?


fbdbdfb
 Share

Recommended Posts

Apk downloads normally, but when installing starts, 2d system window pops up so i can't do anything. How it can be fixed? Help please

This is installing script.

        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject unityContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");

        string packageName = unityContext.Call<string>("getPackageName");
        string authority = packageName + ".provider";

        AndroidJavaClass intentObj = new AndroidJavaClass("android.content.Intent");
        string ACTION_VIEW = intentObj.GetStatic<string>("ACTION_VIEW");
        AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", ACTION_VIEW);


        int FLAG_ACTIVITY_NEW_TASK = intentObj.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK");
        int FLAG_GRANT_READ_URI_PERMISSION = intentObj.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION");

        AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);
        AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
        AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>("getUriForFile", unityContext, authority, fileObj);

        intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/vnd.android.package-archive");
        intent.Call<AndroidJavaObject>("addFlags", FLAG_ACTIVITY_NEW_TASK);
        intent.Call<AndroidJavaObject>("addFlags", FLAG_GRANT_READ_URI_PERMISSION);

        currentActivity.Call("startActivity", intent);

 

Link to comment
Share on other sites

What do you mean by 2D system window?
Can you share a screenshot?

Also, a question to you, I'm using the exact same code that you are, but the fileProdiver returned from 

AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");

is just returning null for me.
Could you maybe share your aar/plugin/manifest setup?

Link to comment
Share on other sites

On 2/19/2022 at 3:32 PM, Paximillian said:

What do you mean by 2D system window?
Can you share a screenshot?

Can't do screenshot now. It's just a window on black background, that asks permission to install apk. I can't click yes/no cause controllers don't work in this mode. 

I had the same problem but i solved it by using Permission Manager. In this case i need FLAG_GRANT_READ_URI_PERMISSION, so i can't use it.

On 2/19/2022 at 3:32 PM, Paximillian said:

Could you maybe share your aar/plugin/manifest setup?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools">
    <application android:icon="@drawable/app_icon"
                 android:label="@string/app_name"
                 android:theme="@style/Theme.WaveVR.Black"
                 android:requestLegacyExternalStorage="true"
				 tools:replace="android:theme">  <!--You can use your theme here.-->
        <activity android:name="com.htc.vr.unity.WVRUnityVRActivity"
                  android:label="@string/app_name"
				          android:configChanges="density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
                  android:enableVrMode="@string/wvr_vr_mode_component" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="com.htc.intent.category.VRAPP" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
            <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
        </activity>
      <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
      </provider>
    </application>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="vive.wave.vr.oem.data.OEMDataRead" />
    <uses-permission android:name="vive.wave.vr.oem.data.OEMDataWrite" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  
</manifest>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...