Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Android Il2CPP build eats so much disk space and build fail

Discussion in 'Android' started by optimise, Apr 23, 2021.

  1. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,030
    Hi @Yury-Habets. Seems like Android Il2CPP build eats so much disk space like 10 GB+ and build fails. Will this bug be fixed soon?

    And also I the log mention "WARNING: The option 'android.enableR8' is deprecated and should not be used anymore. It will be removed in a future version of the Android Gradle plugin, and will no longer allow you to disable R8." Will it fixed at new Unity version?

    One more thing I notice the root project directory it generate 2 weird files. What is that actually?

    upload_2021-4-23_12-56-20.png
     

    Attached Files:

    Last edited: Apr 23, 2021
  2. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,030
    Hi @Tomas1856. I keep getting the build fail error message when rolling out Development build with dots build configuration and build will fail. Seems like it's caused by java.lang.OutOfMemoryError. Sometimes I can build it successfully. It's kidn of random. Any idea how to fix it?
     
  3. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    I see you're using com.unity.platforms.android 0.10 version, that seems to have a bug where org.gradle.jvmargs isn't passed to gradle, thus not allocating enough memory. Is it possible for you to upgrade com.unity.platforms and friends? If not, try exporting a project instead and build from Android Studio.
     
  4. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,030
    @Tomas1856. I would like to get the newer com.unity.platforms version but seems like it's not publicly accessible. Is there anyway for me to access it? Btw seems weird why Release build is working properly? Did Release build and Develop build inside com.unity.platforms package are implemented different?
     
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Probably no :/
    They're not implemented differently, but it's possible that java allocated less memory, being that the build is smaller.
     
  6. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,030
    @Tomas1856. If that's the case, can u give me hotfix code file(s) for com.unity.platforms just enough to fix the bug?
     
  7. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Here's a file com.unity.platforms.android/Editor/Unity.Build.Android.Classic/NonIncremental/Steps/AndroidBuildGradleProject.cs

    Code (CSharp):
    1. using System;
    2. using System.Diagnostics;
    3. using System.IO;
    4. using System.Text;
    5. using System.Text.RegularExpressions;
    6. using Unity.Build.Classic;
    7. using Unity.Build.Classic.Private;
    8. using Unity.Build.Common;
    9. using UnityEngine;
    10.  
    11. #if UNITY_ANDROID
    12. using UnityEditor.Android;
    13. #endif
    14.  
    15. namespace Unity.Build.Android.Classic
    16. {
    17.     sealed class AndroidBuildGradleProject : BuildStepBase
    18.     {
    19.         public override bool IsEnabled(BuildContext context)
    20.         {
    21.             return context.GetComponentOrDefault<AndroidExportSettings>().ExportProject == false &&
    22.                  context.HasComponent<InstallInBuildFolder>() == false;
    23.         }
    24.  
    25.         private string GetJVMArgs(string gradleProjectPath)
    26.         {
    27.             // This value can be get from AndroidJavaTools.PreferredHeapSizeForJVM, but AndroidJavaTools is not accessible
    28.             // Instead get it from gradle.properties
    29.  
    30.             var gradleProperties = Path.Combine(gradleProjectPath, "gradle.properties");
    31.             if (!File.Exists(gradleProperties))
    32.                 return string.Empty;
    33.             var contents = File.ReadAllText(gradleProperties);
    34.             var regex = new Regex(@"org\.gradle\.jvmargs=(?<jvmargs>\S+)");
    35.             var result = regex.Match(contents);
    36.             if (result.Success)
    37.                 return result.Groups["jvmargs"].Value;
    38.             return string.Empty;
    39.         }
    40.  
    41.         private string GetJDKPath()
    42.         {
    43. #if UNITY_ANDROID
    44.             return AndroidExternalToolsSettings.jdkRootPath;
    45. #else
    46.             return string.Empty;
    47. #endif
    48.         }
    49.  
    50.         public override BuildResult Run(BuildContext context)
    51.         {
    52.             var exportSettings = context.GetComponentOrDefault<AndroidExportSettings>();
    53.             var isAppBundle = exportSettings.TargetType == AndroidTargetType.AndroidAppBundle;
    54.             var classicData = context.GetValue<ClassicSharedData>();
    55.             var nonIncrementalClassicData = context.GetValue<NonIncrementalClassicSharedData>();
    56.             var gradleJarFile = GradleTools.GetGradleLauncherJar(classicData.BuildToolsDirectory);
    57.             var gradleTask = GradleTools.GetGradleTask(classicData.DevelopmentPlayer, isAppBundle);
    58.             var gradleOuputDirectory = Path.Combine(nonIncrementalClassicData.TemporaryDirectory, "gradleOut");
    59.             var gradleFile = Path.Combine(gradleOuputDirectory, "build.gradle");
    60.             var jdkPath = GetJDKPath();
    61.             if (string.IsNullOrEmpty(jdkPath))
    62.                 throw new Exception("JDK path is empty, is active Editor platform set to Android?");
    63.  
    64.             var fileName = Path.Combine(jdkPath, "bin", "java" + (Application.platform == RuntimePlatform.WindowsEditor ? ".exe" : ""));
    65.             var args = string.Join(" ",
    66.                 new[]
    67.                 {
    68.                     "-classpath",
    69.                     $"\"{gradleJarFile}\"",
    70.                     "org.gradle.launcher.GradleMain",
    71.                     "-b",
    72.                      $"\"{gradleFile}\"",
    73.                 });
    74.  
    75.             // Classic Unity build pipeline passes this command line argument even though gradle.properties have the same entry, do the same here.
    76.             var jvmArgs = GetJVMArgs(gradleOuputDirectory);
    77.             if (!string.IsNullOrEmpty(jvmArgs))
    78.                 args += $" \"-Dorg.gradle.jvmargs={jvmArgs}\"";
    79.  
    80.             args += $" \"{gradleTask}\"";
    81.  
    82.  
    83.             Process process = new Process();
    84.             process.StartInfo.FileName = fileName;
    85.             process.StartInfo.Arguments = args;
    86.             process.StartInfo.UseShellExecute = false;
    87.             process.StartInfo.RedirectStandardOutput = true;
    88.             process.StartInfo.RedirectStandardError = true;
    89.             process.StartInfo.CreateNoWindow = true;
    90.             var output = new StringBuilder();
    91.             process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
    92.             {
    93.                 if (!string.IsNullOrEmpty(e.Data))
    94.                 {
    95.                     output.AppendLine(e.Data);
    96.                 }
    97.             });
    98.  
    99.             var error = new StringBuilder();
    100.             process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
    101.             {
    102.                 if (!string.IsNullOrEmpty(e.Data))
    103.                 {
    104.                     error.AppendLine(e.Data);
    105.                 }
    106.             });
    107.  
    108.             process.Start();
    109.             process.BeginOutputReadLine();
    110.             process.BeginErrorReadLine();
    111.             process.WaitForExit();
    112.  
    113.             BuildResult result;
    114.             if (process.ExitCode == 0)
    115.             {
    116.                 var outputPath = Path.Combine(gradleOuputDirectory, GradleTools.GetGradleOutputPath(classicData.DevelopmentPlayer, isAppBundle));
    117.                 var finalPath = Path.Combine(context.GetOutputBuildDirectory(), context.GetComponentOrDefault<GeneralSettings>().ProductName + Path.GetExtension(outputPath));
    118.                 File.Copy(outputPath, finalPath, true);
    119.  
    120.                 var artifact = context.GetOrCreateBuildArtifact<AndroidArtifact>();
    121.                 artifact.OutputTargetFile = new FileInfo(finalPath);
    122.  
    123.                 result = context.Success();
    124.             }
    125.             else
    126.             {
    127.                 result = context.Failure($"{fileName} {args}\n\n{output}\n{error}");
    128.             }
    129.  
    130.             process.Close();
    131.  
    132.             return result;
    133.         }
    134.     }
    135. }
    136.  
    The important line here is
    Code (CSharp):
    1. args += $" \"-Dorg.gradle.jvmargs={jvmArgs}\"";
    It tells gradle how memory to allocate. Hope that helps
     
    Hello_hxh likes this.
  8. Hello_hxh

    Hello_hxh

    Joined:
    Nov 24, 2017
    Posts:
    6
    It works, Thank you very much。And when will the new version be updated?
     
  9. Rob-Fireproof

    Rob-Fireproof

    Joined:
    Dec 18, 2012
    Posts:
    55
    Hi there,

    We're getting the same problem (java.lang.OutOfMemoryError when using IL2CPP in Android builds). We're on Unity 2021.1.28f1.

    I'm a bit confused by how to go about updating that package (or how to replace that .cs file, even). I don't have a com.unity.platforms.android folder in my project's PackageCache folder. The only thing called that on my PC is in C:\Users\Rob\AppData\Local\Unity\cache\npm\packages.unity.com\packages.unity.com, but that just contains a .cache.json file (which says that I'm on 0.10.0-preview.10).

    The file AndroidBuildGradleProject.cs doesn't seem to exist on my PC at all.

    Neither my manifest.json or packages-lock.json refer to that com.unity.platforms.android package.

    My build log also doesn't contain the lines that the OP's did referencing the com.unity.platforms.android package.

    @Tomas1856 Is this a different problem, or if not could you help me to upgrade/alter that package?

    Thanks!
    Rob.
     
  10. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    People above are using Dots with Android, I assume you don't use it, so you have a different problem.
    If you're not using Dots or Build Configurations, for you it might be enough, to go to Preferences->External Tools in Unity and increase Maximum JVM heap size, Mbytes, assuming you're producing apk or aab from Unity itself.
     
  11. Rob-Fireproof

    Rob-Fireproof

    Joined:
    Dec 18, 2012
    Posts:
    55
    Ah, that makes sense. Was getting mixed up between DOTS (which we don't use) and Burst (which we do). Hadn't seen that option in preferences before. I'll give it a go!
     
  12. Rob-Fireproof

    Rob-Fireproof

    Joined:
    Dec 18, 2012
    Posts:
    55