Some pits in the interaction between Unity and Android Studio

 A specific tutorial on the combination of the two: Simple interaction between Unity and AndroidStuido

I personally think this tutorial is better

Here are some pitfalls that exist here:

1. About unity's calsses.jar and UnityPlayerActivity.java ​Original link: Interaction between Unity and Android Studio_Shushu did you learn today's blog-CSDN Blog

2. In the exported jar file, UnityplayerActivity.class and BuildConfig.class need to be deleted. Otherwise, an error will be reported when unity is exported.

3. The package name in the Unity player settings is consistent with the AndroidManifest.xml, as for the company name and product name may not be the same (does not affect )

4. How to call the function in the jar package in the unity c# script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public Text test1;//测试文本框
    public Button btn1;

    public AndroidJavaClass jc;
    public AndroidJavaObject jo;

    private void Awake()
    {
        //固定写法
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        //按钮事件
        btn1.onClick.AddListener(TEST);
    }

    public void TEST()
    {
        //以string类型为例
        string str1 = jo.Call<string>("test");//jar包内存在函数test,返回字符串"test"
        test1.text = str1;//文本框内容更新为接收到的字符串
    }
}

5. If there are dependent libraries imported in the jar package, there may be cases where the dependent libraries are missing after packaging, then you need to configure mainTemplate.gradle in unity.

Specific operation reference link: https://blog.csdn.net/weixin_43677968/article/details/106691682?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-4-106691682 .pc_agg_new_rank&utm_term=jar%E5%8C%85+unity+v4+%E4%BE%9D%E8%B5%96&spm=1000.2123.3001.4430

Tip: If something unexpected happens during operation after exporting the apk, you can use the adb command logcat to find the error. It is recommended to run it under the terminal in Android studio. Ctrl+F can find the package name and quickly locate it.

Adb configuration reference: ADB configuration debugging

Guess you like

Origin blog.csdn.net/Clscx/article/details/125473044