Simple command to build my Unity project for android via command line

Viewed 1404

What is the right command to build a Unity project as an Android apk?

Using this command I'm able to compile for Windows:

"C:\Program Files\Unity\Hub\Editor\2019.4.29f1\Editor\Unity.exe" 
  -batchmode 
  -nographics  
  -projectPath "C:\Users\MyUser\MyGitRepo\MyUnityProject" 
  -buildWindowsPlayer "C:\Users\MyUser\Desktop\MyGame.exe" 
  -logFile 
  "C:\Users\MyUser\Desktop\build.log" 
  -quit

(This is a one line command)

However now I want to do the same for Android. What to I need to change?

-buildAndroidPlayer

did not work for me...

Update
This website has an overview of command line options. There are options for anything but Android (eg -buildOSX64Player <pathname> etc). I can specify -buildTarget <name> where would be android. But I still dont get any apk. Also it does not say to specify an output path

1 Answers

There was no simple command for me. I had to add a Build Script that specifies all scenes which are part of the game:

using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class BuildScript 
{
    static void PerformBuild()
    {
        string[] defaultScene = { 
            "Assets/MyGame/Scenes/MyGame_World.unity",
            "Assets/MyGame/Scenes/MyGame_TitleScreen.unity",
            "Assets/MyGame/Scenes/MyGame_Character_Selection.unity",
            "Assets/MyGame/Scenes/MyGame_Episode_Content.unity",
            "Assets/MyGame/Scenes/MyGame_Episode_Selection.unity",
            "Assets/MyGame/Scenes/MyGame_Result.unity",
            "Assets/MyGame/Scenes/MyGame_Test.unity",
            "Assets/MyGame/Scenes/MyGame_Settings.unity",
            "Assets/MyGame/Scenes/MyGame_Mate_Selection.unity",
            };

        BuildPipeline.BuildPlayer(defaultScene, "MyGame.apk" ,
            BuildTarget.Android, BuildOptions.None);
    }

}

and build using

"C:\Program Files\Unity\Hub\Editor\2019.4.29f1\Editor\Unity.exe" -projectPath "path\to\my\Game" -executeMethod BuildScript.PerformBuild -logFile "MyGame\APKs\build.log" 
Related