I'm trying to port a native C++ DLL to be a shared library on Android. For building, I am using VSCode, not using Visual Studio. Downloaded the Android NDK r18b. My project is CMake based, and when I try to generate it to be with the "NMake Mmakefiles" generator, I always get this error:
CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio Edition' is not installed.
I have created a small project to test this.
main.cpp
int foo( int a, int b ) { return a + b; }
CMakeLists.txt
cmake_minimum_required( VERSION 3.11.0 )
add_library( Engine SHARED main.cpp )
I run it with this command line (CMake 3.11.4):
cmake -g "NMake Makefiles" .. -DCMAKE_TOOLCHAIN_FILE=%NDK_ROOT%\build\cmake\android.toolchain.cmake -DANDROID_NDK=%NDK_ROOT%
With this one, I still get the error:
CMake Error in CMakeLists.txt:
CMAKE_SYSTEM_NAME is 'Android' but 'NVIDIA Nsight Tegra Visual Studio Edition' is not installed.
Can someone help me to make this small main.cpp into an Android .so? I'd not like to use Android Studio nor an other IDE. Looking for creating a make file and add it to my VSCode build.
Thank you.
In case you were willing to use Gradle to configure your cmake...
I use the Gradle Wrapper, after you install gradle you can call
gradle wrapper
in a directory and it will install a local copy. This copy can be set at a specific version or can be upgraded.The Android Gradle plugin uses the ninja build system to configure cmake and target ABIs. The following scripts will generate ABIs for all supported platform types, but it is easy to remove any that you don't wish to create.
VSCode has a gradle plugin for generating from the IDE, or you can create a build type that simply calls the gradlew command line.
First, I inited a gradle project using
gradle wrapper
and thengradlew init
.Then I added the build scripts for building an Android Library.
build.gradle
This build file includes a dummy AndroidManifest.xml file:
I needed an older version of CMake, I went with 3.4 instead of 3.11
CMakeLists.txt
local.properties
gradle.properties
Finally, to build I simply run:
or
And it generates the libraries in the
build\intermediates\cmake
directory.