(Transfer) Unity export Android project

Code words are not easy, please indicate the source for reprinting
https://blog.csdn.net/newchenxf/article/details/121923158

Foreword
There are two ways to package a Unity game into an android apk, one is to compile the apk directly on Unity; the other is to export the android code from Unity, and then compile it into the AndroidStudio project. The first is very simple, this article will not expand, let’s talk about the second. This article is based on the Unity 2020.3.13f1c1 version, and different versions may be used slightly differently.

1 Export
First, the project needs to support both arm v7 and arm v8 architectures, otherwise it will fail to run on some models.
The method is as follows: File -> Build Settings, after opening the panel, click Player Settings in the lower left corner, and after it comes out, in the Configuration column, make the following modifications:

Once checked, close the window. Return to the Build Settings interface.

Select Export Project, then click Export in the lower right corner. After the pop-up window appears, select an export directory, and the export will start. Finally, a project that can be opened with AndroidStudio is generated, as follows:


2 AndroidStudio opens
the exported project, and AndroidStudio can directly open and run it.
Of course, there is another situation where you don't want to open its project directly and want to migrate manually, so how do you do it?

Copy the unityLibrary to your project
Copy some parameters of the exported gradle.properties to the gradle.properties of your project, especially unityStreamingAssets=.unity3d
Add a new module to settings.gradle: namely: add this line: include ':app','unityLibrary'
add the modules that need to rely on Unity, plus the dependency on unityLibrary: implementation project(path: ':unityLibrary')
copy the res/values ​​resources under the launcher module to your app module, or Go to unityLibrarys, otherwise Resources#NoFoundException may appear when running

If you migrate manually, the default activity is not Unity. You can add jump logic to your default activity (usually under the app module), and jump to UnityPlayerActivity after startup.
Finally compile and run.
3 Possible errors
3.1 When compiling, encounter Task :unityLibrary:BuildIl2CppTask FAILED run-detectors: unable to find an interpreter for ../il2cpp.exe
Reason: If you open it with the linux version of AndroidStudio, you will encounter the above error, The reason is that this il2cpp is in exe format and can only run on windows. Well, it is a bit helpless, and it does not support the AS project of the linux version.

3.2 armv7 and armv8 cannot be checked at the same time
This happens in the first step.
Reason:
One is that you did not choose IL2CPP in the first step. This is easy to solve.

Another way is that your Unity may have selected the PC, Mac... platform first, and then manually modified the Android platform. After switching, although it will help you download the Android-related SDK, it may not be complete.

So what you have to do is to download the full Android SDK.

As follows, Unity Hub, click 3 points, [Add Module]

Check it, then click Done. You can install the Android dependency library.

Of course, there is another extreme situation, that is, after opening, it shows that it has been downloaded. what to do? ? ? ? ? Don't be afraid, then you can reinstall a Unity version. At the beginning of the installation, fix the Android compilation dependencies, and then open your Unity project. In theory, it should be fine!

3.3 Check armv7 and armv8, click export, and the error "unity library missing for the selected architecture" appears.
Reason: Same as 3.2, the Android SDK is incomplete. The solution is the same as 3.2.
 

Guess you like

Origin blog.csdn.net/duyiqun/article/details/131375658