在 Android NDK 上编译 Google 个测试用例?
Compiling Google Test cases on Android NDK?
我正在尝试使用 Google 测试框架为 Android NDK 代码编译一个简单的单元测试。我的代码几乎一字不差地遵循 README:
Application.mk
APP_STL := gnustl_shared
Android.mk
# Build module
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jniLib
LOCAL_SRC_FILES := jniLib.cpp
include $(BUILD_SHARED_LIBRARY)
# Build unit tests
include $(CLEAR_VARS)
LOCAL_MODULE := jniLib_unittest
LOCAL_SRC_FILES := jniLib_unittest.cpp
LOCAL_SHARED_LIBRARIES := jniLib
LOCAL_STATIC_LIBRARIES := googletest_main
include $(BUILD_EXECUTABLE)
$(call import-module,third_party/googletest)
jniLib.cpp
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y) {
return x + y;
}
jniLib.h
#pragma once
#include <jni.h>
extern "C" {
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y);
}
jniLib_unittest.cpp
#include <gtest/gtest.h>
Test(AddTest, FivePlusFive) {
EXPECT_EQ(10, add(5, 5));
}
当我尝试使用 ndk-build
构建时,出现以下编译错误:
jni/jniLib_unittest.cpp:10:9: error: expected constructor, destructor, or type
conversion before '(' token make.exe:
*** [obj/local/armeabi/objs/jniLib_unittest/jniLib_unittest.o] Error 1
我做错了什么?
我的代码有很多错误,但主要问题是测试声明区分大小写:它是 TEST
,而不是 Test
。我更正后的代码如下:
Application.mk
# Set APP_PLATFORM >= 16 to fix:
APP_PLATFORM = android-18
APP_STL := gnustl_shared
jniLib.cpp
#include "jniLib.h"
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y) {
return doAdd(x, y);
}
int doAdd(int x, int y) {
return x + y;
}
jniLib.h
#pragma once
#include <jni.h>
extern "C" {
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y);
}
int doAdd(int x, int y);
jniLib_unittest.cpp
#include <gtest/gtest.h>
/**
* Unit test the function 'add', not the JNI call
* 'Java_test_nativetest_MainActivity_add'
*/
TEST(AddTest, FivePlusFive) {
EXPECT_EQ(10, add(5, 5));
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
我正在尝试使用 Google 测试框架为 Android NDK 代码编译一个简单的单元测试。我的代码几乎一字不差地遵循 README:
Application.mk
APP_STL := gnustl_shared
Android.mk
# Build module
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jniLib
LOCAL_SRC_FILES := jniLib.cpp
include $(BUILD_SHARED_LIBRARY)
# Build unit tests
include $(CLEAR_VARS)
LOCAL_MODULE := jniLib_unittest
LOCAL_SRC_FILES := jniLib_unittest.cpp
LOCAL_SHARED_LIBRARIES := jniLib
LOCAL_STATIC_LIBRARIES := googletest_main
include $(BUILD_EXECUTABLE)
$(call import-module,third_party/googletest)
jniLib.cpp
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y) {
return x + y;
}
jniLib.h
#pragma once
#include <jni.h>
extern "C" {
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y);
}
jniLib_unittest.cpp
#include <gtest/gtest.h>
Test(AddTest, FivePlusFive) {
EXPECT_EQ(10, add(5, 5));
}
当我尝试使用 ndk-build
构建时,出现以下编译错误:
jni/jniLib_unittest.cpp:10:9: error: expected constructor, destructor, or type
conversion before '(' token make.exe:
*** [obj/local/armeabi/objs/jniLib_unittest/jniLib_unittest.o] Error 1
我做错了什么?
我的代码有很多错误,但主要问题是测试声明区分大小写:它是 TEST
,而不是 Test
。我更正后的代码如下:
Application.mk
# Set APP_PLATFORM >= 16 to fix:
APP_PLATFORM = android-18
APP_STL := gnustl_shared
jniLib.cpp
#include "jniLib.h"
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y) {
return doAdd(x, y);
}
int doAdd(int x, int y) {
return x + y;
}
jniLib.h
#pragma once
#include <jni.h>
extern "C" {
jint Java_test_nativetest_MainActivity_add(JNIEnv* env, jobject jThis, jint x, jint y);
}
int doAdd(int x, int y);
jniLib_unittest.cpp
#include <gtest/gtest.h>
/**
* Unit test the function 'add', not the JNI call
* 'Java_test_nativetest_MainActivity_add'
*/
TEST(AddTest, FivePlusFive) {
EXPECT_EQ(10, add(5, 5));
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}