[펌] [Android] Tips to reduce android build times.
With recent Unity versions (2020, 2021 and 2022) Flutter Android builds will take a lot longer, because it also has to compile the IL2CPP code from Unity.
From the Readme:
- Android builds takes forever to complete Unity 2022.1.*, remove these lines from unityLibrary/build.gradle filecommandLineArgs.add("--enable-debugger")
commandLineArgs.add("--profiler-report")
commandLineArgs.add("--profiler-output-file=" + workingDir + "/build/il2cpp_"+ abi + "_" + configuration + "/il2cpp_conv.traceevents")
Here is some testing to check how to speed things up.
Unity settings
IL2CPP Code Generation
- Build Settings -> IL2CPP Code Generation: select Faster (smaller) builds instead of Faster runtime.
- Or C# EditorUserBuildSettings.il2CppCodeGeneration = UnityEditor.Build.Il2CppCodeGeneration.OptimizeSize;
C++ compiler configuration
- In Player settings -> Other settings -> configuration, select Debug.
- Or C# PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Android, Il2CppCompilerConfiguration.Debug);
Disable script debugging
Don't bother setting this in the Unity settings, it will be overridden by the build script.
Assets/FlutterUnityIntegration/Editor/build.cs
In the function DoBuildAndroid() remove the line enabling script debugging.
playerOptions.options = BuildOptions.AllowDebugging;
This is the same as removing commandLineArgs.add("--enable-debugger") from the build.gradle file after an export.
Measuring results
When you run a Flutter build, there are multiple IL2CPP stages that report their duration, with 2 sections taking far longer than all others.
We can compare these build durations when using different settings in Unity.
Log of the slow armv7 and arm64 sections
Starting: Z:\fuw20221.1.0+7\2021\example\android\unityLibrary\src\main\Il2CppOutputProject\IL2CPP\build\deploy\bee_backend\win-x64\bee_backend.exe --profile="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_armeabi-v7a_Release/il2cpp_cache/buildstate/backend_profiler1.traceevents" --stdin-canary --dagfile="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_armeabi-v7a_Release/il2cpp_cache/buildstate/bee.dag" --continue-on-failure --dagfilejson="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_armeabi-v7a_Release/il2cpp_cache/buildstate/bee.dag.json" FinalProgram
WorkingDir: Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_armeabi-v7a_Release/il2cpp_cache/buildstate
ExitCode: 0 Duration: 2m:04s
Build succeeded with 553 successful nodes and 0 failed ones
Starting: Z:\fuw20221.1.0+7\2021\example\android\unityLibrary\src\main\Il2CppOutputProject\IL2CPP\build\deploy\bee_backend\win-x64\bee_backend.exe --profile="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_arm64-v8a_Release/il2cpp_cache/buildstate/backend_profiler1.traceevents" --stdin-canary --dagfile="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_arm64-v8a_Release/il2cpp_cache/buildstate/bee.dag" --continue-on-failure
--dagfilejson="Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_arm64-v8a_Release/il2cpp_cache/buildstate/bee.dag.json" FinalProgram
WorkingDir: Z:/fuw20221.1.0+7/2021/example/android/unityLibrary/build/il2cpp_arm64-v8a_Release/il2cpp_cache/buildstate
ExitCode: 0 Duration: 2m:03s
Build succeeded with 553 successful nodes and 0 failed ones
Results
The exact durations here aren't important, as this will be different of every computer and project setup.
This is after flutter clean and a fresh Unity export. Any subsequent runs will be faster because of caching.
Flutter build apk, with an export from Unity 2021.3.5f1 in android/unityLibrary on Windows 10.
Settingarmv7 durationarm64 duration
example project | 2m:04s | 2m:03s |
1 - Code generation (faster builds) | 1m:41s | 1m:38s |
2 - Compiler configuration (debug) | 45s | 43s |
3 - No script debugging | 30s | 30s |
1 + 2 | 35s | 34s |
1 + 2 + 3 | 23s | 25s |
Conclusion
My advice is to add this to the build script in DoBuildAndroid() for any test or debug builds, remove it for final release builds.
//Assets/FlutterUnityIntegration/Editor/build.cs
bool isReleaseBuild = false;
#if UNITY_2022_1_OR_NEWER
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Android, isReleaseBuild ? Il2CppCompilerConfiguration.Release : Il2CppCompilerConfiguration.Debug);
PlayerSettings.SetIl2CppCodeGeneration(UnityEditor.Build.NamedBuildTarget.Android, UnityEditor.Build.Il2CppCodeGeneration.OptimizeSize);
#elif UNITY_2021_2_OR_NEWER
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.Android, isReleaseBuild ? Il2CppCompilerConfiguration.Release : Il2CppCompilerConfiguration.Debug);
EditorUserBuildSettings.il2CppCodeGeneration = UnityEditor.Build.Il2CppCodeGeneration.OptimizeSize;
#endif
If it doesn't affect your workflow, disable script debugging.
// playerOptions.options = BuildOptions.AllowDebugging;
And if you are developing on an arm64 device anyway, temporarily remove the armv7 build target from the Unity player settings.
[출처] https://github.com/juicycleff/flutter-unity-view-widget/issues/643
'Unity3D > Tips' 카테고리의 다른 글
[사용금지] Unity 2022.3 (0) | 2023.11.17 |
---|---|
[링크] 유니티에서 패키지를 기본값으로 재설정하기(reset pacakges to defaults) (0) | 2023.11.13 |
[Unity] dll 등 import 후, Revert Factory Settings 무한루프에 빠질 때 (0) | 2023.08.23 |
[펌] 메모리 최적화 꿀팁 (0) | 2023.04.12 |
AssetPostprocessor_Audio(Audio 최적화 설정 셋팅) (0) | 2023.04.05 |