Sentry-java: Can Sentry be initialized in the Android Application class? If so, update docs

Created on 30 Jan 2019  路  7Comments  路  Source: getsentry/sentry-java

Currently, the documentation suggests that Sentry should be initialized in Activity.onCreate. However, if the application is ever started from a different Activity, this code will not be run. This code could be called from all of the activity entry points in the app but it's easy to forget.

In theory, a preferable approach would be to initialize Sentry in Application.onCreate, which runs before any Activities start. Is there any problem with doing this? If not, I'd recommend updating the docs to officially support this configuration.

All 7 comments

Anywhere should be fine, as long as you have access to the application Context object, which we use to automatically enrich the events with app state.

Great, thank you! You can close this bug unless you want to use it for something else, e.g. like updating the documentation.

Hi,
I moved sentry initializing from Activity.onCreate to Application.onCreate, and now no more error reported to UI.
Also this warning show:

No 'stacktrace.app.packages' was configured, this option is highly recommended as it affects stacktrace grouping and display on Sentry. See documentation: https://docs.sentry.io/clients/java/config/#in-application-stack-frames

Which place I should set this stacktrace.app.packages property?

I put in sentry.properties file inside of local.properties file:

stacktrace.app.packages=org.myapp.app

But still have this warning and also no errors reported.

Update: Fixed, and errors reported, it was network issue.
But still I have warning about stacktrace, I think it also existed before moving.

@omidraha I also encountered this message today. I am not sure, the sentry.properties file is used on Android. You can add the stacktrace.app.package to the DSN string that you pass into Sentry.init

Like https://<dsn-token>@sentry.url:9000&stacktrace.app.packages=org.myapp.app

@omidraha @lyio that message is logged by the DefaultSentryClientFactory when no stacktrace.app.package property is found either in the DSN or in the application jar resources, at runtime.

If you are using the provided AndroidSentryClientFactory, the in-app frame data will be automatically populated with the Android app package name if no value is found either in the DSN or in the app resources. If that is enough for your use case, you can simply ignore the warning.

Otherwise you should add a comma separated list of app package names in the runtime sentry.properties file that is bundled in your app, that means it must be stored either in your app's jar resources folder or any build-type, variant, flavor's resources folder (e.g. <your-project>/<app-module>/src/main/resources/)

We've update the docs to use Application instead.
https://docs.sentry.io/clients/java/integrations/#initialization

@ninniuz
I moved sentry.properties to <your-project>/<app-module>/src/main/resources/,
And now there is no warning.
Thank you so much.

@bruno-garcia
Thank you for updating docs to configure Sentry in Application.

Was this page helpful?
0 / 5 - 0 ratings