Let’s assume you have got a great Android app, how do you go about diagnosing problems users have with it? One big problem with mobile apps is the fact that it is gone out of your hands and you can’t see what’s happening when things go wrong.
Ideally, you would ask the user to send you some logs, but it is not as easy as it should be. Some time ago you could install an app that would read logs for you, but in a fairly recent versions of Android they can only see their own logs which is not very useful. You could ask users to root their device, or install developer toolkit and use ADB. Good luck trying. This is when a simple diagnostic tools could be helpful, if it allowed users to send your application’s logs back to you. Please meet diagnostic-tools.
Here you have a jar file you should add to your project, which will give you a LogCollector. This class can read application logs and send them in an email with some information about the runtime environment. It actually doesn’t send email, it prepares it and lets user decide how to send it. Give it a try by installing the demo app or building it from source code.
What you need to do is (refer to the demo app if something is not clear):
Add this jar file to your Android project. You can also build it yourself from source code. However, the demo app defines a project dependency instead.
Add a provider as shown below to your application. This is needed by FileProvider to share the log file with the email sending app as an attachment.
<provider android:name=”android.support.v4.content.FileProvider” android:authorities=”com.codolutions.diagnostics.demo.fileprovider” android:exported=”false” android:grantUriPermissions=”true” >
<meta-data android:name=”android.support.FILE_PROVIDER_PATHS” android:resource=”@xml/file_provider_paths” />
Add the XML file mentioned in the configuration above, which specifies the path for the files exported by this provider.
<paths xmlns:android=”http://schemas.android.com/apk/res/android” >
<files-path name=”files_dir” path=”.” />
And finally, when you want to send logs, call the log collector.
mLogCollector.sendLogs(“email@example.com”, “Error Log”, “Hey there, here is your log!”, “Some extra information, just because you wanted.”, “com.codolutions.diagnostics.demo.fileprovider”);
Just one more thing that could be useful: If you are lucky enough to spot the issue and fix it, you go ahead and upload a new version, but there is no guarantee users will opt-in to your fix. One thing that can help with that is a kill switch. Using the kill switch you can remotely disable particular versions of your app and force users to upgrade to the latest. I’d recommend you to have at least a very basic kill switch from day 1, you never know when you are going to use it.
Credits: I have borrowed some code from https://code.google.com/p/android-log-collector/.