Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasicNativeAndroidTest: Move gtest to a single flavor #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()