Chapter 59 Unity releases Android platform

In this chapter, we explain how to package and publish to the Android mobile phone platform. To build and run applications for Android, the Unity Android Build Support platform module must be installed. The Android Software Development Kit (SDK) and Native Development Kit (NDK) are also required to build and run the code on an Android device. By default, Unity installs the OpenJDK-based Java Development Kit.

When we use Unity Hub to install Unity Editor, we will be prompted whether to install the platform compilation library.

In the screenshot above, we found the "Android Build Support" check option, if our project needs to be released to the Android mobile phone platform, we need to check this item. Then, Unity will automatically download and install SDK, NDK and OpenJDK for us. Of course, it doesn’t matter if there is no choice during installation, we can install them through Unity Hub. We will not introduce the specific operation in detail.

Unity's platform compilation library directory is under "Unity Editor\2020.3.35f1c2\Editor\Data\PlaybackEngines"

Here we installed three platforms: Android, WebGL, Windows

We enter the "AndoridPlayer" directory to view

We can see NDK, OpenJDK, SDK three directories. Their version has some relationship to our Unity version. Since we installed the Unity2020 version, our NDK is version r19, our OpenJDK is version 1.8, and there are two versions 29 and 30 in the SDK.

If multiple Unity versions share Android SDK & NDK Tools and OpenJDK, you can specify a shared location in the Unity Preferences window. To do this, please select Edit -> Preferences -> External Tools in the menu bar, and enter the corresponding installation directory in the JDK, SDK and NDK fields.

After introducing the environment configuration for compiling Android applications, let's talk about some configuration information for compiling Android applications. AndroidManifest.xml is a necessary file in every android program, which contains the configuration information of the application. This file is a configuration file in XML format, its root tag is manifest, and many sub-tags under it are used to configure various information, such as application for configuration of the entire application, activity for configuration activities, and so on.

What is the function of AndroidManifest.xml?

1. Describe the package name of the application: Android devices distinguish different APPs according to the package name.

2. Describe the components in the application: such as Activity, Service, Provider, BroadcastReceiver and other components.

3. Describe the permissions that the application needs to access.

In addition, Android applications are generally built through the gradle tool. Google's official Android Studio development tool uses gradle, and our Unity also uses it. The tool generates a build.gradle file for each application. In this file, we will configure the package name applicationId of the application, which is equivalent to the id of the application. What we need to know is the configuration of compileSdkVersion, minSdkVersion and targetSdkVersion. These three configurations correspond to the version of the Android system. For the version of the Android system, you can see the list below.

System version SDK version Version name

Android 11.0      30              (Android 11)

Android 10.0      29              (Android Q)

Android 9.0        28               Pie (Android P)

Android 8.1        27               Oreo(Android O)

Android 8.0 26 Oreo (Android O) (Oreo)

Android 7.1 25 Nougat (Android N) (Nougat)

Android 7.0 24 Nougat (Android N) (Nougat)

Android 6.0 23 Marshmallow (Android M) (Marshmallow)

Android 5.1 22 Lollipop (Android L) (Lollipop)

Android 5.0 21 Lollipop (Android L) (Lollipop)

Android 4.4 19 KITKAT (Kit Kat Chocolate)

Android 11 is a new operating system version officially released by Google on September 9, 2020. So, why do you need to configure three versions to develop an Android application? Due to the frequent release of the Android system, the corresponding API also changes accordingly. For example: some APIs may be abandoned, some newly added APIs, and so on. Well, this creates a problem, how does our application run stably in many versions. In other words, how can our application program be compatible with these different versions of the Android system to the greatest extent.

First, compileSdkVersion specifies which version of the Android SDK Grandle will use to compile its own application, and the API level used in the application must not be higher than the version of the SDK used for compilation. Google officially recommends always compiling your own applications with the latest stable version of the SDK.

Second, minSdkVersion is the minimum system version requirement that the application can run. This application cannot be installed on Android systems lower than minSdkVersion. During the development phase, if you use an API lower than the minSdkVersion, the development tool will issue a warning. For the selection of minSdkVersion, we should look at the proportion of users using each API. Generally speaking, we adapt to 4.4 or above, that is, minSdkVersion=19, but we have to choose the corresponding version number according to our actual situation.

To put it bluntly, when we use the SDK API, we need to be between minSdkVersion and compileSdkVersion. However, if the mobile phone system we use is higher than compileSdkVersion, can't our APP be installed and run? Definitely not, but some behaviors may be different. This involves the setting of targetSdkVersion.

Finally, targetSdkVersion target SDK version. targetSdkVersion is slightly more difficult to understand than compileSdkVersion and minSdkVersion. When you set the targetSdkVersion, it means that you have fully tested the operation of your application on the target version. targetSdkVersion is the main means for Android system to provide forward compatibility. What does it mean? With the upgrade of the Android version, the behavior of one of its APIs may change, but in order to ensure that the behavior of our application is still compatible with the previous one. As long as the targetSdkVersion setting remains unchanged, even if the application is installed on a new Android system, its behavior will still maintain the behavior on the old system, thus ensuring the forward compatibility of the system to the old application. Of course, new systems (greater than targetSdkVersion) will exhibit new API behaviors. Therefore, as a developer, you should pay special attention to API changes when the Android system version is updated. Therefore, when we develop Android applications, we try to use some basic and stable APIs to implement functions, so that even an old version of the application can still run smoothly on a new Android device after a few years.

But for some newly added APIs, we must use them in the new Android version. For example, our current Android 9.0 (SDK=28) and Android 10 (SDK=29) have added 5G-related APIs. Our application wants to use 5G-related APIs. At this time, we only need to set compileSdkVersion to version 29, and the compilation will pass during the compilation phase. In addition, it should be noted that since this 5G API is newly added, it is necessary to judge that the current system version >= 29 to use the new API, otherwise it cannot be used (on Android systems lower than version 29).

Taken together, the relationship between the three is: minSdkVersion <= targetSdkVersion <= compileSdkVersion

And it is recommended that targetSdkVersion be the same as compileSdkVersion.

So, how to set compileSdkVersion, minSdkVersion, targetSdkVersion in Unity? We click on the menu: Edit -> Project Settings -> Player, select the Android platform, and then find it in the Other Settings -> Identification panel.

 

We found two project settings, minSdkVersion and targetSdkVersion, but no compileSdkVersion setting. Please note that the default targetSdkVersion = compileSdkVersion in Unity settings. According to the above screenshot, the minimum version is Android4.4 (SDK=19), and the target compilation version is "Automatic". That is, the highest version of the SDK we installed. As we mentioned before, Unity 2020 is installed with Android version 29 and 30 by default. Then, it is very obvious here that the target compiled version is version 30. That is, the latest Android version released by Google. We can understand the above content.

Next, we started to package and publish our "TouchDemo" project as an Android application.

The content of the scene in this 3D mode

This is the UI content in 2D mode.

In this project, we added a MoveScript script to the main camera.

Next, we double-click to open the MoveScript script and modify it as follows

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

public class MoveScript : MonoBehaviour
{

    // 立方体游戏对象
    public Transform cube;

    // 立方体移动速度
    private float speed = 2.0f;

    public void OnMoveTest(Vector2 v)
    {
        // 摇杆输入
        float horizontal = v.x;
        float vertical = v.y;

        // 物体位置变化
        Vector3 position = cube.position;
        position.x += speed * horizontal * Time.deltaTime;
        position.z += speed * vertical * Time.deltaTime;

        // 更新物体位置
        cube.position = position;
    }
}

After the modification, we return to the Inspector inspection panel of the main camera,

We drag the Cube in the Hierarchy layer panel to the Cube under the MoveScript script in the inspection panel shown above. To put it bluntly, we just want to use the analog stick to control the Cube to move. We can play to see the effect.

Next, we package the project under the Android platform. First, we click File -> Build Settings in the menu bar, and then select the "Android" platform on the left side of the pop-up window. If we have selected other platforms before, we need to click "Switch Platform" in the lower right corner to switch platforms.

 

After the platform is switched, we click the "Player Settings" button in the lower left corner

Here we set the company name, application name and launch icon.

When Unity packages the Android installation package, you can set the horizontal and vertical screen display of the game and whether to allow the rotation direction in the Resolution and Presentation -> Default Orientation option. Default Orientation: Auto Rotation.

All its options are as follows:

1. Portrait: vertical screen

2. Portrait Upside Down: vertical screen, phone upside down

3. Landscape Right: Horizontal screen, the screen is on the right side of the home button

4. Landscape Left: Horizontal screen, the screen is on the left of the home button (commonly used)

5. Auto Rotation: Automatically rotate the screen

Here, let's choose a Landscape Left.

Then we continue to expand "Other Settings"

Then find the Identification item

Here we only modify the package name of the Android application. Unity requires the package name to be in the following format: the package name must follow the convention "com.YourCompanyName.YourProductName" and can only contain alphanumeric and underscore characters. Among them, YourCompanyName and YourProductName are basically locked. Therefore, there is basically no need to change here. Finally, we close the current window, return to the Build Settings window, and click the "Build" button in the lower right corner to package and release.

Select a storage directory (E:\workspace\TouchDemo), name it "TouchDemo.apk", and click Save.

 

The package is starting to be released. After waiting for a while, the package will be completed. Let's go to "E:\workspace\TouchDemo" to check

We send the installation package of this APP to the mobile phone (you can also use the simulator to test) and install it on it.

 

 

 

 

This is the effect after running, you can use the "joystick" to control the movement of Cube.

The content involved in this course has been shared to Baidu Netdisk: https://pan.baidu.com/s/1e1jClK3MnN66GlxBmqoJWA?pwd=b2id

Guess you like

Origin blog.csdn.net/konkon2012/article/details/130654985