unity导入jar包真机运行闪退问题

unity导入jar包真机运行闪退问题

本人是unity小白,这次需要搞个apk自动安装的功能,跟着大佬的流程一步步做了,结果在真机一直闪退,最后在自己的反复琢磨下终于解决了问题,这就来向大家分享一下。
1.xml有关问题
之前因为是直接用了大佬的jar包 ,所以不是很懂,然后发现AndroidManifest.xml里的package 不能用com.unity3d.player。(要用这个应该要把这个包放到Plugins/Android/libs里面才行,应该也包括那些不使用jar包的方法。) 如果是用自己的jar包就要把package改成自己的,比如我的是"com.am1105.installapk"。


<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.am1105.installapk"
	android:installLocation="preferExternal"
	android:theme="@android:style/Theme.NoTitleBar"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="27" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

    <application
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:debuggable="false">
        <activity android:name="com.am1105.installapk.MainActivity"
                  android:label="@string/app_name">
            <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" />
            <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
        activity>
         <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.XGdavey.abc.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        provider>
        
        <uses-library android:name="com.ti.s3d" android:required="false" />
        
        <uses-library android:name="com.osterhoutgroup.api.ext" android:required="false" />
    application>
manifest>


2.xml注意点
如果是用自己的jar包,下面这个也要改。把com.unity3d.player改成自己的包名,包名不是unity里的,是你jar里的。

 

3.不闪退后引入无法跳转安装界面的问题
Plugins/Android/res/xml/目录下的filepaths.xml按大佬的教程是要在path内填路径的,然后就没法跳转安装页面,然后研究了一下发现其实这path不需要填,因为我这个c#脚本函数已经引入path了,这边给出自己的filepaths.xml和有关c#脚本,大家可以参考一下。
unity导入jar包真机运行闪退问题_第1张图片下面是我用的c#脚本

    private bool installApp(string apkPath)
    {
        bool success = true;
        loadingText.text = "开始安装";
        // GameObject.Find("TextDebug").GetComponent().text = "Installing App";
        try
        {
            //Get Activity then Context
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic("currentActivity");
            AndroidJavaObject unityContext = currentActivity.Call("getApplicationContext");

            //Get the package Name
            string packageName = unityContext.Call("getPackageName");
            string authority = packageName + ".fileprovider";

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


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

            //File fileObj = new File(String pathname);
            AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", apkPath);


            //FileProvider object that will be used to call it static function
            AndroidJavaClass fileProvider = new AndroidJavaClass("android.support.v4.content.FileProvider");
            //getUriForFile(Context context, String authority, File file)
            AndroidJavaObject uri = fileProvider.CallStatic("getUriForFile", unityContext, authority, fileObj);

            intent.Call("setDataAndType", uri, "application/vnd.android.package-archive");
            intent.Call("addFlags", FLAG_ACTIVITY_NEW_TASK);
            intent.Call("addFlags", FLAG_GRANT_READ_URI_PERMISSION);
            currentActivity.Call("startActivity", intent);
            //getText.text = "Success";  
            //GameObject.Find("TextDebug").GetComponent().text = "Success";
        }
        catch (System.Exception e)
        {
            //GameObject.Find("TextDebug").GetComponent().text = "Error: " + e.Message;
            success = false;
            loadingText.text = "安装错误!错误报告:"+ e.Message;
        }

        return success;
    }

4.结尾
因为是第一次写文章,有什么不足之处,请多多包含。

你可能感兴趣的:(unity,unity,apk,android)