To clear up some confusion here: ndk-build is a build system included in the NDK. It uses Android.mk
files. The NDK itself is a collection of compilers and libraries that are needed to build C/C++ code for Android. ndk-build and cmake both use the NDK.
What is the actual difference between CMake and NDK build in android studio project.
They use a different language (custom makefiles vs cmake) to describe builds. Ideally there is no difference in output for identically described builds, but that doesn't mean there aren't any bugs.
Can anyone has a better explanation with an example when we need to use what?
In general, use whichever system you prefer.
CMake's main advantage is that you can use one set of build files for all your targets (Android, Linux, Windows, iOS, etc). If your project is cross platform, CMake should make that easiest on you. It also is widely known outside Android developers, so people new to Android will have a better chance of understanding it.
ndk-build should be preferred if you're building a project that already uses Android.mk
files for its build system (legacy projects).
If you're writing new code, use whatever you're comfortable with. If you're not familiar with either, cmake is probably the better choice because it will make cross-platform work easier in the future if you choose to do so.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…