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

Initializing the SDK programmatically by using FirebaseOptions cause crash when call URLConnection.getInputStream() #5584

Closed
yin-haoran opened this issue Dec 1, 2023 · 5 comments
Labels
api: performance type: bug Something isn't working

Comments

@yin-haoran
Copy link

Calling URLConnection.getInputStream() before FirebaseApp.initializeApp() will lead crash. Maybe due to the Bytecode instrumented functions com.google.firebase.perf.network.FirebasePerfUrlConnection#openStream(java.net.URL).

stack info as follow:

java.lang.ExceptionInInitializerError
	at com.google.firebase.perf.config.RemoteConfigManager.getInstance(RemoteConfigManager.java:119)
	at com.google.firebase.perf.config.ConfigResolver.<init>(ConfigResolver.java:78)
	at com.google.firebase.perf.config.ConfigResolver.getInstance(ConfigResolver.java:86)
	at com.google.firebase.perf.application.AppStateMonitor.<init>(AppStateMonitor.java:98)
	at com.google.firebase.perf.application.AppStateMonitor.getInstance(AppStateMonitor.java:87)
	at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.<init>(NetworkRequestMetricBuilder.java:92)
	at com.google.firebase.perf.metrics.NetworkRequestMetricBuilder.builder(NetworkRequestMetricBuilder.java:84)
	at com.google.firebase.perf.network.FirebasePerfUrlConnection.openStream(FirebasePerfUrlConnection.java:63)
	at com.google.firebase.perf.network.FirebasePerfUrlConnection.openStream(FirebasePerfUrlConnection.java:47)
	at com.tony.firebasetest.MainActivity.onCreate(MainActivity.kt:38)
	at android.app.Activity.performCreate(Activity.java:8000)
	at android.app.Activity.performCreate(Activity.java:7984)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3600)
	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)

I provide a example of this issue.

@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@yin-haoran
Copy link
Author

Hi @argzdev, could you see this issue please.

@argzdev
Copy link
Contributor

argzdev commented Dec 1, 2023

Hey @yin-haoran, thanks for bringing this up. I was also able to exhibit the same behavior. I tried following our documentation on disabling the performance monitoring library, but this doesn't seem to work when Firebase is initialized programmatically.

        <meta-data
            android:name="firebase_performance_collection_enabled"
            android:value="false" />

Let me reach out to our engineering team and see what we can do here.

@argzdev argzdev added the type: bug Something isn't working label Dec 1, 2023
@yin-haoran
Copy link
Author

Hi @argzdev , is there any progress now?

argzdev added a commit that referenced this issue May 1, 2024
…zed (#5885)

Proposed fix for #5584:

### Issue
Crash happens if a URL connection is opened e.g. `url.openStream()` when
`TransportManager` has not been initialized. If developer wants to
programmatically enable Firebase using FirebaseOptions, but have
included the Firebase Perf plugin, the app will crash after doing a
network call.

### Steps to reproduce
1. Add Firebase perf plugin
- Module `build.gradle`
```
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    id("com.google.gms.google-services") version "4.4.1" apply false
    id("com.google.firebase.firebase-perf") version "1.4.2" apply false
}
```
- App `build.gradle`
```
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id("com.google.gms.google-services")
    id("com.google.firebase.firebase-perf")
}
```
2. Disable Firebase
```xml
        <provider
            android:name="com.google.firebase.provider.FirebaseInitProvider"
            android:authorities="${applicationId}.firebaseinitprovider"
            tools:node="remove"
            />   
```
3. Do a network call
```
val url = URL("https", "www.amazon.com", 443, "")
val inputStream = url.openStream()
```

### Investigation
1. `openStream` in `FirebasePerfUrlConnection` intercepts all urls that
opens connections.
5. `openStream` requires `TransportManager` to be initialized
6. `TransportManager` is only initialized after `FirebasePerformance`
has been initialized

### Solution
If `TransportManager` is not initialized, we should simply return the
URL openStream and not intercept the connection.
rlazo pushed a commit that referenced this issue May 2, 2024
…zed (#5885)

Proposed fix for #5584:

### Issue
Crash happens if a URL connection is opened e.g. `url.openStream()` when
`TransportManager` has not been initialized. If developer wants to
programmatically enable Firebase using FirebaseOptions, but have
included the Firebase Perf plugin, the app will crash after doing a
network call.

### Steps to reproduce
1. Add Firebase perf plugin
- Module `build.gradle`
```
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    id("com.google.gms.google-services") version "4.4.1" apply false
    id("com.google.firebase.firebase-perf") version "1.4.2" apply false
}
```
- App `build.gradle`
```
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id("com.google.gms.google-services")
    id("com.google.firebase.firebase-perf")
}
```
2. Disable Firebase
```xml
        <provider
            android:name="com.google.firebase.provider.FirebaseInitProvider"
            android:authorities="${applicationId}.firebaseinitprovider"
            tools:node="remove"
            />   
```
3. Do a network call
```
val url = URL("https", "www.amazon.com", 443, "")
val inputStream = url.openStream()
```

### Investigation
1. `openStream` in `FirebasePerfUrlConnection` intercepts all urls that
opens connections.
5. `openStream` requires `TransportManager` to be initialized
6. `TransportManager` is only initialized after `FirebasePerformance`
has been initialized

### Solution
If `TransportManager` is not initialized, we should simply return the
URL openStream and not intercept the connection.
@lehcar09
Copy link

Hi @yin-haoran, the fix for this issue has been released in BOM version 33.1.0. I'll be closing this issue now. If you run into any other problems please open a new issue. Thank you for patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: performance type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants