Guide tested with:
Unity - 2018.3.0f2
Android SDK - 28.0.3
If you are new to Android development you will need to make sure you have your development environment setup properly on your computer. As of Unity 2018.3 it comes installed with an embedded JDK but any version of Unity before this will need to install the JDK separately. In either case you need the Android SDK. Unity has a great guide on getting the Android environment setup here:
Creating the project
Open Unity or Unity Hub and create a new project.
Unity Hub new project window
After your project has loaded follow this link to import the Simple Mobile Placeholder project from the Unity Asset Store (a free sample project provided by Unity).
You can also find this package by opening the Asset Store tab in Unity and searching for Simple Mobile Placeholder.
Once you have found the package in the Asset Store click on Import to load the package into your new project. This will warn you that this could potentially overwrite some of your project settings and it is recommended to do this in an empty project unless you know what you are doing. Continuing through the warning message you will be shown a checklist for all of the items expected to be imported into your project.
For now make sure everything is selected and proceed to import the assets. After everything has been loaded into your project find the scene file Main and double-click to open it. You can play test the simple app by clicking the Play button in the Unity Editor.
It is a simple game where a spinning cube can change directions by tapping or clicking on it.
Setting up Unity
Now that we have our sample game ready to go we need to take care of a few setup items in Unity before we can create our first build. The first thing to do is make sure our Android and Java environments were setup properly and Unity knows where to find them. In Unity open Edit > Preferences > External Tools and make sure that both the SDK and JDK sections are filled in and pointing to the right locations.
Unity 2018.3 Preferences
Unity 2018.2 Preferences
Next we will open our Player settings menu under Edit > Project Settings > Player
Under the Other Settings tab you need to update the Package Name to something following the pattern indicated in the sample name (but not the same as the sample name)
As you release updates for your game, this is where you will update the version information for the .apk. There are many other settings that we could cover but for now let's stay focused on something minimal that gets the job done. If you want to know more about the various settings here is some documentation to help:
Building and Testing
After all of your settings are set you can proceed to your first build and run of the project. To be able to test your game the easiest way is to have an Android device connected to your computer with Developer Mode and USB debugging enabled. This will allow you to deploy directly to your device and enable faster testing and feedback.
The Unity Android Environment Setup Documentation has a section on setting up an Android device for this purpose.
Open File > Build Settings.
You will first need to add a scene to the Build. Unity scenes can be added or removed at will from a build, and one of the easiest ways in this case is to just click Add Open Scenes. If this did not add the Main scene to the Scenes In Build list you can manually add it by finding it in your Assets and dragging & dropping it into the list.
We also want to make sure we have Android selected as the target platform. We can leave all of the other settings default for now. If you want to deploy to your device directly make sure it is plugged in and select Build and Run. If everything was setup properly Unity will take a bit of time to compile and build the application, and then attempt to deploy to your device.
If everything was successful the game will launch automatically and you can test it out!
Signing the .apk
When you are ready to share your game with the world you can look into publishing your game on the Google Play store. One of the first steps to doing that is building a "signed" version of your game with Unity. To do that we will need a Key and a Keystore, which Unity can help us with. Open the Player Settings menu.
Edit > Project Settings > Player > Publish Settings
If you happen to have an existing Keystore you want to use, make sure to check the box for Use Existing Keystore. Otherwise:
- Check the box for Create a new keystore.
- Then Browse Keystore to select a location to save your new keystore.
- Enter and confirm the password for the new Keystore (6 character min.).
- After you have chosen a keystore name, location, and password the option to create a new Key under the Key section becomes available.
- Select Create a new key.
- The only required information is:
- Alias (Key name)
- One of the 6 remaining identifiers
- The only required information is:
- Fill out the information and click Create Key to finalize the key creation.
That's it! You can now switch between the default Unsigned(debug) key and the new signed key that we just created. When you build the game it will automatically use whatever key is selected in this menu.
Here is some more information on what app-signing is, why it happens, and how it works. Keep in mind that we are letting Unity handle all of our app-signing for us, and will not follow the process described in this documentation but it still contains a lot of useful information:
You will need to re-build your .apk after setting up your Keystore and Key to be sure that the .apk gets properly signed. Once it has been re-built and signed you are ready to publish your app to the Google Play Store!
Here is a guide on how you can do that:
Congratulations! I hope at this point you have been able to see how easy it can be to get a working sample of a game onto a device of your own, and can start building upon that into something bigger and better of your own.
This guide was based mainly on this guide from Unity updated with the newest Unity version and documentation. Many thanks to the original authors for an in-depth guide.