juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.13k stars 518 forks source link

Receiving "NDK is not installed" error despite using NDK installed with Unity #832

Closed Shaobin-Jiang closed 1 year ago

Shaobin-Jiang commented 1 year ago

Unity version: 2022.2.0f1c1 on Windows 11

android/local.properties

sdk.dir=C:\\Installed\\Scoop\\apps\\android-sdk\\current
flutter.sdk=C:\\Installed\\flutter
ndk.dir=C:\\Installed\\UnityEditor\\2022.2.0f1c1\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\NDK

Where the above ndk path is what I directly copied from Unity's preference settings.

Despite having done this, after using Flutter run, I keep receiving the error below:

[CXX1100] Both android.ndkPath and ndk.dir in local.properties are set

FAILURE: Build failed with an exception.

* Where:
Build file 'F:\Files\Codes\Projects\sports-training\sports_training\android\unityLibrary\build.gradle' line: 69

* 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

BUILD FAILED in 2s
Running Gradle task 'assembleDebug'...                              3.4s
Exception: Gradle task assembleDebug failed with exit code 1

This is kind of frustrating, since I have done exactly what the instructions from the README dictates. Is there anything I can do to solve this?

weidenbach commented 1 year ago

I have the same issue too. You can fix it by specifying the ndkVersion within the build.gradle in the generated unity project. However, it will be overriden every time you export the project, so I wrote a command that overrides the generated version with the one that includes the ndkVersion line.

android { ndkVersion = "21.3.6528147" task BuildIl2CppTask { doLast { BuildIl2Cpp(projectDir.toString().replaceAll('\\\\', '/'), 'Release', 'armv7', 'armeabi-v7a', [ ] as String[]); BuildIl2Cpp(projectDir.toString().replaceAll('\\\\', '/'), 'Release', 'arm64', 'arm64-v8a', [ ] as String[]); } } afterEvaluate { if (project(':unityLibrary').tasks.findByName('mergeDebugJniLibFolders')) project(':unityLibrary').mergeDebugJniLibFolders.dependsOn BuildIl2CppTask if (project(':unityLibrary').tasks.findByName('mergeReleaseJniLibFolders')) project(':unityLibrary').mergeReleaseJniLibFolders.dependsOn BuildIl2CppTask } sourceSets { main { jni.srcDirs = ["src/main/Il2CppOutputProject"] } } }

Shaobin-Jiang commented 1 year ago

I have the same issue too. You can fix it by specifying the ndkVersion within the build.gradle in the generated unity project. However, it will be overriden every time you export the project, so I wrote a command that overrides the generated version with the one that includes the ndkVersion line.

android { ndkVersion = "21.3.6528147" task BuildIl2CppTask { doLast { BuildIl2Cpp(projectDir.toString().replaceAll('\\\\', '/'), 'Release', 'armv7', 'armeabi-v7a', [ ] as String[]); BuildIl2Cpp(projectDir.toString().replaceAll('\\\\', '/'), 'Release', 'arm64', 'arm64-v8a', [ ] as String[]); } } afterEvaluate { if (project(':unityLibrary').tasks.findByName('mergeDebugJniLibFolders')) project(':unityLibrary').mergeDebugJniLibFolders.dependsOn BuildIl2CppTask if (project(':unityLibrary').tasks.findByName('mergeReleaseJniLibFolders')) project(':unityLibrary').mergeReleaseJniLibFolders.dependsOn BuildIl2CppTask } sourceSets { main { jni.srcDirs = ["src/main/Il2CppOutputProject"] } } }

That did not exactly work for me @weidenbach , but gave me a hint as to how the problem might be solved. As I have only limited experience with flutter and almost none with vanilla android development, or at least I have never engaged in projects that requires me to look deeper into the project structure, I never realized that there might be more than one build.gradle file in the project.

In my case, the solution is this: I manually replaced line 69 in android/unityLibrary/build.gradle, which was originally

commandLineArgs.add("--tool-chain-path=" + android.ndkDirectory)

with the line below:

commandLineArgs.add("--tool-chain-path=" + "C:/Installed/UnityEditor/2022.2.0f1c1/Editor/Data/PlaybackEngines/AndroidPlayer/NDK")

where the path within the quotation marks is where my NDK is installed.

timbotimbo commented 1 year ago

Just ran into the same error while testing a build on Unity 2022.3.4. This line at the top of the error hints at the solution: [CXX1100] Both android.ndkPath and ndk.dir in local.properties are set

These newer Unity versions add a ndkPath variable to unityLibrary/build.gradle

android {
+    ndkPath "C:/Program Files/Unity/Hub/Editor/.....

This conflicts with the ndk.dir in local.properties.

Simply removing ndk.dir from local.properties solved this error for me.