Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't generate Android APK from Unity command line

When I run the following build script from commandline there are no errors in the build.log but also no APK is produced. Occasionally I am am able to get a build using the exact same command with no added or changed files (I have check the folder diff using Meld).

I seem to be more successful if I open and close unity but there are still no guarantees that it will build using command line. There is also added or changed files after closing unity.

It always works when running the build in unity through the GUI.

Windows command line:

START /WAIT "" "C:\Program Files\Unity\Editor\Unity.exe" -batchmode -quit -executeMethod BuildScript.BuildAndroid -logfile build.log

I have also tried with -nographics to no avail

BuildScript.cs:

public class EditorSetup {
  public static string AndroidSdkRoot {
    get { return EditorPrefs.GetString("AndroidSdkRoot"); }
    set { EditorPrefs.SetString("AndroidSdkRoot", value); }
  }

  public static string JdkRoot {
    get { return EditorPrefs.GetString("JdkPath"); }
    set { EditorPrefs.SetString("JdkPath", value); }
  }

  // This requires Unity 5.3 or later
  public static string AndroidNdkRoot {
    get { return EditorPrefs.GetString("AndroidNdkRoot"); }
    set { EditorPrefs.SetString("AndroidNdkRoot", value); }
  }
}

public class BuildScript {
    public static string AndroidTitle = "Android"

    [MenuItem("Custom Build/Build AndroidHeadsetClient")]
    public static void BuildAndroid()
    {
        EditorSetup.AndroidSdkRoot = Environment.GetEnvironmentVariable("ANDROID_SDK_HOME");
        EditorSetup.JdkRoot = Environment.GetEnvironmentVariable("JDK_HOME");
        Debug.Log("JDK_HOME: " + EditorSetup.JdkRoot.ToString());
        Debug.Log("ANDROID_SDK_HOME: " + EditorSetup.AndroidSdkRoot.ToString());
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = new[] { "Assets/Scenes/Main/" + AndroidTitle + ".unity" };
        buildPlayerOptions.locationPathName = "MyArtifact.apk";
        buildPlayerOptions.target = BuildTarget.Android;
        buildPlayerOptions.options = BuildOptions.None;
        BuildPipeline.BuildPlayer(buildPlayerOptions);
    }
}

File structure:

.
├── Assets
│   ├── Animations
│   │   ├── ...
│   ├── Animations.meta
│   ├── Animators
│   │   ├── ...
│   ├── Animators.meta
│   ├── AudioClips
│   │   ├── ...
│   ├── AudioClips.meta
│   ├── Data
│   │   ├── ...
│   ├── Data.meta
│   ├── Documents
│   │   ├── ...
│   ├── Documents.meta
│   ├── Editor
│   │   ├── BuildScript.cs
│   │   ├── BuildScript.cs.meta
│   │   ├── ...
└──...

Unity version: 5.6.1f1

Any help with this issue would be greatly appreciated.

like image 925
Crunchy234 Avatar asked Oct 29 '22 05:10

Crunchy234


1 Answers

[Fixed]

This could happen because of multiple issues, and the real issue here is that, Unity batchmode doesn't show proper error logs in the console or log file.

I removed the -batchmode option and ran the command. Now the command is executed with Unity Editor opened. It will now show any errors, either in the log or as a popup.

My Issue:

In my case I forgot to change some keystore parameter, and unity showed me an error popup.

like image 120
bthoppil Avatar answered Nov 13 '22 02:11

bthoppil