Android studio4.0 embedded UnityActivity

The easiest way to embed UnityActivity

My buddy, I've turned the signs on the Internet. I was dazzled by all kinds of postures. In the end, I think the wildflowers are still quite fragrant. Feel it by yourself!

First, you have to export the Unity Android project

Not much to say about exporting projects, just look at the pictures and write words! The Android environment in External Tools here is all manually imported with Unity Hub, there is no problem at all. Of course, you can also download the SDK, JDK, NDK and other things by yourself. If you don't torture for a long time, you can't figure it out. But buddy, I'm still verbose, we are the Unity3d program, it is recommended to directly use the Hub provided by the official father to install the Android environment.
Insert picture description here

Insert picture description here

I am using the Unity android project exported by Unity2019.3.4f. There are two modules in the unityLibrary directory:

  • launcher
  • unityLibrary

Unity officially stated that this is to completely strip out the Unity project, so that it can be processed again later. I only use unityLibrary here.

Special note: There is no gradle/wrapper/gradle-wrapper.properties file structure in this project. So don't look for it foolishly.

Insert picture description here

Second, create a new android studio project

Create a new android project, my version of android studio is 4.1.2, and the new one is completely Next——>Next. Of course, here you have to understand the most basic knowledge of android studio, create a new project, import modules, and module dependencies, right? Otherwise, let's take a look at the first hello world example of android sudio.

  1. Create a new project, open the Project view, and click File->New->ImportModule on the toolbar. Click here is File on the top toolbar.

  2. Import the module, here only the unityLibrary module needs to be imported, the launcher module does not need to be imported. Because we created a new android project ourselves, we only need to display unityActivity in the activity of the newly created project.
    Insert picture description here

  3. After the import is successful, select the app module, right-click to find the Open Module Settings setting, and set unityLibrary as a dependency of the app module.
    Insert picture description here

  4. Add the following code in MainActivity.java. Here is the simplest code, open UnityActivity in the default activity, don't tell me why, my Nima will be angry.
    Insert picture description here

  5. Click Sync Project with Gradle Files, and then proceed to build. Before building, you have to connect the emulator or mobile phone, or build an egg. The night god simulator I used here, version 7.0.0.6, is still very easy to use.

  6. The above direct build is definitely unsuccessful. After all, the configuration files of the android project exported by Unity and the android studio created by itself are still somewhat different, and there are still gaps in the concubine. Next, we started to solve the error, not much, please take the bag of melon seeds and continue to eat melons, continue to tease, continue to watch, I guess my buddy's hands are all cocooned, temperate.

Troubleshoot

  1. The following error will appear in the first build. Focusing on "java.lang.UnsatisfiedLinkError" is nothing more than missing some library references. Add the configuration information in the red box to the build.gradle of the app module. Of course, other basic configurations must be consistent,
    such as:
    compileSdkVersion 30
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName 1.0

2021-03-30 14:11:47.661 7154-7154/com.snoopy.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.snoopy.myapplication, PID: 7154
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 2019.3.4f1
Device model : HUAWEI LIO-AN00
Device fingerprint: HUAWEI/LIO-AN00/LIO-AN00:7.1.2/LIO-AN00/700201021:user/release-keys

Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.snoopy.myapplication-2/base.apk"],nativeLibraryDirectories=[/data/app/com.snoopy.myapplication-2/lib/x86, /system/lib, /vendor/lib]]] couldn't find "libmain.so"

Insert picture description here

  1. To modify the build.gradle configuration file, you need to click Sync, and then build. Waiting for the hard result! Oops, my Nima is wrong again. I read a lot of this mistake on the Internet and only know how to correct it, but I don't know what it means. If you know, you can attach it in the comment area. Thank you Chunfeng Pavilion for patronizing my Qianjin Pavilion!

com.snoopy.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.snoopy.myapplication, PID: 7728
java.lang.Error: FATAL EXCEPTION [main]
Unity version : 2019.3.4f1
Device model : HUAWEI LIO-AN00
Device fingerprint: HUAWEI/LIO-AN00/LIO-AN00:7.1.2/LIO-AN00/700201021:user/release-keys

Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snoopy.myapplication/com.unity3d.player.UnityPlayerActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0

Insert picture description here

  1. Finally, finally, ah ah ah, it succeeded.
    Insert picture description here

Testimonials

Finally finished writing, as a novice, it is still a bit of a headache to read what the big guys on the Internet write. So I sorted it out according to my own steps, and you can leave a message if you don't understand.

problem

1. Why add game_view_content_description to strings.xml to solve the second error?

Welcome to call! *************

Guess you like

Origin blog.csdn.net/zyxyuting/article/details/115321941