Workspace/Android

[Gradle][Unity] NDK is not installed

Bombus 2023. 8. 18. 11:48

| 현상

 

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/<user>/.jenkins/workspace/<job>/GradleProject/unityLibrary/build.gradle' line: 176

* What went wrong:
Execution failed for task ':unityLibrary:BuildIl2CppTask'.
> NDK is not installed

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings

Gradle 빌드 중 설치된 NDK를 인식하지 못하는 현상

UnityHub가 도입되고 고도화되면서 Unity 설치 시에 각 플랫폼 별 빌드 지원하는 툴들을 설치하게 되었다.

그 후로는 유사한 이슈가 발생하지 않았는데 2021.3.22f1버전에서 2022.3.6f1버전으로 업그레이드 하면서 발생했다.

 

 

| 해결

 

using System.IO;
using UnityEditor.Android;

namespace Tools
{
#if UNITY_2022_3_OR_NEWER
    public class AndroidPostBuildProcessor : IPostGenerateGradleAndroidProject
    {
        public int callbackOrder => 1;

        public void OnPostGenerateGradleAndroidProject(string path)
        {
            Debug.Log("OnPostGenerateGradleAndroidProject: " + path);

            var di = new DirectoryInfo(path);
            var localPropertiesFile = Path.Combine(di.Parent.FullName, "local.properties");
            if (File.Exists(localPropertiesFile))
            {
                var hasNdkProp = HasNdkProp(localPropertiesFile);
                if (false == hasNdkProp)
                {
                    var ndkDir = $"{System.Environment.NewLine}ndk.dir={AndroidExternalToolsSettings.ndkRootPath}";
                    File.AppendAllText(localPropertiesFile, ndkDir);
                }
            }
        }

        private bool HasNdkProp(string path)
        {
            using (var streamReader = new StreamReader(path, System.Text.Encoding.UTF8))
            {
                string line = string.Empty;
                while ((line = streamReader.ReadLine()) != null)
                {
                    if (line.StartsWith("ndk.dir="))
                        return true;                    
                }
            }
            return false;
        }
    }
#endif
}

근본적인 해결책은 아니지만 
Android Project를 생성할 때, local.properties 파일에 NDK 정보를 입력하는 식으로 해결하였다.

| 참고

 

 

빌드 구성  |  Android 개발자  |  Android Developers

Android 빌드 시스템은 앱 리소스 및 소스 코드를 컴파일하고 개발자가 테스트, 구축, 서명 및 배포할 수 있는 APK로 패키징합니다.

developer.android.com

 

Unity - Scripting API: LocalPropertiesFile

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

※ 2023.1버전대부터 Ndk, LocalPropertiesFile 등 Gradle 관련 세팅을 제어하는 클래스들이 Unity.Android.Gradle 네임스페이스에 추가된다.

 

 

Unity - Scripting API: AndroidExternalToolsSettings

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

※ Preferences > External Toos > Android에서 설정한 값들을 위 클래스를 통해 제어할 수 있다.

반응형