ndk开发-gtest编译

用NDK编译gtest库:

1. gtest代码pull下来后,创建jni目录,将googleTest下文件放到jni目录下

2. 在jni目录下创建Application.mk和Android.mk文件

Application.mk

##Application.mk
#######################################################################
APP_ABI := armeabi-v7a
APP_MODULES := libgtest libgtest_main
APP_STL := stlport_static  
APP_PLATFORM := android-18

Android.mk

PROJECT := gtest
LOCAL_PATH:= $(call my-dir)
PRJ_TOP := $(call my-dir)
include $(PRJ_TOP)/googletest/Android.mk

3. 在jni/googletest目录下创建Android.mk

Android.mk

##Android.mk
#######################################################################
# gtest lib target

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CPP_EXTENSION := .cc

LOCAL_SRC_FILES := src/gtest-all.cc

#LOCAL_C_INCLUDES := $(libgtest_target_includes)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/src
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include

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 := src/gtest_main.cc

LOCAL_C_INCLUDES := $(LOCAL_PATH)/src
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include

LOCAL_STATIC_LIBRARIES := libgtest

LOCAL_MODULE := libgtest_main
LOCAL_MODULE_TAGS := eng

include $(BUILD_STATIC_LIBRARY)

4. ndk-build编译生成libgtest.a和libgtest_main.a

测试程序编译链接gtest库:

1. 将交叉编译好的gtest库libgtest.a和libgtest_main.a放到测试代码目录

2. 编写测试程序代码test.cpp

#include "gtest/gtest.h"
#include "downloader_interface.h"

TEST(TestDemo, DL_CreateHandle)
{
    DLHandle handle = DL_CreateHandle();
    ASSERT_NE(handle, (void*)NULL);

    DL_CloseHandle(handle);
}

3. 编写Android.mk

LOCAL_PATH:=$(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := gtest_download
LOCAL_SRC_FILES := test.cpp

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src

LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS  -DHAVE_PTHREADS
LOCAL_LDFLAGS  += -pie
LOCAL_LDFLAGS  += -L$(LOCAL_PATH)/
LOCAL_LDLIBS +=  -llog -lz -lm
LOCAL_LDLIBS +=  -lgtest -lgtest_main
LOCAL_STATIC_LIBRARIES += download_a
include $(BUILD_EXECUTABLE)

$(call import-module,third_party/googletest)

gtest单元测试命令:

./gest_program --gtest_filter=dl_test.dl_http

你可能感兴趣的:(ndk开发-gtest编译)