Visual Studio Android Google Test Project Setup

Issue

I have a normal native cpp activity application project set up in VS2015, i.e.:

  • Android Project -> C++ code used to build a .so file
  • AndroidPackaging Project -> java / manifest stuff references the .so to create the apk.

I would like to add some unit tests using google test for the C++ code (not java or jni) in the .so to run on an Android device.

How would I set up a project to do this? (looking at this it appears to be building an executable rather than an apk, and I can’t see a suitable Visual Studio Project template to do that).

Do I have to set up a makefile project? How would I ensure the build environment (target API level, C++ features etc.) is the same as my actual project? I don’t really have much clue when it comes to makefiles.

Solution

It actually seems quite simple to add a new “executable” configuration type for Clang by editing the existing MSBuild xml files in the C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Application Type\Android\2.0\ folder.

Make sure to back these up before changing them to avoid breaking anything.


File: 1033/general_android.xml, line 80, add:

<EnumValue Name"Executable" DisplayName"Executable" Description"Executable" />

(Adds the “Executable” option to the Configuration Type dropdown menu in Project Properties > General).

File: Android.Common.targets, change line 325:

<ProjectTools Condition"'$(ConfigurationType)'  'DynamicLibrary' or '$(ConfigurationType)'  'Executable'" Include"Link" />

(Makes the Project Properties > Linker menu available for the executable configuration type).

File: Android.Common.props, add to ~line 85:

  <PropertyGroup Condition"'$(ConfigurationType)'  'Executable'">
    <LinkCompiled>true</LinkCompiled>
    <TargetExt></TargetExt>
    <OutputType>exe</OutputType>
    <TargetName Condition"'$(TargetName)'  ''">$(RootNamespace)</TargetName>
  </PropertyGroup>

This should result add the executable option here:

Executable Configuration Type

The usual project property settings can be used to add extra include directories / library dependencies etc. You may have to manually add -fPIE and -pie to the linker command line options, and change the Output File option to $(TargetPath).

The google test project can then be built separately as a static or shared lib and linked in. The resulting executable can be run on a device with commands similar to the ones described here.

I haven’t yet got this launching under a debugger.

Answered By – user673679

Leave a Comment