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

Question Remove IL2CPP flags from Android build.gradle file

Discussion in 'Android' started by Wepner-PD, Jun 27, 2022.

  1. Wepner-PD

    Wepner-PD

    Joined:
    Jul 13, 2018
    Posts:
    32
    Hello everyone,

    for one of our Flutter projects, we're currently taking a look into the flutter_unity_widget in order to embed a Unity project into a Flutter project (e. g. to use the Unity rendering for visualizations). Setting it up generally worked, but there were 2 things that needed to be adjusted in the generated
    build.gradle
    file. The first thing was easy to do by just adding a line to the "Custom Main Gradle Template". The other step however requires to remove lines that are only added later on to the file.

    The default file content ends with the placeholders:
    Code (csharp):
    1. [...]
    2. [added line]
    3. }**REPOSITORIES**
    4. **IL_CPP_BUILD_SETUP**
    5. **SOURCE_BUILD_SETUP**
    6. **EXTERNAL_SOURCES**
    7.  
    One of these steps generates a section that looks like this:

    Code (csharp):
    1. def BuildIl2Cpp(String workingDir, String configuration, String architecture, String abi, String[] staticLibraries) {
    2.     def commandLineArgs = []
    3.     commandLineArgs.add("--compile-cpp")
    4.     commandLineArgs.add("--platform=Android")
    5.     commandLineArgs.add("--architecture=" + architecture)
    6.     commandLineArgs.add("--outputpath=" + workingDir + "/src/main/jniLibs/" + abi + "/libil2cpp.so")
    7.     commandLineArgs.add("--libil2cpp-static")
    8.     commandLineArgs.add("--baselib-directory=" + workingDir + "/src/main/jniStaticLibs/" + abi)
    9.     commandLineArgs.add("--incremental-g-c-time-slice=3")
    10.     commandLineArgs.add("--configuration=" + configuration)
    11.     commandLineArgs.add("--dotnetprofile=unityaot-linux")
    12.     commandLineArgs.add("--enable-debugger")
    13.     commandLineArgs.add("--profiler-report")
    14.     commandLineArgs.add("--profiler-output-file=" + workingDir + "/build/il2cpp_"+ abi + "_" + configuration + "/il2cpp_conv.traceevents")
    15.     commandLineArgs.add("--print-command-line")
    16.     commandLineArgs.add("--generatedcppdir=" + workingDir + "/src/main/Il2CppOutputProject/Source/il2cppOutput")
    17.     commandLineArgs.add("--cachedirectory=" + workingDir + "/build/il2cpp_"+ abi + "_" + configuration + "/il2cpp_cache")
    18.     commandLineArgs.add("--tool-chain-path=" + android.ndkDirectory)
    19.     staticLibraries.eachWithIndex {fileName, i->
    20.         commandLineArgs.add("--additional-libraries=" + workingDir + "/src/main/jniStaticLibs/" + abi + "/" + fileName)
    21.     }
    22.     def executableExtension = ""
    23.     if (org.gradle.internal.os.OperatingSystem.current().isWindows())
    24.         executableExtension = ".exe"
    25.     exec {
    26.         executable workingDir + "/src/main/Il2CppOutputProject/IL2CPP/build/deploy/il2cpp" + executableExtension
    27.         args commandLineArgs
    28.         environment "ANDROID_SDK_ROOT", getSdkDir()
    29.     }
    30.     delete workingDir + "/src/main/jniLibs/" + abi + "/libil2cpp.sym.so"
    31.     ant.move(file: workingDir + "/src/main/jniLibs/" + abi + "/libil2cpp.dbg.so", tofile: workingDir + "/symbols/" + abi + "/libil2cpp.so")
    32. }
    4 of these lines need to be removed as described in a thread in one of flutter_unity_widgets GitHub issues. Removing 4 lines by hand is no problem, doing this in a pipeline is a different matter - still possible, but not desirable - but I'd like to instead go to the root of the problem and adjust the settings directly.



    What settings do I need to adjust in order to prevent the following command line arguments from being added to the
    build.gradle
    file?
    Code (csharp):
    1. commandLineArgs.add("--dotnetprofile=unityaot-linux")
    2. commandLineArgs.add("--enable-debugger")
    3. commandLineArgs.add("--profiler-report")
    4. commandLineArgs.add("--profiler-output-file=" + workingDir + "/build/il2cpp_"+ abi + "_" + configuration + "/il2cpp_conv.traceevents")
    The settings currently active are the following:
    upload_2022-6-27_19-7-22.png
    upload_2022-6-27_19-7-46.png
    upload_2022-6-27_19-7-58.png
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    I think from those 4 only
    1. commandLineArgs.add("--enable-debugger")
    can be affected by PlayerSettings, this line appears when Scripting Debugging is enabled.
    Other lines will be always available, since they're required for Android il2cpp.
     
  3. Wepner-PD

    Wepner-PD

    Joined:
    Jul 13, 2018
    Posts:
    32
    Thank you for the response.

    If I'm not mistaken, Script Debugging is disabled (see the last screenshot), yet the corresponding command line argument is still added.

    It sounds like these lines will need to be removed after the fact. =/
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    That's strange, could you build to empty folder and see if it still happens ? In 2021.2 Android switched to incremental build pipeline, it's possible that Unity thinks it doesn't need regenerate that file
     
  5. Wepner-PD

    Wepner-PD

    Joined:
    Jul 13, 2018
    Posts:
    32
    I'm sorry for the late response: I've built into an empty directory (due to git branch changes, the previous build directory was removed), and the result is still the same.

    I just noticed that due to an accident - probably related to switching git branches and having Unity Hub and/or Untiy open at an unfortunate time - the project is currently using Unity 2021.3.2f1, but the unity-in-flutter package is used in version 2022.1.0. I don't think this discrepancy should cause the issues described above, but later on, I'll check again if they persist with Unity 2022.1.0.
     
  6. timbotimbo

    timbotimbo

    Joined:
    Jul 1, 2022
    Posts:
    1
    Regarding the debugging setting:
    It is enabled in the function DoBuildAndroid() of the build script in the unitypackage of the plugin.
    https://github.com/juicycleff/flutt...terUnityIntegration/Editor/Build.cs#L188-L192

    It might also be possible to remove those 4 lines from the gradle file in this script, there are already comments that appear to do that.
    https://github.com/juicycleff/flutt...terUnityIntegration/Editor/Build.cs#L308-L310


    The real issue here is the project export from Unity.
    In Unity 2019 this export did the IL2CPP compile, and afterwards builds of the (flutter) android project were quick.
    In newer Unity versions the export moves part of the compilation to be executed from gradle, making the android project build a lot slower.

    Is it in anyway possible to do an export from Unity 2021/2022 that is more similar to Unity 2019?
     
  7. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Since il2cpp compile/link moved from Unity to Gradle, the total time for producing apk/aab hasn't changed.

    I am afraid not, note though, in 2021.2 Android switched to incremental build pipeline, thus sequential builds from Unity without any changes are very fast, assuming you preserve Library folder.[/QUOTE]
     
  8. Wepner-PD

    Wepner-PD

    Joined:
    Jul 13, 2018
    Posts:
    32
    I've tested the behavior again using Unity 2022.1.0f1 (so the same version I'm using the flutter plugin for), the same arguments are still present in the generated
    build.gradle
    file. It seems like now it builds successfully, but the built version crashes immediately, but I guess I'll circumvent this by just switching back and adjusting the
    build.gradle
    manually...

    Just to clearify: Do you mean by this the time it takes to build if the build is started from Unity? If so, then this wasn't what @timbotimbo wanted to point out.
    Even for an almost empty Unity project, it's makes a clearly noticeable difference for the build time of the Flutter project (which has to do the IL2CPP step now on top of building the Flutter project).