Understanding .trace file of an Android application - android

My application gives ANR popup. To find out the reason I have generated a sample.trace file using this line:
Debug.startMethodTracing("sample");
in onCreate method of my activity, and I have added this line to stop tracing to the onDestroy method of the activity:
Debug.stopMethodTracing();
I get the sample.trace file after I destroy the activity. But when I review the file I do not understand it. As I learn there is an online tool which is Perfetto.
However when I open the file in there, it shows blank page while I am expecting some UI.
As a result I need to understand the file and find out the problems that cause the ANR issue but I do not know how to do.
This is a piece of my sample.trace file :
*version
3
data-file-overflow=true
clock=dual
elapsed-time-usec=165959496
num-method-calls=599184
clock-call-overhead-nsec=3338
vm=art
pid=13695
*threads
14436 RxIoScheduler-2
14460 Okio Watchdog
14589 AsyncTask #5
14612 Timer-0
15013 AsyncTask #6
15014 AsyncTask #7
15015 AsyncTask #8
15049 Timer-4
13695 main
13718 Jit thread pool worker thread 0
13724 Signal Catcher
13726 ADB-JDWP Connection Control Thread
13728 FinalizerDaemon
13729 FinalizerWatchdogDaemon
13727 ReferenceQueueDaemon
13730 HeapTaskDaemon
13732 Binder:13695_1
13733 Binder:13695_2
13736 Binder:13695_3
13895 Profile Saver
13909 Queue
13910 Queue
13911 Queue
13912 Queue
13913 Queue
13914 Queue
13916 Queue
13917 Queue
13915 Queue
13936 Answers Events Handler1
13937 queued-work-looper
13941 Crashlytics Exception Handler1
13950 StethoListener-main
13970 OkHttp ConnectionPool
13977 Binder:13695_4
13978 process reaper
13980 process reaper
14016 RenderThread
14023 Binder:13695_5
14026 Binder:13695_6
14021 Thread-16
14152 Thread-17
14153 Thread-18
14156 Studio:InputCon
14165 magnifier pixel copy result handler
14151 Thread-19
14435 RxIoScheduler-1 (Evictor)
14459 OkHttp ConnectionPool
14476 RealmFinalizingDaemon
14480 pool-3-thread-1
14481 pool-3-thread-2
14482 pool-3-thread-3
14483 pool-3-thread-4
14484 pool-3-thread-5
14487 pool-3-thread-6
14489 pool-3-thread-7
14490 pool-3-thread-8
14491 pool-3-thread-9
14602 glide-disk-cache-thread-0
14604 glide-source-thread-0
14614 Timer-1
14615 IntentService[]
14628 glide-source-thread-1
14630 OkHttp ConnectionPool
14633 glide-active-resources
14780 pool-3-thread-10
14788 Picasso-Stats
14789 Picasso-Dispatcher
14790 Picasso-refQueue
14805 Timer-3
14973 SpotScheduleThread
14974 PlaylistScheduleThread
14980 pool-3-thread-11
15000 pool-3-thread-12
15021 RxComputationScheduler-1
15025 RxComputationScheduler-2
15028 OkHttp ConnectionPool
15050 Timer-5
15052 pool-3-thread-13
15053 pool-3-thread-14
15767 pool-3-thread-15
15771 pool-3-thread-16
15772 pool-3-thread-17
*methods
0x18c8 java.lang.BootClassLoader getInstance ()Ljava/lang/BootClassLoader; ClassLoader.java
0x6ba0 java.lang.BootClassLoader findClass (Ljava/lang/String;)Ljava/lang/Class; ClassLoader.java
0x6b9c java.lang.BootClassLoader loadClass (Ljava/lang/String;Z)Ljava/lang/Class; ClassLoader.java
0x7878 java.lang.ClassLoader <init> (Ljava/lang/ClassLoader;)V ClassLoader.java
0x7880 java.lang.ClassLoader <init> (Ljava/lang/Void;Ljava/lang/ClassLoader;)V ClassLoader.java
Thanks in advance.

As I learned I can use Android Studio to see which process is taking more place on cpu and etc.
The only thing that I need to do is opening Profiler window then click the + button and Load from file. Then it will show the statistics of the .trace file.
Thanks!

Related

How to resolve ANR for MapsInitializer.initialize?

The MapsInitializer.initialize method forces you to be called on main thread, and yet it causes ANRs.
ANR:
ii: Application Not Responding for at least 5000 ms.
at android.os.BinderProxy.transactNative(Binder.java)
at android.os.BinderProxy.transact(Binder.java:511)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:644)
at android.content.ContentResolver.call(ContentResolver.java:1447)
at com.google.maps.api.android.lib6.drd.o.A(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:41)
at com.google.maps.api.android.lib6.impl.ay.a(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:8)
at fjj.a(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:1)
at com.google.android.gms.maps.internal.CreatorImpl.c(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:35)
at com.google.android.gms.maps.internal.CreatorImpl.logInitialization(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:1)
at com.google.android.gms.maps.internal.i.bo(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:5)
at baw.onTransact(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200031.477283333.477283333:4)
at android.os.Binder.transact(Binder.java:392)
at js8.A(com.google.android.gms:play-services-maps##18.1.0:8)
at h49.N0(com.google.android.gms:play-services-maps##18.1.0:13)
at q84.a(com.google.android.gms:play-services-maps##18.1.0:99)
Running on non main thread:
java.lang.IllegalStateException: Method addObserver must be called on the main thread
at xy.j(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:0)
at xy.b(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:1)
at com.google.android.libraries.geo.crash.monitoring.UncleanTerminationDetector.i(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:6)
at com.google.android.gms.maps.internal.CreatorImpl.c(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:53)
at com.google.android.gms.maps.internal.CreatorImpl.logInitialization(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:1)
at com.google.android.gms.maps.internal.i.bm(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:5)
at ayk.onTransact(:com.google.android.gms.policy_maps_core_dynamite#223812200#223812200065.477283333.477283333:4)
at android.os.Binder.transact(Binder.java:1164)
at com.google.android.gms.internal.maps.zza.zzc(com.google.android.gms:play-services-maps##18.1.0:2)
at com.google.android.gms.maps.internal.zze.zzl(com.google.android.gms:play-services-maps##18.1.0:4)
at com.google.android.gms.maps.MapsInitializer.initialize(com.google.android.gms:play-services-maps##18.1.0:12)

FusedLocationApi.requestLocationUpdates failed

I have a form that I open in a webview in my app, after I complete it, it will appear a button that will let me download a pdf andd open it but when I do this I keep getting the next error:
OneSignal: FusedLocationApi.requestLocationUpdates failed!
java.lang.NullPointerException: Calling thread must be a prepared Looper thread.
at com.google.android.gms.common.internal.Preconditions.checkNotNull(com.google.android.gms:play-services-basement##17.6.0:2)
at com.google.android.gms.internal.location.zzz.requestLocationUpdates(com.google.android.gms:play-services-location##18.0.0:4)
at com.onesignal.GMSLocationController$FusedLocationApiWrapper.requestLocationUpdates(GMSLocationController.java:198)
at com.onesignal.GMSLocationController$LocationUpdateListener.init(GMSLocationController.java:181)
at com.onesignal.GMSLocationController$LocationUpdateListener.<init>(GMSLocationController.java:165)
at com.onesignal.GMSLocationController.onFocusChange(GMSLocationController.java:117)
at com.onesignal.LocationController.onFocusChange(LocationController.java:326)
at com.onesignal.OneSignal.onAppLostFocus(OneSignal.java:1308)
at com.onesignal.ActivityLifecycleHandler$AppFocusRunnable.run(ActivityLifecycleHandler.java:279)
at java.lang.Thread.run(Thread.java:923)
The button will appear in the webview. I think I get that error because I open the pdf on the main thread.

Android ANR in "android.app.ResourcesManager"

Recently i see a quite significant increase of ANRs in the Google Play Console for my app. There are reports for different types of causes but they have one thing in common:
Both the main thread and my worker thread are blocked and at least one of them is always blocked while accessing a method from "android.app.ResourcesManager"
Example 1:
"main" prio=5 tid=1 Blocked
at android.app.ResourcesManager.getAdjustedDisplay (ResourcesManager.java:353)
at android.app.ContextImpl.getDisplayNoVerify (ContextImpl.java:2559)
at android.view.WindowManagerImpl.getDefaultDisplay (WindowManagerImpl.java:164)
at org.n277.lynxlauncher.helper.DeviceUtilities.updateSystemHelper (DeviceUtilities.java:225)
at org.n277.lynxlauncher.views.DragContainerFrameLayout.onApplyWindowInsets (DragContainerFrameLayout.java:231)
at android.view.View.dispatchApplyWindowInsets (View.java:11399)
at android.view.ViewGroup.dispatchApplyWindowInsets (ViewGroup.java:7393)
at android.view.ViewGroup.newDispatchApplyWindowInsets (ViewGroup.java:7418)
at android.view.ViewGroup.dispatchApplyWindowInsets (ViewGroup.java:7400)
at android.view.ViewGroup.newDispatchApplyWindowInsets (ViewGroup.java:7418)
...
"Lynx-Worker" prio=5 tid=17 Blocked
at android.app.ResourcesManager.getOrCreateResources (ResourcesManager.java:865)
at android.app.ResourcesManager.getResources (ResourcesManager.java:982)
at android.app.ActivityThread.getTopLevelResources (ActivityThread.java:2411)
at android.app.ApplicationPackageManager.getResourcesForApplication (ApplicationPackageManager.java:1527)
at android.app.ApplicationPackageManager.getDrawable (ApplicationPackageManager.java:1277)
at android.app.ApplicationPackageManager.loadUnbadgedItemIcon (ApplicationPackageManager.java:2684)
at android.app.ApplicationPackageManager.loadItemIcon (ApplicationPackageManager.java:2655)
at android.content.pm.PackageItemInfo.loadIcon (PackageItemInfo.java:516)
at android.content.pm.ComponentInfo.loadDefaultIcon (ComponentInfo.java:256)
at android.app.ApplicationPackageManager.loadUnbadgedItemIcon (ApplicationPackageManager.java:2687)
at android.app.ApplicationPackageManager.loadItemIcon (ApplicationPackageManager.java:2655)
at android.content.pm.PackageItemInfo.loadIcon (PackageItemInfo.java:516)
at android.content.pm.LauncherActivityInfo.getIcon (LauncherActivityInfo.java:117)
at org.n277.lynxlauncher.visual.c.d.v (SourceFile:1)
at org.n277.lynxlauncher.visual.c.d.u (SourceFile:3)
...
The main thread tries to get the display size for measurement using the "windowManager.getDefaultDisplay().getSize()" method (for Pre Android R devices). The worker thread tries to get the Icon of an Application using a LauncherActivityInfo.
Example 2:
"main" prio=5 tid=1 Blocked
at android.view.WindowManagerImpl.getCurrentBounds (WindowManagerImpl.java:241)
at android.view.WindowManagerImpl.getCurrentWindowMetrics (WindowManagerImpl.java:235)
at org.n277.lynxlauncher.helper.DeviceUtilities.getUsableHeight (DeviceUtilities.java:190)
at org.n277.lynxlauncher.views.FullScreenFrameLayout.onMeasure (FullScreenFrameLayout.java:114)
at android.view.View.measure (View.java:25532)
at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure (FrameLayout.java:194)
at android.view.View.measure (View.java:25532)
at android.widget.RelativeLayout.measureChildHorizontal (RelativeLayout.java:735)
...
"Lynx-Worker" prio=5 tid=17 Blocked
at android.app.ResourcesManager.createResources (ResourcesManager.java:846)
at android.app.ResourcesManager.getResources (ResourcesManager.java:947)
at android.app.ActivityThread.getTopLevelResources (ActivityThread.java:2296)
at android.app.ApplicationPackageManager.getResourcesForApplication (ApplicationPackageManager.java:1672)
at android.app.ApplicationPackageManager.getDrawable (ApplicationPackageManager.java:1437)
at android.app.ApplicationPackageManager.loadUnbadgedItemIcon (ApplicationPackageManager.java:2950)
at android.app.ApplicationPackageManager.loadUnbadgedItemIcon (ApplicationPackageManager.java:2953)
at android.app.ApplicationPackageManager.loadItemIcon (ApplicationPackageManager.java:2929)
at android.content.pm.PackageItemInfo.loadIcon (PackageItemInfo.java:271)
at android.content.pm.LauncherActivityInfo.getIcon (LauncherActivityInfo.java:119)
at org.n277.lynxlauncher.visual.c.d.v (SourceFile:1)
at org.n277.lynxlauncher.visual.c.d.u (SourceFile:3)
...
Again, the main thread tries to get the display size for measurement, but this time using windowManager.getCurrentWindowMetrics(). The worker thread again tries to get the Icon of an Application using a LauncherActivityInfo.
Example 3:
Exactly the same as example 2, but there is another generic thread stuck with the very same trace as the worker thread.
For me it really looks like there is some sort of deadlock when accessing the "android.app.ResourcesManager" class from multuple threads.
I suspect that also "WindowManagerImpl.getCurrentBounds" is accessing the ResourcesManager although it is not shown explicidly. However, this did not happen before the last update and i have not changed anything regarding the threading. The only thing that comes into my mind is that i updated several dependencies:
buildToolsVersion from 30.0.0 to 30.0.2
com.android.billingclient:billing from 3.0.3 to 4.0.0
androidx.appcompat:appcompat from 1.2.0 to 1.3.0
androidx.recyclerview:recyclerview from 1.1.0 to 1.2.1
What i suspect is thas it is causing problems now when the Resources are accessed from multiple threads simultaneously. Moving the App Icon loading into the main thread however does not seem wise to me as this would clog this thread easily. For the "LauncherActivityInfo.getIcon" method it is clear that it has to access the resources to get the icon of the app (although not the resources of the app the method is called from but from the app from which the icon should be loaded). However many other tasks are also performed in the worker thread and many of the called API methods could also access the resources internally even though it is not obvious.
Has anyone else experiencef problems of this kind? (after an update of one of the above mentioned dependencies).
Or has anyone found a workaround/solution to this specific ANR?

Casting audio with local server socket

I'm trying to fix an existing project which is casting video and audio to the web.
I need to create local socket:
socketId = "my.application.media." + suffix + "-" + new
Random().nextInt();
localServerSocket = new LocalServerSocket(socketId);
receiver = new LocalSocket();
receiver.connect(new LocalSocketAddress(socketId));
receiver.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
receiver.setSendBufferSize(SOCKET_BUFFER_SIZE);
sender = localServerSocket.accept();
sender.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
sender.setSendBufferSize(SOCKET_BUFFER_SIZE);
and creating media recorder:
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setAudioEncodingBitRate((int) 7.95 * 1024);
mMediaRecorder.setAudioSamplingRate(8000);
mMediaRecorder.setAudioChannels(1);
mMediaRecorder.setOutputFile(sender.getFileDescriptor());
mMediaRecorder.prepare();
But I'm getting java.lang.IllegalStateException after calling start on mMediaRecorder. What am I missing? When I'm not using sender.getFileDescriptor() everything is working correctly so probably that's the problem. I know that there are many libraries which are providing this functionality but I prefer to fix this one. Casting the only video is working correctly and the only problem is with the audio. Thanks a lot for help.
Order of executed methods:
added logs to check the order of methods and thread:
creating sockets: Socket opening thread
creating receiver: Socket opening thread
creating sender: Socket opening thread
setting audio source: Socket opening thread
setting properties: Socket opening thread
creating file descriptor: Socket opening thread
preparing media recorder: Socket opening thread
starting media recorder: Socket opening thread
I found that I'm also receiving errors:
2019-02-13 18:15:49.701 6176-13833/? E/StagefrightRecorder: Output file descriptor is invalid
2019-02-13 18:15:49.701 7851-9780/my.application E/MediaRecorder: start failed: -38
As stated here this error java.lang.IllegalStateException occurring when
a method has been invoked at an illegal or inappropriate time.
So with that in mind and with this article in mind of how to use sockets, You should put your socket related staff inside AsyncTask (separated thread) and use try catch.
AsyncTask Documentation , and Socket Documentation if you want to expand your knowledge.
As it seems you are trying to use getFileDescriptor before (or after if it close) sender have the data to pull it out.
Try extracting the data at earlier location in the code to a variable and than use this variable instead.
Another possibility could be; MediaRecorder documentation says
You must specify a file descriptor that represents an actual file
so be sure that the type that sender.getFileDescriptor() return is the right type that mMediaRecorder.setAudioChannels can get.

PJSUA Registration is not proceeding

Hello fellow developers,
I am developing SIP application using PJSIP library...
Here is my code
And here is my Logcat log
The problem is application is not trying to proceed registration, and no errors at all... (Or I missed something?)
And log on my Kamailio Server (when my device tries to connect and log stucks nothing happens):
22(32406) DEBUG: <core> [ip_addr.c:229]: print_ip(): tcpconn_new: new tcp connection: 192.168.0.150
22(32406) DEBUG: <core> [tcp_main.c:985]: tcpconn_new(): on port 17749, type 3
22(32406) DEBUG: <core> [tcp_main.c:1295]: tcpconn_add(): hashes: 879:4076:3775, 50
22(32406) DEBUG: <core> [io_wait.h:376]: io_watch_add(): DBG: io_watch_add(0x9f8540, 44, 2, 0x7f6cdbbf3948), fd_no=34
ps. I tested server with ready client and everything is ok...
Thanks in advance :)
I have resolved issue...
The problem was in registering PJSIP in another thread... I was using main Thread and initializing connection on OnCreate() event in MainActivity. Which means, after OnCreate() fired and finishes its job, initialization process was getting destroyed before it connects into server...
What I did:
Before:
UaConfig ua_cfg = epConfig.getUaConfig();
ua_cfg.setUserAgent("Pjsua2 Android " + ep.libVersion().getFull());
ua_cfg.setThreadCnt(0);
ua_cfg.setMainThreadOnly(true);
After:
UaConfig ua_cfg = epConfig.getUaConfig();
ua_cfg.setUserAgent("Pjsua2 Android " + ep.libVersion().getFull());
Removing those lines makes PJSIP library to create its own worker thread...

Categories

Resources