我的项目结构如下:
project
∟ app // apply `google-services` and `firebase-crashlytics` plugins here
∟ google-services.json
∟ lib1 // including Firebase SDK, and Firebase related operations here
∟ lib2
在正常运行应用程序时,它表现良好。但是当我在lib1
中运行androidTest时,我遇到了以下异常:
Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this XXX. Make sure to call FirebaseApp.initializeApp(Context) first.
我尝试通过在/androidTest/values/res/
中放置一个带有google-services
相关配置的strings.xml
文件来解决它。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<! -- Present in all applications -->
<string name="google_app_id" translatable="false">client_info/mobilesdk_app_id</string>
<! -- Present in applications with the appropriate services configured -->
<string name="gcm_defaultSenderId" translatable="false">${project_info/project_number}</string>
<string name="default_web_client_id" translatable="false">${oauth_client/client_id}</string>
<string name="ga_trackingId" translatable="false">${analytics_property/tracking_id}</string>
<string name="firebase_database_url" translatable="false">${project_info/firebase_url}</string>
<string name="google_api_key" translatable="false">${api_key/current_key}</string>
<string name="google_crash_reporting_api_key" translatable="false">${api_key/current_key}</string>
<string name="project_id" translatable="false">${project_info/project_id}</string>
</resources>
然而,我遇到了另一个问题
java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.IllegalStateException: The Crashlytics build ID is missing. This occurs when Crashlytics tooling is absent from your app's build configuration. Please review Crashlytics onboarding instructions and ensure you have a valid Crashlytics account.
我曾尝试在lib1
模块中应用firebase-crashlytics
插件,但出现了错误。
Caused by: org.gradle.api.GradleException: Applying the Firebase Crashlytics plugin to a library project is unsupported. It should only be applied to the application module of your project to enable automatic upload of obfuscation mapping files for your application.
所以我必须保留firebase-crashlytics
插件在app
模块中。
然后我尝试将测试移动到app
模块中,但是遇到了另一个错误。
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/internal/runner/tracker/UsageTrackerRegistry;
at androidx.test.espresso.GraphHolder.baseLayer(GraphHolder.java:9)
at androidx.test.espresso.Espresso.<clinit>(Espresso.java:2)
at androidx.test.espresso.Espresso.onView(Espresso.java:38)
欢迎任何帮助。谢谢。