Troubleshooting Android development
Reporting crash bugs under Android

Inside the Android build process

The Android application build process has two steps:

  1. It generates the application package (.apk file) with all the required libraries and serialized Assets.

  2. The application package is deployed to the actual device.

Go to File > Build Settings… or File > Build & Run to open the Build Settings dialog box.

In the Build Settings dialog box, select Build And Run to perform both steps. Note that typing Cmd+B on your keyboard invokes this automatic build and run process, with the most recently used file as the build target.

If you select Build in the Build Settings dialog box, Unity only does step 1.

When you first try to build an Android project, you are asked the location of the Android SDK that is required to build and install your Android application on the device. You can change this setting later in via the Unity Preferences dialog box (menu: Unity > Preferences).

When you build your app to the Android device, check the USB Debugging and Allow mock locations checkboxes under Device Settings.

Run the adb devices command (which is in your Android SDK/platform-tools folder), so that the operating system can see your device. This works both for Mac and Windows.

Unity builds an application archive (.apk file) for you and installs it on the connected device.

NOTE: In some cases your application cannot automatically start (for example on iPhone), so you need to unlock the screen. In rare cases, you need to find the newly installed application in the menu.

Texture compression

In the Build Settings dialog box, there is a Texture Compression option. By default, Unity uses the ETC1/RGBA16 texture format for textures that don’t have individual texture format overrides (see Texture 2D / Per-Platform Overrides).

If you want to build an application archive (an .apk file) targeting a specific hardware architecture, use the Texture Compression option to override this default behavior. Any texture that is set to not 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 application is only deployed on devices which support the selected texture compression, Unity edits the AndroidManifest to include tags matching the particular format selected. This enables the Android Market filtering mechanism to only serve the application to devices with the appropriate graphics hardware.

Permissions automatically added to the build manifest

Unity automatically adds certain permissions to the manifest. The logic is defined in the file PlatformDependent/AndroidPlayer/Editor/Managed/PostProcessor/Tasks/GenerateManifest.cs :: SetPermissionAttributes. For example, if your script code references Application.internetReachability, Unity automatically adds android.permission.ACCESS_NETWORK_STATE to the manifest. You can edit GenerateManifest.cs to remove it.

The following is the contents of /GenerateManifest.cs :: SetPermissionAttributes:

private void SetPermissionAttributes(PostProcessorContext context,

AndroidManifest manifestXML,

AssemblyReferenceChecker checker)

{

// Add internet permission if it's necessary

if (_developmentPlayer || PlayerSettings.Android.forceInternetPermission || doesReferenceNetworkClasses(checker))

manifestXML.AddUsesPermission("android.permission.INTERNET");


if (checker.HasReferenceToMethod("UnityEngine.Handheld::Vibrate"))

manifestXML.AddUsesPermission("android.permission.VIBRATE");


if (checker.HasReferenceToMethod("UnityEngine.iPhoneSettings::get_internetReachability")

|| checker.HasReferenceToMethod("UnityEngine.Application::get_internetReachability"))

manifestXML.AddUsesPermission("android.permission.ACCESS_NETWORK_STATE");


if (checker.HasReferenceToMethod("UnityEngine.Input::get_location")

|| checker.HasReferenceToMethod("UnityEngine.iPhoneInput::get_lastLocation")

|| checker.HasReferenceToMethod("UnityEngine.iPhoneSettings::get_locationServiceStatus")

|| checker.HasReferenceToMethod("UnityEngine.iPhoneSettings::get_locationServiceEnabledByUser")

|| checker.HasReferenceToMethod("UnityEngine.iPhoneSettings::StartLocationServiceUpdates")

|| checker.HasReferenceToMethod("UnityEngine.iPhoneSettings::StopLocationServiceUpdates"))

{

manifestXML.AddUsesPermission("android.permission.ACCESS_FINE_LOCATION");

manifestXML.AddUsesFeature("android.hardware.location.gps", false /*encourage gps, but don't require it*/);

// This is an implied feature, make it not required to support Android TV

manifestXML.AddUsesFeature("android.hardware.location", false);

}


if (checker.HasReferenceToType("UnityEngine.WebCamTexture"))

{

manifestXML.AddUsesPermission("android.permission.CAMERA");

// By default we don't require any camera since a WebCamTexture may not be a crucial part of the app.

// We need to explicitly say so, since CAMERA otherwise implicitly marks camera and autofocus as required.

manifestXML.AddUsesFeature("android.hardware.camera", false);

manifestXML.AddUsesFeature("android.hardware.camera.autofocus", false);

manifestXML.AddUsesFeature("android.hardware.camera.front", false);

}

Using Google Android project

To generates a project which can be opened by Android Studio, follow the steps below:

  1. Download Android Studio.
  2. Export the project from Unity with the Google Android project checkbox selected.
  3. Open Android Studio:
    • Go to File > Import Project. Choose a path (For example: If your project was exported to C:\MyProjects, pick C:\MyProjects\<Product Name\> path).
    • Pick Import Destination Folder
    • If it asks you to reload a project in the Language Level Changed dialog box, choose yes.

Notes: If you want to debug your application, you must set android:debuggable="true" in AndroidManifest.xml. Don’t forget to set it to false once you’re done.

Troubleshooting Android development
Reporting crash bugs under Android