トラッキング コード

3/11/2012

How to use Google Test on Android-4.0.3_r1

AOSP has Google Test project in extarnal/gtest directory.

I try to use Google Test in my Application.

Create Android.mk for Google Test

I was referring to the existing Android.mk in extarnal/gtest/test.

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

# Gtest depends on STLPort which does not build on host/simulator.

ifeq ($(BUILD_WITH_ASTL),true)
libgtest_test_includes := \
    bionic/libstdc++/include \
    external/astl/include \
    external/gtest/include \
 $(LOCAL_PATH)/../test \
 $(LOCAL_PATH)/..
libgtest_test_static_lib := libgtest_main libgtest libastl
libgtest_test_shared_lib :=
libgtest_test_host_static_lib := libgtest_main_host libgtest_host libastl_host
libgtest_test_host_shared_lib :=
else
# BUILD_WITH_ASTL could be undefined, force it to false (for the guard
# before the test-target call).
BUILD_WITH_ASTL := false
libgtest_test_includes := \
    bionic \
    external/stlport/stlport \
    external/gtest/include \
 $(LOCAL_PATH)/../test \
 $(LOCAL_PATH)/..
libgtest_test_static_lib := libgtest_main libgtest
libgtest_test_shared_lib := libstlport
libgtest_test_host_static_lib :=
libgtest_test_host_shared_lib :=
endif


LOCAL_MODULE_TAGS := TestAppGtest
LOCAL_C_INCLUDES := $(libgtest_test_includes)

LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libutils \
    libandroid_runtime \
    $(libgtest_test_shared_lib) \
    $(libgtest_test_host_shared_lib)

LOCAL_STATIC_LIBRARIES := \
    $(libgtest_test_static_lib) \
    $(libgtest_test_host_static_lib)

# if LOCAL_SRC_FILES is .cc
#LOCAL_CPP_EXTENSION := .cc

# YOUR Source and Test Source
LOCAL_SRC_FILES := \
    sample1.cpp \
    sample1_unittest.cpp

LOCAL_MODULE := gtest_mytest
LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)

include $(BUILD_EXECUTABLE)


Building Google Test

I try to build an apllicaion with google test code.

I uploaded sample project.
https://docs.google.com/open?id=0BwdCdBWuE_7cb2ZzVGt3eHhSOEtEbGVxNnRCSkFJdw

$ cd "YOUR_BUILD_DIRECTORY"
$  . build/envsetup.sh
$ lunch "YOUR_BUILD_LUNCH"
$ make
$ cd packages/apps/
$ cd TestApp
$ mm -B


If building success , executable file is created.
target thumb C++: gtest_mytest <= packages/apps/TestApp/jni/test/sample1.cpp
target thumb C++: gtest_mytest <= packages/apps/TestApp/jni/test/sample1_unittest.cpp
target Executable: gtest_mytest (out/target/product/maguro/obj/EXECUTABLES/gtest_mytest_intermediates/LINKED/gtest_mytest)
target Symbolic: gtest_mytest (out/target/product/maguro/symbols/data/app/gtest_mytest)
target Strip: gtest_mytest (out/target/product/maguro/obj/EXECUTABLES/gtest_mytest_intermediates/gtest_mytest)
Install: out/target/product/maguro/data/app/gtest_mytest

Running Google Test

I try to run Google Test on Galaxy Nexus. My Galaxy Nexus is flashed Android-4.0.3_r1. Copy to executable file.
cd out/target/product/maguro/
$ adb push data/app/gtest_mytest /data/app
2870 KB/s (48728 bytes in 0.016s)
Running test using ADB.
$ adb shell
root@android:/ # cd data/app                                                   
root@android:/data/app # ./gtest_mytest    
Success to run, The test results are displayed.
root@android:/data/app # ./gtest_mytest                                        
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive
[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive
[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran.
[  PASSED  ] 6 tests.

2 comments:

  1. Great stuff mate! This helped a lot.

    ReplyDelete
  2. This is good sample. Can you share your AndroidManifest.xml?

    ReplyDelete