Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build an APK on unity using a command on the terminal

I have a project on Unity3d (working on a mac) and I am trying to generate the android apk file from the command line. Is this doable?

Right now I have a PerformBuild.cs file inside Assets/Editor

and I call inside it:

BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);

However this is only generating the Android Project for it, and not the apk.

Can I directly generate the APK using a cs build script or will I have to generate the project, import it to eclipse and then build the apk?

Thank you

Additional information:

Here is the full method in my script

[UnityEditor.MenuItem("CUSTOM/Test Android Build Step")]
static void androidBuild ()
{
    Debug.Log("Command line build android version\n------------------\n------------------");

    string[] scenes = GetBuildScenes();
    string path = GetBuildPathAndroid();
    if(scenes == null || scenes.Length==0 || path == null)
        return;

    Debug.Log(string.Format("Path: \"{0}\"", path));
    for(int i=0; i<scenes.Length; ++i)
    {
        Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
    }

    Debug.Log("Starting Android Build!");
    BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);
    BuildPipeline.buil
}

and I call it from the command line using the following:

/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -executeMethod PerformBuild.androidBuild

and I have the android sdk setup and configured

like image 421
Y2theZ Avatar asked Oct 20 '22 07:10

Y2theZ


1 Answers

Just remove AcceptExternalModificationsToPlayer from the BuildOptions.

BuildOptions.AcceptExternalModificationsToPlayer

On Android, this setting will create a new Eclipse project. Existing Eclipse project setting changes will be discarded.

Source: http://docs.unity3d.com/ScriptReference/BuildOptions.AcceptExternalModificationsToPlayer.html

Ok, I must admit, it's not 100% clear from this, but this is the "switch" to change, to build directly an .apk file instead of creating an Android project.

So in your case, just change

BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);

to

BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);

You may want to take a look at all the different BuildOptions e.g. if you want to make your build debuggable (to be able to attach the MonoDevelop debugger to it => BuildOptions.Development | BuildOptions.AllowDebugging)

Source: http://docs.unity3d.com/ScriptReference/BuildOptions.html

like image 180
d4Rk Avatar answered Nov 12 '22 19:11

d4Rk