Skip to content

Understanding Unity Build Output for Android

When developing mobile games or applications with Unity, building for the Android platform involves generating specific files and folders that are crucial for the deployment and functionality of your app on Android devices. This article delves into the components of Unity's Android build output, explaining their purposes and how they come together to create a functional Android application.


Overview of Android Build Files

When you build a Unity project for Android, the output can be one of the following:

  • APK (Android Package): A packaged application file that can be installed directly on Android devices.
  • AAB (Android App Bundle): A publishing format that includes all your app's compiled code and resources, but defers APK generation and signing to Google Play.

Additionally, you can choose to export your project as a Gradle project, which provides more flexibility for further customization using Android Studio.


Detailed Breakdown

APK (Android Package)

An APK is the final product typically used for sideloading or distribution outside the Google Play Store.

Key Components Inside an APK:

  • AndroidManifest.xml: Describes essential information about your app to the Android system.
  • classes.dex: Contains compiled Java/Kotlin code as Dalvik Executable format.
  • lib: Contains native libraries (.so files) for different CPU architectures (e.g., armeabi-v7a, arm64-v8a).
  • assets: Includes asset files packaged with your app.
  • res: Contains resources like images, layouts, and strings.
  • META-INF: Stores metadata about the APK, including signatures.

Libraries and Plugins

Unity includes several libraries and plugins in the build output to support various features.

Native Libraries (.so files)
  • Located under lib/<architecture>/
  • Include Unity engine code and other native plugins.
  • Architectures: armeabi-v7a, arm64-v8a, x86, x86_64
Plugins
  • Android Plugins: Located in Assets/Plugins/Android/
  • Can include additional JAR/AAR files for extended functionalities.
  • Integrated into the build via Gradle configurations.

Assets and Resources

Unity packages assets and resources needed for your game.

Assets Folder:

  • Contains Unity's serialized assets, scene data, and resource files.
  • Includes bin/Data with assets like textures, audio, and meshes.

Resource Files:

  • Standard Android resources in the res folder.
  • Includes layouts, drawables, and strings used by Java-based plugins.

Manifest Files

AndroidManifest.xml:

  • Declares app permissions, activities, services, and other components.
  • Unity generates a default manifest, which can be customized or merged with plugin manifests.

Customization:

  • Place a custom manifest at Assets/Plugins/Android/AndroidManifest.xml to override or merge with Unity's default manifest.
  • Ensure proper merging of manifests to avoid conflicts.

IL2CPP and Mono Scripting Backends

Unity supports two scripting backends for Android:

IL2CPP (Intermediate Language To C++)
  • Native Compilation: Converts C# scripts to C++ and then compiles to native code.
  • Performance: Generally faster execution and better protection of code.
  • Output: Generates native libraries (libil2cpp.so) for each CPU architecture.
Mono
  • Just-In-Time (JIT) Compilation: Uses a Mono virtual machine that compiles code at runtime.
  • Faster Build Times: Quicker builds during development due to less compilation overhead.
  • Output: Includes Mono runtime libraries and assemblies (.dll files in assets/bin/Data/Managed).

AAB (Android App Bundle)

An AAB is a publishing format that helps reduce app size by generating optimized APKs for each user's device configuration.

Advantages of AAB:

  • Dynamic Delivery: Allows modularization of features and on-demand downloads.
  • Size Optimization: Google Play generates device-specific APKs, reducing download size.

When building an AAB, you typically upload it to Google Play Console, and Google handles the generation and distribution of APKs.

Gradle Project Structure

If you choose to export your project as a Gradle project, Unity generates files compatible with Android Studio.

Structure:

  • /build.gradle: The root Gradle build file.
  • /app/: Contains application-specific configurations.
    • /src/main/: Source code and resources.
    • /libs: Libraries and dependencies.
  • /gradle/: Gradle wrapper files.
  • gradlew & gradlew.bat: Scripts to execute Gradle tasks.

Benefits:

  • Customization: Modify Gradle scripts for advanced build configurations.
  • Integration: Use Android Studio's tools for debugging and profiling.

Building Process Explained

Compilation and Packaging

  1. Script Compilation: C# scripts are compiled based on the selected scripting backend (IL2CPP or Mono).
  2. Asset Bundling: Assets are processed and serialized into formats suitable for the Android platform.
  3. Library Inclusion: Necessary native libraries and plugins are included.
  4. Manifest Generation: An AndroidManifest.xml file is generated or merged.
  5. APK/AAB Packaging: All components are packaged into an APK or AAB file using the Android build tools.

Code Stripping and Optimization

  • Managed Code Stripping: Removes unused code to reduce app size.
  • Engine Code Stripping: Excludes unnecessary parts of the Unity engine.

Configuration:

  • Adjust stripping levels in Player Settings under Publishing Settings.
  • Be cautious; aggressive stripping may remove code that is actually used via reflection.

Asset Bundling

  • Resources Folder: Assets in Assets/Resources/ are included in the build and can be loaded at runtime.
  • StreamingAssets Folder: Directly copied into the APK's assets folder for access via file APIs.
  • AssetBundles: Packages of assets that can be loaded dynamically, useful for downloadable content.

Optimizing Android Builds

Build Settings

  • Development Build: Uncheck for release versions to optimize performance.
  • Scripting Backend: Use IL2CPP for better performance and code obfuscation.
  • Target Architectures: Only include necessary CPU architectures to reduce size.

Reducing APK Size

  1. Texture Compression: Use appropriate texture compression formats (e.g., ASTC, ETC2).
  2. Audio Compression: Compress audio files and adjust quality settings.
  3. Enable Code Stripping: Remove unused scripts and engine features.
  4. Exclude Unused Assets: Ensure that only necessary assets are included in the build.
  5. Split APKs by Architecture: Create separate APKs for different CPU architectures.

Performance Tips

  • Optimized Code: Profile and optimize scripts to reduce CPU usage.
  • Memory Management: Manage memory usage to prevent crashes and improve performance.
  • Garbage Collection: Reduce allocations to minimize garbage collection overhead.
  • Graphics Settings: Adjust quality settings to balance performance and visual fidelity.

Common Challenges and Solutions

Dependency Management

Problem: Conflicts with multiple versions of the same library or missing dependencies.

Solution:

  • Gradle Dependencies: Use Gradle to manage dependencies and resolve conflicts.
  • Unity Play Services Resolver: Use this tool to manage Android library dependencies.

Compatibility Issues

Problem: App crashes or behaves unexpectedly on certain devices or Android versions.

Solution:

  • Testing: Test on a range of devices and Android versions.
  • Min SDK Version: Set an appropriate minimum SDK version in Player Settings.
  • Restricted APIs: Avoid using deprecated or restricted APIs.

Debugging and Profiling

Problem: Identifying performance bottlenecks or bugs in the app.

Solution:

  • Logcat: Use Android Logcat to view logs and debug messages.
  • Profiler: Use Unity's Profiler to analyze CPU, GPU, memory usage.
  • Debug Builds: Enable development build and script debugging for testing.
  • Android Studio Tools: Use tools like CPU Profiler, Memory Profiler for in-depth analysis.

Conclusion

Understanding the build output of Unity for Android is essential for optimizing your game or application, troubleshooting issues, and ensuring a smooth user experience. By familiarizing yourself with the structure and components of the APK/AAB files, the build process, and how Unity handles assets and code for Android, you can make informed decisions that enhance performance and reduce app size.

Key Takeaways:

  • APK vs. AAB: Choose the appropriate packaging format for your distribution needs.
  • Scripting Backend: Select the scripting backend that balances performance and build times.
  • Optimization: Use Unity's tools and settings to optimize asset sizes and application performance.
  • Testing: Regularly test on various devices to catch compatibility issues early.
  • Continuous Learning: Stay updated with Unity's latest features and best practices for Android development.

By understanding and utilizing the information provided in this article, you can enhance your Unity Android builds' efficiency, performance, and compatibility, ensuring a better experience for your users and a smoother development process.