diff --git a/BUILD.gn b/BUILD.gn index a2918645f8..3aa0ae58f7 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -643,7 +643,12 @@ if (rtc_include_tests && !build_with_chromium) { ] data = video_engine_tests_resources if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] shard_timeout = 900 } if (is_ios) { @@ -688,7 +693,12 @@ if (rtc_include_tests && !build_with_chromium) { data = webrtc_perf_tests_resources if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] shard_timeout = 4500 } if (is_ios) { diff --git a/audio/BUILD.gn b/audio/BUILD.gn index f90c136b98..31980e45bb 100644 --- a/audio/BUILD.gn +++ b/audio/BUILD.gn @@ -243,7 +243,12 @@ if (rtc_include_tests) { "//third_party/abseil-cpp/absl/strings", ] if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] } data = [ "../resources/voice_engine/audio_tiny16.wav", diff --git a/modules/BUILD.gn b/modules/BUILD.gn index 54dffe0a63..d24bf64cdb 100644 --- a/modules/BUILD.gn +++ b/modules/BUILD.gn @@ -82,12 +82,14 @@ if (rtc_include_tests && !build_with_chromium) { data = modules_tests_resources if (is_android) { + use_default_launcher = false deps += [ # NOTE(brandtr): Including Java classes seems only to be possible from # rtc_test targets. Therefore we include this target here, instead of # in video_coding_modules_tests, where it is actually used. "../sdk/android:libjingle_peerconnection_java", - "//testing/android/native_test:native_test_native_code", + "//sdk/android:native_test_jni_onload", + "//testing/android/native_test:native_test_support", ] shard_timeout = 900 } @@ -231,6 +233,7 @@ if (rtc_include_tests && !build_with_chromium) { data = modules_unittests_resources if (is_android) { + use_default_launcher = false deps += [ "../sdk/android:libjingle_peerconnection_java", "//testing/android/native_test:native_test_support", diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn index f78c5260c5..aa06e7b56c 100644 --- a/modules/audio_coding/BUILD.gn +++ b/modules/audio_coding/BUILD.gn @@ -1543,7 +1543,12 @@ if (rtc_include_tests) { data = audio_decoder_unittests_resources if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] shard_timeout = 900 } if (is_ios) { @@ -1634,7 +1639,12 @@ if (rtc_include_tests) { data = audio_codec_speed_tests_resources if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] shard_timeout = 900 } diff --git a/modules/audio_device/BUILD.gn b/modules/audio_device/BUILD.gn index ad9b49ee53..b09aaa7c8a 100644 --- a/modules/audio_device/BUILD.gn +++ b/modules/audio_device/BUILD.gn @@ -441,11 +441,6 @@ if (rtc_include_tests && !build_with_chromium) { ] } if (is_android) { - # Need to disable error due to the line in - # base/android/jni_android.h triggering it: - # const BASE_EXPORT jobject GetApplicationContext() - # error: type qualifiers ignored on function return type - cflags = [ "-Wno-ignored-qualifiers" ] sources += [ "android/audio_device_unittest.cc", "android/audio_manager_unittest.cc", @@ -453,8 +448,10 @@ if (rtc_include_tests && !build_with_chromium) { "android/ensure_initialized.h", ] deps += [ - "../../base", + "../../sdk/android:internal_jni", "../../sdk/android:libjingle_peerconnection_java", + "../../sdk/android:native_api_jni", + "../../sdk/android:native_test_jni_onload", ] } if (!rtc_include_internal_audio_device) { diff --git a/modules/audio_device/DEPS b/modules/audio_device/DEPS index fc5eed7833..9cc627d330 100644 --- a/modules/audio_device/DEPS +++ b/modules/audio_device/DEPS @@ -5,7 +5,7 @@ include_rules = [ specific_include_rules = { "ensure_initialized\.cc": [ - "+base/android", + "+sdk/android", ], "audio_device_impl\.cc": [ "+sdk/objc", diff --git a/modules/audio_device/android/ensure_initialized.cc b/modules/audio_device/android/ensure_initialized.cc index 37086cc9eb..59e9c8f7a6 100644 --- a/modules/audio_device/android/ensure_initialized.cc +++ b/modules/audio_device/android/ensure_initialized.cc @@ -10,19 +10,13 @@ #include "modules/audio_device/android/ensure_initialized.h" +#include #include +#include -#include "rtc_base/ignore_wundef.h" - -// Note: this dependency is dangerous since it reaches into Chromium's base. -// There's a risk of e.g. macro clashes. This file may only be used in tests. -RTC_PUSH_IGNORING_WUNDEF() -#include "base/android/jni_android.h" -RTC_POP_IGNORING_WUNDEF() -#include "modules/audio_device/android/audio_record_jni.h" -#include "modules/audio_device/android/audio_track_jni.h" #include "modules/utility/include/jvm_android.h" #include "rtc_base/checks.h" +#include "sdk/android/src/jni/jvm.h" namespace webrtc { namespace audiodevicemodule { @@ -30,8 +24,9 @@ namespace audiodevicemodule { static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; void EnsureInitializedOnce() { - RTC_CHECK(::base::android::IsVMInitialized()); - JNIEnv* jni = ::base::android::AttachCurrentThread(); + RTC_CHECK(::webrtc::jni::GetJVM() != nullptr); + + JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded(); JavaVM* jvm = NULL; RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm)); diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn index 260c9f619b..77162ef3fd 100644 --- a/modules/video_coding/BUILD.gn +++ b/modules/video_coding/BUILD.gn @@ -826,13 +826,14 @@ if (rtc_include_tests) { deps = [ "../../api/video_codecs:video_codecs_api", + "../../modules/utility:utility", "../../rtc_base:checks", "../../rtc_base:ignore_wundef", "../../rtc_base:rtc_base_approved", + "../../sdk/android:internal_jni", "../../sdk/android:native_api_base", "../../sdk/android:native_api_codecs", "../../sdk/android:native_api_jni", - "//base", ] } } diff --git a/modules/video_coding/codecs/test/android_codec_factory_helper.cc b/modules/video_coding/codecs/test/android_codec_factory_helper.cc index 1227b1fbbe..d1be684cbb 100644 --- a/modules/video_coding/codecs/test/android_codec_factory_helper.cc +++ b/modules/video_coding/codecs/test/android_codec_factory_helper.cc @@ -10,25 +10,19 @@ #include "modules/video_coding/codecs/test/android_codec_factory_helper.h" +#include #include +#include #include +#include "modules/utility/include/jvm_android.h" #include "rtc_base/checks.h" -#include "rtc_base/ignore_wundef.h" -#include "sdk/android/native_api/base/init.h" #include "sdk/android/native_api/codecs/wrapper.h" #include "sdk/android/native_api/jni/class_loader.h" #include "sdk/android/native_api/jni/jvm.h" #include "sdk/android/native_api/jni/scoped_java_ref.h" - -// Note: this dependency is dangerous since it reaches into Chromium's base. -// There's a risk of e.g. macro clashes. This file may only be used in tests. -// Since we use Chrome's build system for creating the gtest binary, this should -// be fine. -RTC_PUSH_IGNORING_WUNDEF() -#include "base/android/jni_android.h" -RTC_POP_IGNORING_WUNDEF() +#include "sdk/android/src/jni/jvm.h" namespace webrtc { namespace test { @@ -37,16 +31,15 @@ namespace { static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; -// There can only be one JNI_OnLoad in each binary. So since this is a GTEST -// C++ runner binary, we want to initialize the same global objects we normally -// do if this had been a Java binary. void EnsureInitializedOnce() { - RTC_CHECK(::base::android::IsVMInitialized()); - JNIEnv* env = ::base::android::AttachCurrentThread(); - JavaVM* jvm = nullptr; - RTC_CHECK_EQ(0, env->GetJavaVM(&jvm)); + RTC_CHECK(::webrtc::jni::GetJVM() != nullptr); - InitAndroid(jvm); + JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded(); + JavaVM* jvm = NULL; + RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm)); + + // Initialize the Java environment (currently only used by the audio manager). + webrtc::JVM::Initialize(jvm); } } // namespace diff --git a/pc/BUILD.gn b/pc/BUILD.gn index 62c7939512..0704abee19 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -2150,7 +2150,12 @@ if (rtc_include_tests && !build_with_chromium) { } if (is_android) { - deps += [ "//testing/android/native_test:native_test_support" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] } } @@ -2393,8 +2398,18 @@ if (rtc_include_tests && !build_with_chromium) { "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/types:optional", ] + if (is_android) { - deps += [ ":android_black_magic" ] + use_default_launcher = false + deps += [ + ":android_black_magic", + + # We need to depend on this one directly, or classloads will fail for + # the voice engine BuildInfo, for instance. + "//sdk/android:libjingle_peerconnection_java", + "//sdk/android:native_test_jni_onload", + ] + shard_timeout = 900 } deps += [ @@ -2432,33 +2447,23 @@ if (rtc_include_tests && !build_with_chromium) { "../test:test_main", "../test:test_support", ] - - if (is_android) { - deps += [ - "//testing/android/native_test:native_test_support", - - # We need to depend on this one directly, or classloads will fail for - # the voice engine BuildInfo, for instance. - "../sdk/android:libjingle_peerconnection_java", - ] - - shard_timeout = 900 - } } if (is_android) { rtc_library("android_black_magic") { - # The android code uses hacky includes to chromium-base and the ssl code; - # having this in a separate target enables us to keep the peerconnection - # unit tests clean. - check_includes = false + # The android code uses hacky includes to ssl code. Having this in a + # separate target enables us to keep the peerconnection unit tests clean. testonly = true sources = [ "test/android_test_initializer.cc", "test/android_test_initializer.h", ] deps = [ + "../sdk/android:internal_jni", "../sdk/android:libjingle_peerconnection_jni", + "//modules/utility:utility", + "//rtc_base:checks", + "//rtc_base:rtc_base", "//testing/android/native_test:native_test_support", ] } diff --git a/pc/test/DEPS b/pc/test/DEPS index fff21d98ea..33e6d94b25 100644 --- a/pc/test/DEPS +++ b/pc/test/DEPS @@ -1,6 +1,5 @@ include_rules = [ - # Allow include of Chrome base/android to allow inclusion of headers needed - # for accessing the JVM and Application context in gtest. - "+base/android", + # Allow include of sdk/android to allow accessing the JVM and Env in tests. + "+sdk/android", "+modules/utility/include/jvm_android.h", ] diff --git a/pc/test/android_test_initializer.cc b/pc/test/android_test_initializer.cc index 584aedaaed..963544cb4b 100644 --- a/pc/test/android_test_initializer.cc +++ b/pc/test/android_test_initializer.cc @@ -10,22 +10,14 @@ #include "pc/test/android_test_initializer.h" +#include #include - -#include "rtc_base/ignore_wundef.h" - -// Note: this dependency is dangerous since it reaches into Chromium's base. -// There's a risk of e.g. macro clashes. This file may only be used in tests. -// Since we use Chromes build system for creating the gtest binary, this should -// be fine. -RTC_PUSH_IGNORING_WUNDEF() -#include "base/android/jni_android.h" -RTC_POP_IGNORING_WUNDEF() +#include #include "modules/utility/include/jvm_android.h" #include "rtc_base/checks.h" - -// TODO(phoglund): This include is also to a target we can't really depend on. +#include "sdk/android/src/jni/jvm.h" +// TODO(phoglund): This include is to a target we can't really depend on. // We need to either break it out into a smaller target or find some way to // not use it. #include "rtc_base/ssl_adapter.h" @@ -40,8 +32,8 @@ static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; // C++ runner binary, we want to initialize the same global objects we normally // do if this had been a Java binary. void EnsureInitializedOnce() { - RTC_CHECK(::base::android::IsVMInitialized()); - JNIEnv* jni = ::base::android::AttachCurrentThread(); + RTC_CHECK(::webrtc::jni::GetJVM() != nullptr); + JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded(); JavaVM* jvm = NULL; RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm)); diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index 8ff0f8aad7..e3b2e8ad15 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -1599,6 +1599,20 @@ if (is_android) { ] } + rtc_library("native_test_jni_onload") { + testonly = true + + sources = [ "native_unittests/test_jni_onload.cc" ] + + deps = [ + ":base_jni", + ":internal_jni", + ":native_api_base", + ":native_api_jni", + "../../rtc_base:checks", + ] + } + rtc_library("native_unittests") { testonly = true @@ -1611,7 +1625,6 @@ if (is_android) { "native_unittests/java_types_unittest.cc", "native_unittests/peerconnection/peer_connection_factory_unittest.cc", "native_unittests/stacktrace/stacktrace_unittest.cc", - "native_unittests/test_jni_onload.cc", "native_unittests/video/video_source_unittest.cc", ] @@ -1632,6 +1645,7 @@ if (is_android) { ":native_api_peerconnection", ":native_api_stacktrace", ":native_api_video", + ":native_test_jni_onload", ":opensles_audio_device_module", ":video_jni", "../../api:field_trials_view", diff --git a/stats/BUILD.gn b/stats/BUILD.gn index c636e044d8..54fc7b27dd 100644 --- a/stats/BUILD.gn +++ b/stats/BUILD.gn @@ -67,7 +67,12 @@ if (rtc_include_tests && !build_with_chromium) { ] if (is_android) { - deps += [ "//testing/android/native_test:native_test_native_code" ] + use_default_launcher = false + deps += [ + "//build/android/gtest_apk:native_test_instrumentation_test_runner_java", + "//testing/android/native_test:native_test_java", + "//testing/android/native_test:native_test_support", + ] } } }