Skip to content

Commit

Permalink
BasicNativeAndroidTest: Move gtest to a single flavor
Browse files Browse the repository at this point in the history
The junit-gtest library will bring native test library and its c++
shared library into the final package. It's not easy to keep these
necessary shared libraries into androidTest flavor only. So this CL adds
a new flavor called nativeTest to include native test related libraries
and run it with androidTest. For normal usage, we can use core flavor,
the new default falvor without any native test related libraries.

Signed-off-by: utzcoz <[email protected]>
  • Loading branch information
utzcoz committed Oct 28, 2023
1 parent 910bd7c commit bcd2d99
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
11 changes: 10 additions & 1 deletion test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ for p in $(cat projects.conf); do
echo "====================================================================="

pushd $p > /dev/null # Silent pushd
./gradlew $@ testDebug nexusOneApi30DebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory

./gradlew $@ testDebug | sed "s@^@$p @" # Prefix every line with directory

if [ "$p" == "unit/BasicNativeAndroidTest" ]; then
./gradlew $@ nexusOneApi30CoreDebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory
./gradlew $@ nexusOneApi30NativeTestDebugAndroidTest --info | sed "s@^@$p @"
else
./gradlew $@ nexusOneApi30DebugAndroidTest --info | sed "s@^@$p @" # Prefix every line with directory
fi

code=${PIPESTATUS[0]}
if [ "$code" -ne "0" ]; then
exit $code
Expand Down
30 changes: 25 additions & 5 deletions unit/BasicNativeAndroidTest/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
arguments "-DANDROID_STL=c++_shared", "-DENABLE_NATIVE_TEST=OFF"
}
}
}

flavorDimensions = ["default"]

productFlavors {
core {
dimension "default"
applicationId = defaultConfig.applicationId
}
nativeTest {
dimension "default"
externalNativeBuild {
cmake {
arguments "-DENABLE_NATIVE_TEST=ON"
}
}
}
}

externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
Expand All @@ -47,7 +65,8 @@ android {
testOptions {
managedDevices {
devices {
// run with ../gradlew nexusOneApi30DebugAndroidTest
// run with ../gradlew nexusOneApi30CoreDebugAndroidTest without native tests
// or ../gradlew nexusOneApi30NativeTestDebugAndroidTest with native tests
nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) {
// A lower resolution device is used here for better emulator performance
device = "Nexus One"
Expand All @@ -62,9 +81,10 @@ android {
}

dependencies {
androidTestImplementation "junit:junit:$rootProject.junitVersion"
implementation "androidx.test.ext:junit-gtest:$rootProject.junitGtestVersion"
implementation "com.android.ndk.thirdparty:googletest:$rootProject.googletestVersion"
nativeTestImplementation "androidx.test.ext:junit-gtest:$rootProject.junitGtestVersion"
nativeTestImplementation "com.android.ndk.thirdparty:googletest:$rootProject.googletestVersion"
androidTestImplementation "androidx.test:runner:$rootProject.runnerVersion"
androidTestImplementation "androidx.test.ext:junit-ktx:$extJUnitVersion"
androidTestImplementation "com.google.truth:truth:" + rootProject.truthVersion
androidTestImplementation "junit:junit:$rootProject.junitVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.android.testing.nativesample

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class DefaultInstrumentationTest {
@Test
fun useAppContext() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertThat(appContext.packageName).isEqualTo("com.example.android.testing.nativesample")
}
}
14 changes: 8 additions & 6 deletions unit/BasicNativeAndroidTest/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ cmake_minimum_required(VERSION 3.10.2)

project(junit-gtest-example LANGUAGES CXX)

find_package(googletest REQUIRED CONFIG)
find_package(junit-gtest REQUIRED CONFIG)

include_directories(include)

add_library(adder SHARED src/adder.cpp)

add_library(adder-test SHARED test/adder_test.cpp)
if (ENABLE_NATIVE_TEST)
find_package(googletest REQUIRED CONFIG)
find_package(junit-gtest REQUIRED CONFIG)

add_library(adder-test SHARED test/adder_test.cpp)

target_link_libraries(adder-test
target_link_libraries(adder-test
PRIVATE
adder
googletest::gtest
junit-gtest::junit-gtest
)
)
endif()

0 comments on commit bcd2d99

Please sign in to comment.