<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>LastMinuteDevice</title>
    <description>Code and croissants since 2004. 
</description>
    <link>http://localhost:4000/</link>
    <atom:link href="http://localhost:4000/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 18 Feb 2020 21:56:34 -0800</pubDate>
    <lastBuildDate>Tue, 18 Feb 2020 21:56:34 -0800</lastBuildDate>
    <generator>Jekyll v4.0.0</generator>
    
      <item>
        <title>Evaluate rendering performance on Android</title>
        <description>&lt;h2 id=&quot;see-performance&quot;&gt;See performance&lt;/h2&gt;

&lt;p&gt;Sometimes, especially when working with List/RecyclerViews, it behooves one to work on improving the rendering performance of a given screen in your app. I don't like to optimise blind, and I love automation, so I wanted a way to take a snapshot of the performance of a given area. You can enable a kind of performance overlay and just look at it, but that doesn't give you averages over time and it can't be tied into an automated test, practically speaking.&lt;/p&gt;

&lt;h2 id=&quot;cookie-butter&quot;&gt;cookie-butter&lt;/h2&gt;

&lt;p&gt;My solution was a python script that essentially scrapes dumpsys gfxinfo and produces a chart. (You can get it from GitHub &lt;a href=&quot;https://github.com/Turnsole/cookie-butter&quot;&gt;here&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;To collect data, go to Developer Options &amp;gt; Profile GPU rendering. Have it send data to the dumpsys buffer. (You can alternatively view it on the screen in real time as bars, though you can't choose both.)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/enable_gpu_profiling.png&quot; alt=&quot;Enable GPU profiling&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Restart the app you want to measure. The script requires matplotlib, which is included in many distributions of Python, but if you somehow don't have it already:&lt;/p&gt;

&lt;p&gt;On Linux: 

	
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;	&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;python-matplotlib
	&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;


	or, on OSX: 

	
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;	pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;matplotlib
	&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/p&gt;

&lt;p&gt;Now the fun part. To use the script: 

	
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;python generate_frametime_graphs.py &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;
usage: generate_frametime_graphs.py &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; package &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;seconds] &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;title] &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;device]

Generate frame &lt;span class=&quot;nb&quot;&gt;time &lt;/span&gt;graph &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;a connected ADB device.

positional arguments:
  package     package to target &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;ex: com.google.android&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  seconds     number of seconds to collect data
  title       title to use &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;graph
  device      direct to a specific ADB device

optional arguments:
  &lt;span class=&quot;nt&quot;&gt;-h&lt;/span&gt;, &lt;span class=&quot;nt&quot;&gt;--help&lt;/span&gt;  show this &lt;span class=&quot;nb&quot;&gt;help &lt;/span&gt;message and &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
	&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/p&gt;
&lt;p&gt;For example, here's the Clock app shipped with Lollipop: 
    
	
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;    python generate_frametime_graphs.py com.google.android.deskclock 5 example_graph 078f1fe513d
	&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

    
Output: 

	
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Median: 5.0ms 
Average: 5.0ms 
Device framerate: 60.0 
Estimated dropped frames: 4
	&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/example_graph.png&quot; alt=&quot;Example Graph&quot; /&gt;&lt;/p&gt;

&lt;p&gt; A smooth framerate (not visibly stuttering) is 60 frames per second, or 16 milliseconds to render each frame. You'll want to see the gold-colored curve of that graph stay under 16ms. On emulators, you'll see the gray curve much larger than on a real device, sometimes to the order of ~10ms on Genymotion. That's totally normal, so be sure and check it out on actual hardware before you worry about it.&lt;/p&gt;

</description>
        <pubDate>Fri, 05 Jun 2015 10:30:00 -0700</pubDate>
        <link>http://localhost:4000/android/2015/06/05/graphics-performance-on-android/</link>
        <guid isPermaLink="true">http://localhost:4000/android/2015/06/05/graphics-performance-on-android/</guid>
        
        
        <category>android</category>
        
      </item>
    
      <item>
        <title>Image tinting on Android</title>
        <description>&lt;p&gt;##Basics&lt;/p&gt;

&lt;p&gt;Drawable tinting is a useful development technique on Android, it lets you reuse drawables and create nice-looking overlays on background images. While there are some Lollipop specific features that deserve their own post, this post is focused on something that’s been available since api level 4: Duff-Porter blending.&lt;/p&gt;

&lt;p&gt;Duff-Porter modes are twelve rules[1] that determine the RGBA values of a pixel based on two input pixels. This produces a composite image; we can use it to change the color of a bitmap. For example, here’s a kitty:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/demonstration_cat.png&quot; alt=&quot;Original Cat&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can create an image filter that uses Duff-Porter rules like so:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tintImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PorterDuff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Mode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ImageView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;imageView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;PorterDuffColorFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;porterDuffColorFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PorterDuffColorFilter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;imageView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setColorFilter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;porterDuffColorFilter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There are twelve rules, each with a PorterDuff.Mode, but many of them aren’t distinct when you’re blending a solid color with an image. To tint opaque pixels with another color, use PorterDuff.Mode.SRC_IN.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/demonstration_cat_cyan.png&quot; alt=&quot;Cyan Cat&quot; /&gt;&lt;/p&gt;

&lt;p&gt;##DrawableCompat&lt;/p&gt;

&lt;p&gt;Using the v4 support library, you can take advantage of Lollipop’s tinting for builds where the minimum SDK is earlier. Unfortunately, it does no actual tinting unless the device is running Lollipop or higher.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;ImageView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;icon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;ImageView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// This has no effect on devices running a version &amp;lt; Lollipop. &lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;DrawableCompat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getDrawable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;DrawableCompat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setTintMode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getDrawable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PorterDuff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SRC_IN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;##Themes 
Now let’s take this a step further.&lt;/p&gt;

&lt;p&gt;Imagine that you have some solid colored icons that get used throughout your app, but their color depends on what context they’re in. You can make all your icons white (best for straightforward blending), create a theme for each context in the app, and tint the icons with a theme color.&lt;/p&gt;

&lt;p&gt;attrs.xml:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;resources&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;attr&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;colorIcons&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;format=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;reference|color&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/resources&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Activity theme:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;item&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;colorIcons&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;@color/red&lt;span class=&quot;nt&quot;&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Get the appropriate color from the theme for a given Activity:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;TypedValue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typedValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TypedValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;getTheme&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;resolveAttribute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;colorIcons&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typedValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typedValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;tintImage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PorterDuff&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SRC_IN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;icon&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now each time you tweak your theme, all these icons will be displayed in the appropriate colour, and you haven’t had to touch them or create duplicates for each section of the app.&lt;/p&gt;

&lt;p&gt;That custom attribute you just created is accessible from XML as well. In fact, if the image in question is in a static layout, you can define the tint there rather than adding a filter in Java. (Works at least back to api level 10.)&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;ImageView&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@drawable/ic_action_search&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@+id/icon&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:contentDescription=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;@string/link_search&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_height=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:layout_width=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;android:tint=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;?colorIcons&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;[1] Porter, Thomas; Tom Duff (1984). “Compositing Digital Images”. Computer Graphics 18 (3): 253–259&lt;/p&gt;
</description>
        <pubDate>Thu, 28 May 2015 05:19:30 -0700</pubDate>
        <link>http://localhost:4000/android/2015/05/28/image-tinting/</link>
        <guid isPermaLink="true">http://localhost:4000/android/2015/05/28/image-tinting/</guid>
        
        
        <category>android</category>
        
      </item>
    
      <item>
        <title>Debugging on Android</title>
        <description>&lt;p&gt;If you previously gave up on using an IDE integrated debugger for Android development, have another look. Until about a year ago my opinion was that they’ve usually more frustration than they’re worth. (This opinion is by no means widespread.) The IDE integration in Eclipse never performed consistently enough on any of my development environments to make it more help than hindrance.&lt;/p&gt;

&lt;p&gt;Android Studio, however, does pretty well and so I’ve been using it a bit more lately. Unfortunately, you can’t use it to step through C or C++ JNI code yet (this is coming), but on Java it take barely any effort to set up. This article will run through:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Setting up the debugger in Android Studio&lt;/li&gt;
  &lt;li&gt;Stepping through Java code&lt;/li&gt;
  &lt;li&gt;WebViews: debugging via Chrome and Console&lt;/li&gt;
  &lt;li&gt;Hugo: Annotation-based debug logs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;setting-up-the-debugger-in-android-studio&quot;&gt;Setting up the debugger in Android Studio&lt;/h3&gt;

&lt;p&gt;Start up or plug in some device with debugging enabled in the developer options.&lt;/p&gt;

&lt;p&gt;Picture:&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;img src=&quot;/images/developer_settings_lollipop.png&quot; alt=&quot;Developer Settings on Lollipop&quot; /&gt;&lt;/td&gt;
      &lt;td&gt;&lt;img src=&quot;/images/developer_settings_ics.png&quot; alt=&quot;Developer Settings on ICS&quot; /&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Now in Android Studio, click the bug icon to launch your app and start debugging, or the bug-and-device icon to start debugging an app that is already open. Notice the window that has appeared in your Android Studio workspace.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/debugger_window.png&quot; alt=&quot;Debugging Window&quot; /&gt;&lt;/p&gt;

&lt;p&gt;###Stepping through Java code&lt;/p&gt;

&lt;p&gt;Now set a breakpoint somewhere by clicking next to the line number, or where the line number would be if you were displaying them. A red dot should sit beside any line that has a breakpoint. When execution reaches this line, it will pause. You can then inspect the state of any object in scope. To continue execution, click the little green arrow to the left of the debugging window.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/debugger_breakpoints.png&quot; alt=&quot;Debugging Breakpoints&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Piece of cake, right?&lt;/p&gt;

&lt;h3 id=&quot;webviews-debugging-via-chrome-and-console&quot;&gt;WebViews: debugging via Chrome and Console&lt;/h3&gt;

&lt;p&gt;There are a couple of fairly simple ways to debug Android WebView issues; using Console is easy but only gives you messages, and using Chrome takes some setup but gives you pretty much everything.&lt;/p&gt;

&lt;h4 id=&quot;chrome&quot;&gt;Chrome&lt;/h4&gt;

&lt;p&gt;Google gave us a really nice tool with Chrome Remote Debugging. (See the documentation &lt;a href=&quot;https://developer.chrome.com/devtools/docs/remote-debugging&quot;&gt;here&lt;/a&gt; for full details on how to use this tool.) If you open up an instance of the Chrome desktop browser and navigate to chrome://inspect/#devices, you can see a list of Chrome windows communicating with one of Chrome processes running on the desktop. It’ll show you a preview, let you select elements, show you the Javascript console, and even let you edit things on the fly.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/chrome_debugging_devices.png&quot; alt=&quot;Chrome Device List&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/chrome_debugging_preview.png&quot; alt=&quot;Developer Settings on Lollipop&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is great for Android because some WebViews will do the same. WebViews running in an API environment that are at KitKat (4.4) or higher will do this once it is explicitly enabled. The flag, once set, is set for the entire application.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SDK_INT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;VERSION_CODES&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;KITKAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;WebView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setWebContentsDebuggingEnabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;console&quot;&gt;Console&lt;/h4&gt;

&lt;p&gt;For debugging in the WebView at API levels lower than 4.4, we can use Console. Details are available on the Chrome developer website &lt;a href=&quot;https://developer.chrome.com/devtools/docs/console&quot;&gt;here&lt;/a&gt;. First, Javascript needs to be enabled.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;WebView&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;WebView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;findViewById&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;web_view&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;webView&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getSettings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setJavaScriptEnabled&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then, in Javascript, messages logged via the console object will be sent to LogCat.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;There&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;are&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;two&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;problems&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;computer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;science&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; I/chromium( 3601): [INFO:CONSOLE(1)] &quot;There are two hard problems in computer science.&quot; ... 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It isn’t as convenient as having linebreaks etc like you do with the Android Studio debugger, but it’s better than nothing. There are some third party tools for more functionality, but I find it’s easier to debug web pages in a desktop Chrome browser than on the device. In the unusual circumstance that behavior differs, this is usually enough to help me figure the rest out.&lt;/p&gt;

&lt;h3 id=&quot;hugo-annotation-based-debug-logs&quot;&gt;Hugo: Annotation-based debug logs&lt;/h3&gt;

&lt;p&gt;The incredible Jake Wharton published Hugo (GitHub page &lt;a href=&quot;https://github.com/JakeWharton/hugo&quot;&gt;here&lt;/a&gt;) in 2013. What it provides is a whole slew of log statements that profile the application as it executes. This logging is only enabled in debug builds, so it won’t end up in your published application.&lt;/p&gt;

&lt;p&gt;For example, say I want to know approximately how much time it takes to initialize a third party library. Annotate the method where this is done like so:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@DebugLog&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enableFabric&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isStageBuild&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isStageBuild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TWITTER_KEY_DEBUG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TWITTER_KEY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isStageBuild&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TWITTER_SECRET_DEBUG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TWITTER_SECRET&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TwitterAuthConfig&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;authConfig&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TwitterAuthConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Fabric&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Crashlytics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Twitter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;authConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;… and then LogCat will show when the method begins, as well as the values of any arguments to it and the time it took to execute.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;D/Convoke ( 1973): ⇢ enableFabric()
I/Fabric  ( 1973): Initializing Crashlytics 2.0.0.15
D/Convoke ( 1973): ⇠ enableFabric [60ms]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I can see that Twitter’s Fabric library took 60ms (not accounting for my two String assignments there) to initialize on this device, also that this initialization was executed as a staging build.&lt;/p&gt;

&lt;p&gt;This library isn’t perfect, I’ve run into compilation issues in the past where the injected code would cause a VerifyError. This has since &lt;a href=&quot;https://github.com/JakeWharton/hugo/issues/9&quot;&gt;been resolved&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Between automated logging, breakpoints, and remote Chrome debugging, you can peer pretty deeply into the workings of an Android application without much effort.&lt;/p&gt;

</description>
        <pubDate>Sun, 30 Nov 2014 04:19:30 -0800</pubDate>
        <link>http://localhost:4000/tools/android/2014/11/30/debugging-on-android/</link>
        <guid isPermaLink="true">http://localhost:4000/tools/android/2014/11/30/debugging-on-android/</guid>
        
        
        <category>tools</category>
        
        <category>android</category>
        
      </item>
    
  </channel>
</rss>
