Support for Android CMake projects in Visual Studio

December 15th, 2015

CMake is a cross-platform project generator that enables reuse of shared C++ code across multiple IDE and project systems. We made a change to CMake to support our Android toolchain in Visual Studio. With this change, you can take your existing CMake project targetting Android, and with minimal modifications, you can have it open in Visual Studio, and benefit from our rich IDE experience for Android. We have contributed to the Microsoft CMake open source github repository, and we are planning to work with Kitware to integrate those changes into the public CMake repository. But currently, this is what you have to do to get going. As a prerequisite, please install the Android MDD package in Visual Studio.

Getting CMake

Go to the VCMDDAndroid branch on the Microsoft CMake repository.

Click the Download ZIP button and download an offline copy of the VCMDDAndroid branch, unzip the archive.

Install CMake 3.4.1 from https://cmake.org/download/, this is necessary to build CMake itself, the Win32 Installer is recommended.

Run cmake -G “Visual Studio 14” on the unzipped archive, this should be run from the root folder of CMake. This will take a good 15 minutes or so.

This will use the Visual Studio 2015 generator, and generate Visual Studio projects. The goal is to build CMake in Visual Studio. After CMake runs, you should have the Visual Studio projects in the out directory.

Run msbuild cmake.sln, this will build CMake. You can run msbuild from a VS2015 Developer Command Prompt.

The compiled binaries will be in the bin\Debug folder.

Either add the output directory to PATH, or make sure to run cmake.exe from this directory, so it runs the modified version of CMake and not the installed one.

Building your CMake projects

The following example illustrate the capabilities of the generator and the CMake variables. Run cmake -G “Visual Studio 14 ARM” –DCMAKE_SYSTEM_NAME=VCMDDAndroid to generate the Visual Studio projects. You can remove the ARM specifier to compile for x86. Here’s the content of the CMakeLists.txt. You can also find a link to this short sample below. cmake_minimum_required(VERSION 3.4) project (Sample) add_library(Sample STATIC sample.cpp) # demo of the variables being set when VCMDDAndroid is targeted if(VC_MDD_ANDROID AND VC_MDD_ANDROID_VERSION MATCHES “1.0”) message(“VCMDDAndroid 1.0”) else() if(CMAKE_GENERATOR MATCHES “Visual Studio 14” AND CMAKE_SYSTEM_NAME MATCHES “VCMDDAndroid”) message(“VCMDDAndroid”) endif() endif() # optional, demo of the VC_MDD_ANDROID* properties set_property(TARGET Sample PROPERTY VC_MDD_ANDROID_USE_OF_STL “c++_static”) set_property(TARGET Sample PROPERTY VC_MDD_ANDROID_API_LEVEL “android-21”) A CMake toolchain file is not required, the example is self-contained. We care a great deal about usability and made sure the minimum amount of information is fed into the system by the users. The only information required is the system name. Open and build the generated projects in Visual Studio.

Known issues and limitations

Currently we only support libraries, object, static, or dynamic. We do not support the packaging project (Java), which is used as a bootstrapper, we are planning to add support for it soon. We only support x86 and ARM architectures, we are planning to add support for x64 and ARM64 soon.

What’s next?

Going forward, we will add x64 and ARM64, as well as packaging project support. We will also work with Kitware to integrate this work into the public CMake repository. We hope you find this support useful in your CMake projects and we look forward to your feedback and suggestions on how to improve this support. For more news on this front please reach out to us directly or follow us on VCBlog and our Visual C++ cross-platform page. sample.zip