Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 605
mcExchange Avatar asked Sep 07 '25 04:09

mcExchange


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. The script has to be placed under "Assets/Editor" in order to be accessible from commandline:

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_TitleScene.unity",
            "Assets/MyGame/Scenes/MyGame_MainScene.unity",
            "Assets/MyGame/Scenes/MyGame_EpisodeScene.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" 
like image 128
mcExchange Avatar answered Sep 10 '25 13:09

mcExchange