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.
Or C#EditorUserBuildSettings.il2CppCodeGeneration = UnityEditor.Build.Il2CppCodeGeneration.OptimizeSize;
C++ compiler configuration
InPlayer settings -> Other settings -> configuration, selectDebug.
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 functionDoBuildAndroid()remove the line enabling script debugging. playerOptions.options = BuildOptions.AllowDebugging; This is the same as removingcommandLineArgs.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.
The exact durations here aren't important, as this will be different of every computer and project setup. This is afterflutter cleanand a fresh Unity export. Any subsequent runs will be faster because of caching.
Flutter build apk, with an export from Unity 2021.3.5f1 inandroid/unityLibraryon 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 inDoBuildAndroid()for any test or debug builds, remove it for final release builds.
> Configure project :unityLibrary Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'unityLibrary\build.gradle'
...
[빌드에러 #2]
Execution failed for task ':launcher:checkReleaseDuplicateClasses'. > Could not resolve all files for configuration ':launcher:releaseRuntimeClasspath'.
Also, there is another workaround that keeps the Gradle version (in case some android lib requires it). For that workaround, you will need to copy a file from/Applications/Unity/Hub/Editor/2022.2.3f1/PlaybackEngines/AndroidPlayer/Tools/GradleTemplates/settingsTemplate.gradletoAssets/Plugins/Android/settingsTemplate.gradle. That way Unity will use your template inAssets/Plugins/Android. (It's weird that Unity doesn't has a checkbox for that) For my version of unity, it looks like that:
The conflicting part isRepositoriesMode.PREFER_SETTINGS. But you can safely delete the wholedependencyResolutionManagementblock from here. Since the android project still needs to know about local dependencies, copy therepositoriesblock, you will need it later. NowsettingsTemplate.gradleshould look like this:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':launcher:packageRelease'.
> A failure occurred while executing com.android.build.gradle.tasks.PackageAndroidArtifact$IncrementalSplitterRunnable
> com.android.ide.common.signing.KeytoolException: Failed to read key anne from store
"C:\Test_Unity2022\Keystore\testapp.keystore": Cannot recover key
Unity 2022 포팅 테스트 해볼 겸 올려보다가 빌드 에러가 나서 보니 위와 같은 에러가 뜨고 있다.