mirror of
https://github.com/android/ndk-samples
synced 2025-11-05 06:55:52 +08:00
License text, setup instructions, and support info do not need to be duplicated into every README, since they're already in the top-level README. They were originally in each sample because the samples used to each be separate Android Studio projects with different requirements that could be checkout out independently in Android Studio. This is no longer the case. Most of the docs also included text along the lines of "This sample uses the new Android Studio with CMake support", which hasn't been new since 2015, so doesn't really need to be said. The prerequisites were mostly not true. Android Studio is not required for any of this. Cloning the repo and running `./gradlew build` is sufficient to build all the samples. They also were not being kept up-to-date at all, since they mostly said "Android Studio 2.2 or newer", which was definitely not true. I'm not what the oldest version of Android Studio that will work here is, but I don't actually test anything but the latest so claiming anything otherwise is just misleading. I haven't pruned or edited any of the real content of the docs. I'm sure there are plenty of edits to be made there and a lot of expansion to do, but those changes will be less mechanical and will happen separately.
Unit Test
This Android sample shows how to write unit tests for native code with googletest, and run them on-device.
Writing unit tests
This app has a very simple library containing a function to add two numbers.
#pragma once
int add(int a, int b);
#include "adder.h"
int add(int a, int b) { return a + b; }
adder_test.cpp contains a unit test for this function:
#include "adder.h"
#include "gtest/gtest.h"
TEST(adder, adder) { EXPECT_EQ(3, add(1, 2)); }
Building and running the tests
We need to add a library for the test to CMakeLists.txt, which depends on googletest and junit-gtest:
find_package(googletest REQUIRED CONFIG)
find_package(junit-gtest REQUIRED CONFIG)
add_library(app_tests SHARED adder_test.cpp)
target_link_libraries(app_tests
PRIVATE
$<TARGET_OBJECTS:adder>
googletest::gtest
junit-gtest::junit-gtest
)
We need to add googletest and junit-gtest as dependencies in app/build.gradle. Googletest uses prefab, so we enable that.
build_features {
prefab true
}
dependencies {
implementation 'androidx.test.ext:junit-gtest:1.0.0-alpha01'
implementation 'com.android.ndk.thirdparty:googletest:1.11.0-beta-1'
}
Finally, we need a wrapper in androidTest, NativeTests.kt:
package com.example.unittest
import androidx.test.ext.junitgtest.GtestRunner
import androidx.test.ext.junitgtest.TargetLibrary
import org.junit.runner.RunWith
@RunWith(GtestRunner::class)
@TargetLibrary(libraryName = "app_tests")
class NativeTests
You can run the test on a phone or emulator as described in the Android developer documentation.
Try deliberately breaking the test, like this:
EXPECT_EQ(4, add(1, 2));
You should see a failure message like this:
java.lang.AssertionError:
/path/to/ndk-samples/unit-test/app/src/main/cpp/adder_test.cpp:6
Expected equality of these values:
4
add(1,2)
Which is: 3
