NDK with GoogleTest

$NDK_ROOT/ndk-build is GNU Make based build system shipped with the NDK.
$PROJECT/jni/Android.mk is build description file used by ndk-build.
$PROJECT/jni/Application.mk compilation configuration file used by ndk-build.

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := hello-ndk-gtest

LOCAL_SRC_FILES := Main.cpp Test.cpp
# LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)

LOCAL_STATIC_LIBRARIES := googletest_main

include $(BUILD_EXECUTABLE)

# Googletest is shipped with NDK.
# Import googletest module.
$(call import-module,third_party/googletest)

Application.mk

APP_OPTIM := debug
# To find your device's or AVD's ABI.
# adb shell getprop | grep "ro.product.cpu.abi"
APP_ABI := x86
APP_STL := c++_shared

$PROJECT/jni/Main.cpp

#include "gtest/gtest.h"

GTEST_API_ int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

$PROJECT/jni/Test.cpp

#include "gtest/gtest.h"

class Test : public ::testing::Test
{
protected:
    const char* m_pszName;
protected:
    void SetUp()
    {
        m_pszName = "Test";
    }
    void TearDown()
    {}
};

TEST(MyTest, MyTest1)
{
    ASSERT_TRUE(true);
}

TEST_F(Test, MyTest2)
{
    ASSERT_STREQ("Test", m_pszName);
}

Build output:-

$PROJECT/libs/x86/hello-ndk-gtest
$PROJECT/libs/x86/libc++_shared.so

Comments

Post a Comment