最近在搞NDK开发,用到GTest做UT,在Windows下可以正常运行,那么NDK下的gtest使用,在网上没有找到相关资料,所以将过程记录下来备忘,或许也可以帮到遇到相关难题的同学们;
下面描述具体步骤:
1.首先需要将GTest编译生成静态库,编译后两个库文件 libgtest.a 和 libgtest_main.a
##Application.mk
#######################################################################
APP_MODULES := libgtest libgtest_main
APP_STL := stlport_static #STL
##Android.mk
#######################################################################
# gtest lib target
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := gtest-all.cc
LOCAL_C_INCLUDES := $(libgtest_target_includes)
LOCAL_MODULE := libgtest
LOCAL_MODULE_TAGS := eng
include $(BUILD_STATIC_LIBRARY)
#######################################################################
# gtest_main lib target
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := gtest_main.cc
LOCAL_C_INCLUDES := $(libgtest_target_includes)
LOCAL_STATIC_LIBRARIES := libgtest
LOCAL_MODULE := libgtest_main
LOCAL_MODULE_TAGS := eng
include $(BUILD_STATIC_LIBRARY)
2.有了两个gtest静态库,下面准备Gtest测试程序"testMain.cpp"
#include "gtest/gtest.h"
using testing::Environment;
int main(int argc, char *argv[])
{
testing::AddGlobalTestEnvironment(NULL);
testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
}
TEST(TestDemo, Haha)
{
char szTmp[10];
strcpy(szTmp,"test...");
int nLen = strlen(szTmp);
EXPECT_EQ(nLen,7);
}
3.用NDK编译测试工程
##Application.mk
####################################################################
APP_STL := stlport_static
##Android.mk
####################################################################
LOCAL_PATH := $(call my-dir)
PRJ_INC_PATH := $(call my-dir)/../include
PRJ_LIB_PATH := $(call my-dir)/../libs
include $(CLEAR_VARS)
LOCAL_MODULE := gtest_main
LOCAL_SRC_FILES := ./../libs/libgtest_main.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := gtest
LOCAL_SRC_FILES := ./../libs/libgtest.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := jxtest
LOCAL_SRC_FILES := testMain.cpp
LOCAL_STATIC_LIBRARIES := gtest_main gtest
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ $(PRJ_INC_PATH)/ $(PRJ_LIB_PATH)/
$(warning including path: $(LOCAL_C_INCLUDES))
include $(BUILD_EXECUTABLE)
4.编译生成jxtest的可执行程序,下面将它与需要的库放到手机上,使用ADB完成测试运行.
库文件放到/system/lib ,可执行程序放到/system/bin,并给可执行权限;
完成...