Porting NDK code to Visual Studio

Setting up an NDK project with Visual Studio has several benefits:

  • Live IDE-based visual debugging of ADB connected Android device

  • Building and deploying to device and configuring build options from inside the IDE

  • Visual Studio's intellisense code assistance

Depending on usage, it may be acceptable to download the free Community edition of Visual Studio:
https://www.visualstudio.com/downloads/.

Step-by-step guide

The first step is to ensure visual studio has the 'Mobile Development with C++' component installed. (This can be checked by going to Programs and Features in Windows then finding Visual Studio in the list of programs and going to the 'Change' option.)

Mobile development with C++

It should now be possible to create a new project in Visual Studio using the Android 'Native-Activity Application' template.

In Visual Studio, go to File → New → Project, and navigate to the template under Visual C++ → Cross Platform → Android.

Native-Activity Application

Create the new project with the 'Native-Activity Application' template. This is most suitable as it sets up a simple working program with EGL/GLES functionality already hooked up.

The created project contains two child projects, one called ProjectName.NativeActivity and one called ProjectName.Packaging. The 'Packaging' project is responsible for packaging the final APK from assets/native code. The 'NativeActivity' contains all the cpp/h files and has settings related to the compiler/linker.

For Vulkan support, it may be necessary to update the default API level. This is done in the Project settings for the NativeActivity project under Configuration Properties → General → Target API Level. The Packaging project also has a Target API Level setting to change.

Adding source files

To add source files to the project, just simply drag the .cpp/.h files from an Explorer window into the Visual Studio window under the ProjectName.NativeActivity folder in the 'Solution Explorer'.

For any required libraries (.so), these can be added by going to the project properties for ProjectName.NativeActivity under Project → Properties → Configuration Properties → Linker → Input and adding them to the Library Dependencies property. It may be also necessary to add the library search paths under Linker → General → Additional Library Directories.

Building/Deploying/Debugging APKs

Building the generated project is as simple as hitting F7. Hitting F5 will build and deploy and then debug to any attached device. Visual Studio uses ADB to detect attached Android devices. It usually works, but sometimes it is necessary to restart Visual Studio to pick up newly-attached devices. Most of the common debugging features are supported - e.g. setting breakpoints and watching values.

Visual Studio detected G950F and can deploy/debug it
Visual Studio detected G950F and can deploy/debug it

Embedding Assets in the APK

In order to embed additional assets in the APK, it is necessary to specify an asset directory.

In the ProjectName.Packaging project, open the build.xml file and add the following tags somewhere:

<property name="asset.dir" value="C:\Dev\assets" />
<property name="asset.absolute.dir" location="${asset.dir}" />

Replace "C:\Dev\assets" with the data or asset folder holding the contents that you want embedded in the APK.

C++11/STL Issues

By default the project does not support C++11, but this is changed easily by going to ProjectName.NativeActivity Project → Properties → Configuration Properties → C/C++ → Language →C++ Language Standard

The default STL support is not that comprehensive, but the project can be configured with different STL libraries. These settings can be found under ProjectName.NativeActivity project settings under Configuration Properties → General → Use of STL. Usually setting this to 'LLVM libc++ static library (c++_static)' will give the best compatibility.