- Inside the Android build process
- Internal build system
- Gradle build system
- Exporting the project
- Texture compression
- Build or Build and Run
- Android environment setup
- 1. Install Android Build Support and the Android SDK & NDK tools
- 2. Enable USB debugging on your device
- ADB debugging for Chrome OS devices
- Connecting to your Chrome OS device
- Customizing the Android SDK & NDK Tools and OpenJDK installation
- Change the OpenJDK path
- Change the Android SDK Tools path
- Change the Android NDK path
- Updating the Android SDK Target API
Inside the Android build process
There are two Android build process output types in Unity: the output package (APK) and the exported project. An APK is automatically deployed to your device if you select Build and Run. Alternatively, you can use Android Debug Bridge (ADB) to deploy the APK manually after building. For the full list of ADB commands, refer to the Android Debug Bridge section of the Android Developer documentation.
The output package includes APK expansion files (OBB) if the Split Application Binary option is selected in the Unity Editor’s Player Settings window. For more details on OBB files, refer to the OBB Support section of the Unity Manual
Unity supports two Android build systems: Gradle and Internal. Unity exports projects in two formats: ADT (Google Android project) and Gradle.
Internal build system
The Internal build system creates an APK by invoking Android SDK utilities in a specific order. Unity automatically performs a number of steps to create the APK, including:
Preparing and building the Unity Assets
Processing the plug-ins
Splitting the resources into the parts that go to the APK and the OBB, if Split Application Binary is selected
Building the Android resources using the AAPT utility
Generating the Android manifest and merging the library manifests into it
Compiling the Java code into the Dalvik Executable format (DEX)
Building the IL2CPP library, if IL2CPP Scripting Backend is selected
Building and optimizing the APK and OBB packages
For more information about OBB files, see Unity documentation on OBB Support. For more information about the Android Manifest, see Unity Android Manifest documentation.
Gradle build system
The Gradle build system uses Gradle to build an APK or export a project in Gradle format, which can then be imported to Android Studio. For more details, see Unity documentation on Gradle for Android.
When you select this build system, Unity goes through the same steps as the Internal build system excluding resource compilation with AAPT and running DEX. Unity then generates the build.gradle file (along with the other required configuration files) and invokes the Gradle executable, passing it the task name and the working directory. The APK is then built by Gradle.
Exporting the project
For more control over the build pipeline, or to make changes that Unity does not normally allow (for example, fine tuning the manifest files that are automatically generated by Unity), you must export your project for use with external tools.
Unity supports exporting projects in two formats:
Gradle project — Gradle projects are the recommended export format when exporting into Android Studio.
Google Android project (also known as ADT or Eclipse projects) — ADT projects can be imported into Eclipse or Android Studio (importing into Android converts the project to Gradle). This format is outdated and is no longer supported by Google.
To export the project:
Select Gradle or ADT in the Build Settings window (and check the Export Project checkbox if you have selected Gradle).
Click Export and select the destination folder.
When the export has finished, open Android Studio and import your project. For more information on importing projects to Android Studio, refer to the Migrate to Android Studio section of the Android Developer documentation.
Texture compression
In the Build Settings window, there is a Texture Compression option. By default, Unity uses the ETC texture format for textures that don’t have individual texture format overrides (see Unity documentaton on Textures for more information).
When building an APK to target specific hardware architecture, use the Texture Compression option to override this default behavior. Any texture that is not set to be compressed is left alone — only textures using a compressed texture format use the format selected in the Texture Compression option.
To make sure the app is only deployed on devices which support the selected texture compression, Unity edits the Android Manifest to include tags that match the particular format selected. This ensures that the Google Play Store filtering mechanism will only serve the app to devices with appropriate graphics hardware.
Build or Build and Run
The Build Settings window offers two options: Build and Build and Run. When building, the output packages (APK and OBB, if enabled) are saved to the path that you select. You can publish these packages to the Google Play Store, or install them on your device manually with the help of ADB . For further information about installing apps manually, refer to the Running Your App section of the Android Developer documentation.
Selecting Build and Run saves the output packages to the file path you specify, while also installing your app on the Android device connected to your computer.
If the Split Application Binary option is enabled, the OBB file is pushed to the correct location on your device. If Development Build is checked, Unity also sets up a profiler tunnel and enables CheckJNI. After that, the app is launched.
Tip: Once you specify the output path for the packages, use the Ctrl+B (Windows) or Cmd+B (OSX) keyboard shortcut to Build and Run using the saved output path.
Источник
Android environment setup
To build and run for Android, you must install the Unity Android Build Support platform module. You also need to install the Android Software Development Kit (SDK) and the Native Development Kit (NDK) to build and run any code on your Android device. By default, Unity installs a Java Development Kit based on OpenJDK.
Note: Unity supports Android 4.4 “KitKat” and above. See AndroidSdkVersions for details.
1. Install Android Build Support and the Android SDK & NDK tools
Use the Unity Hub to install Android Build Support and the required dependencies
See in Glossary : Android SDK & NDK tools, and OpenJDK.
Add Android modules
You can install Android Build Support, the Android SDK & NDK tools and OpenJDK when you install the Unity Editor, or add them at a later time.
For information on adding the Android modules:
If you are using a 2018 version of Unity, see the Unity 2018.4 documentation for information on manually installing these dependencies.
2. Enable USB debugging on your device
To enable USB debugging, you must enable Developer options on your device. To do this, find the build number in your device’s Settings menu. The location of the build number varies between devices; for stock Android, it’s usually Settings > About phone > Build number. For specific information on your device and Android version, refer to your hardware manufacturer.
After you navigate to the build number using the instructions above, tap on the build number seven times. A pop-up notification saying “You are now X steps away from being a developer” appears, with “X” being a number that counts down with every additional tap. On the seventh tap, Developer options are unlocked.
Note: On Android versions prior to 4.2 (Jelly Bean), the Developer options are enabled by default.
Go to Settings > Developer options (or, if this does not work, on some devices the path is Settings > System > Developer options), and check the USB debugging checkbox. Android now enters debug mode when it is connected to a computer via USB.
Connect your device to your computer using a USB cable. If you are developing on a Windows computer, you might need to install a device-specific USB driver. See the manufacturer website for your device for additional information.
The setup process differs for Windows and macOS and is explained in detail on the Android developer website. For more information on connecting your Android device to the SDK, refer to the Run Your App section of the Android Developer documentation.
ADB debugging for Chrome OS devices
For information on how to set up your development environment for Chrome OS devices, and enable ADB An Android Debug Bridge (ADB). You can use an ADB to deploy an Android package (APK) manually after building. More info
See in Glossary debugging, see Google’s documentation on Chrome OS Developer Environments.
Connecting to your Chrome OS device
Before you can deploy to your device you need to manually connect with ADB via the device’s IP address.
To begin you need the IP address. Open up the Settings app and choose Network in the sidebar. Next click on the active network interface.
If the interface is wireless then next choose the SSID that you are connected to. In the details you will see your IP address. Make a note of it.
If you are using a wired connection your IP address will be shown on the first details page.
Now we are ready to connect. For the sake of this example let’s say that the device’s IP is 192.168.0.100. In a shell or terminal run the following command
adb connect 192.168.0.100
If the connection was successful you will see a message such as connected to 192.168.0.65:5555 . And adb devices should verify that the device is connected. List of devices attached 192.168.0.65:5555 device
From now on you may run ADB commands to target the device just as you would over a USB connection.
Note: If your device or host machine goes to sleep or loses network connectivity you may need to reconnect.
Customizing the Android SDK & NDK Tools and OpenJDK installation
Unity recommends that you use the Unity Hub to install Android SDK & NDK tools, to ensure that you receive the correct versions and configuration. Unity installs Android SDK & NDK Tools and OpenJDK respectively in the SDK, NDK and OpenJDK folders under /Unity/Hub/Editor/[EditorVersion]/Editor/Data/PlaybackEngines/AndroidPlayer/.
If you have multiple versions of Unity with the same required dependencies (be sure to check System requirements for the latest) and you want to avoid duplicating the installation of Android SDK & NDK Tools and OpenJDK, you can specify a shared location in the Unity Preferences window. To do this, go to Preferences > External tools and enter the directory paths in the SDK and NDK fields:
Preferences window showing external tools settings for Android
Warning: Unity does not officially support versions of the OpenJDK, SDK, or NDK other than the ones it supplies.
To change the OpenJDK, SDK Tools, or NDK that Unity uses to build Android apps:
- Open the Project.
- Open the Preferences window (Windows and Linux: Edit >Preferences; macOS: Unity >Preferences).
- In the left navigation column, select External Tools.
Change the OpenJDK path
- Uncheck JDK Installed with Unity (recommended).
- In the JDK field, enter the path to the JDK installation folder, or use the Browse button to locate it.
Change the Android SDK Tools path
- Uncheck Android SDK Tools Installed with Unity (recommended).
- In the SDK field, enter the path to the SDK installation folder, or use the Browse button to locate it.
Unity works with the most recent version of the Android SDK available at the time of the Unity version release.
Change the Android NDK path
- Uncheck Android NDK Installed with Unity (recommended).
- In the NDK field, enter the path to the NDK installation folder, or use the Browse button to locate it.
Each version of Unity requires a specific version of the Android NDK to be installed:
Unity version | NDK version |
---|---|
2018.4 LTS | r16b |
2019.4 LTS | r19 |
2020.3 LTS | r19 |
See the System requirements page for a complete list of requirements.
Updating the Android SDK Target API
Unity Hub installs the latest version of the Android SDK Target API required by Google Play.
If you need to use a more recent version, you can change the Target API from the Target API Level field in the Player Settings window (menu: Edit > Project Settings > Player, then select the Android platform). You can find the Target API Level option in the Other Settings > Identification section.
Selecting a target API for the Android SDK
After you select an option other than the default, Unity prompts you to update the Android SDK API. You can choose to either:
- Update the Android SDK
- Continue to use the highest installed version of the Android SDK
Note: If you select an older version of the Target API, the Unity Android SDK Updater will not be able to perform the update and will give you this message:
Android SDK does not include your Target SDK of (version). Please use the Android SDK Manager to install your target SDK version. Restart Unity after SDK installation for the changes to take effect.
In this case, to update the Android SDK Target API, you must use the Android sdkmanager from either Android Studio or the command line tool. Regardless of the method you chose, make sure to select the correct Android SDK folder for Unity in the Edit > Preferences > External Tools window.
On Windows, if the Unity Editor is installed in the default folder ( /Program Files/ ), you must run the sdkmanager with elevated privilege (Run as Administrator) to perform the update.
Источник