<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anders Ullnæss on Medium]]></title>
        <description><![CDATA[Stories by Anders Ullnæss on Medium]]></description>
        <link>https://medium.com/@Zredna?source=rss-36f5dcbbde71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*z8S3EQtnfzW6B37Sq9BRSQ.jpeg</url>
            <title>Stories by Anders Ullnæss on Medium</title>
            <link>https://medium.com/@Zredna?source=rss-36f5dcbbde71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 06:01:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Zredna/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Oh Snap!]]></title>
            <link>https://proandroiddev.com/oh-snap-966bd786b7a4?source=rss-36f5dcbbde71------2</link>
            <guid isPermaLink="false">https://medium.com/p/966bd786b7a4</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[screenshot-testing]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Anders Ullnæss]]></dc:creator>
            <pubDate>Sun, 30 Jan 2022 16:12:35 GMT</pubDate>
            <atom:updated>2022-08-13T14:47:15.906Z</atom:updated>
            <content:encoded><![CDATA[<h3>Oh Snap! Snapshot Testing with Jetpack Compose</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lRhT2o3yP4cRWewWroqFAQ.png" /><figcaption><em>#beachbod2022</em></figcaption></figure><p><strong>Edit:<br></strong>I wrote some more about snapshot testing and improving on our setup here:<br><a href="https://dev.to/andersullnass/improving-snapshot-tests-with-paparazzi-1llp">Improving Compose snapshot tests with Paparazzi</a></p><h3>Snapwhat?</h3><p>Snapshot testing (or screenshot testing, I will be using both terms interchangeably) is a type of testing which helps us ensure our UI does not change unintentionally.</p><p>A snapshot test consists of 2 steps:</p><ol><li>Record</li><li>Verify</li></ol><h4>Record</h4><p>When we are done building a piece of UI, whether it is a whole screen or just a button, we write our snapshot test and record. A screenshot is saved in our source code and it defines the baseline for our UI.</p><h4>Verify</h4><p>Most likely we will touch some code at a later point which might or might not change the UI from our original screenshot.<br>When verifying, the snapshot test takes a new screenshot and compares the two images. If they are not identical, our snapshot test fails.</p><p><strong><em>“But what if I meant to change the UI? Won’t that break my snapshot test?”</em></strong><em><br></em>Yes, it will. When intentionally changing the UI we just re-record.<br>As a nice bonus our pull request will show the before and after look, which makes our reviewing buddies happy!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P8sVgzta44WsPN4c26PicQ.png" /><figcaption><em>Who would have thought that designers could change their mind?</em></figcaption></figure><h3>Let’s give it a (snap)shot</h3><p>In my experience snapshot testing has been quite popular among iOS developers and rarely spoken about by Android developers. I haven’t worked on any project before where it was done for Android. One reason could be that for iOS the tooling is fantastic and it is easy to get started. (My colleagues use <a href="https://github.com/pointfreeco/swift-snapshot-testing">https://github.com/pointfreeco/swift-snapshot-testing</a>). For Android, not so much, though I think things are improving.</p><p>Either way we got inspired by our iOS colleagues and wanted to try it out for ourselves. We were also starting to dabble with Jetpack Compose and thought it would be nice to start snapshot testing all our new Compose UI components.</p><p>The first step was finding the right library. Out of the few libraries we found only one of them mentioned Compose, so that was an easy choice:<br><a href="https://github.com/pedrovgs/Shot">https://github.com/pedrovgs/Shot</a></p><p>For the basics of how to write a screenshot test for Jetpack Compose check out the blog post from the author of the library here: <br><a href="https://blog.karumi.com/jetpack-compose-screenshot-testing-with-shot/">https://blog.karumi.com/jetpack-compose-screenshot-testing-with-shot/</a></p><p>Since this blog post already covers the basics I will focus on additional steps we’ve taken:</p><ol><li>Running the snapshot tests as part of CI/CD</li><li>Dealing with screenshots recorded on different emulator architectures</li><li>Making the tests fast and easy to write</li></ol><h3>Continuous Snapping</h3><p>In our project we use Bitrise for CI/CD, but our approach should work for other setups as well. With Shot, you run the screenshot tests through a gradle task ./gradlew internalDebugExecuteScreenshotTests where internalDebug is your build variant.</p><p>If you, like us, have some non-screenshot UI tests already, you need to make things a bit more specific. We do this by using a naming convention, naming all our screenshot tests *ScreenshotTest and running the gradle command like this:</p><pre>./gradlew internalDebugExecuteScreenshots -Pandroid.testIntrumentationRunnerArguments.tests_regex=.*ScreenshotTest.*</pre><p>When you run locally, this command will run on whichever emulator or device you have running. When running as part of your CI/CD, you need to create and run the emulator first. In Bitrise you have a nice AVD manager step to do this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*0MxHrPXNaw2ykEe84TIScw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*lmyPnQlWKeo84UIluZnN7A.png" /></figure><p>When we record screenshots locally it is important to use the same type of emulator, so we also have a script for creating our emulator, appropriately named <em>snappy</em>. (This could also be used in other CI/CDs without the AVD Manager step):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d187478ca4c22ce1558110f04dadf6a0/href">https://medium.com/media/d187478ca4c22ce1558110f04dadf6a0/href</a></iframe><p>This script has grown a bit over time to support my colleague getting a new Mac with Apple silicon (more on that later), but the important part happens on the last line. It creates an emulator with the same specs as the one from Bitrise.</p><p>After the AVD Manager step in Bitrise you have to wait for the emulator to be ready before running your screenshot tests. It took me a while to figure out why it was not working, because the error message you get is not very clear.<br>Of course there is a step for this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*mxSdRsvIfT20FpVPwIZDNg.png" /><figcaption>Patience is a virtue</figcaption></figure><p>Locally we run this script for running (verifying) the screenshot tests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b7678b2971e79e964a3212414a78b6b/href">https://medium.com/media/4b7678b2971e79e964a3212414a78b6b/href</a></iframe><p>The script for recording screenshots is very similar but with the record parameter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f61bf27be70509f46c6f2c8ea584e21/href">https://medium.com/media/4f61bf27be70509f46c6f2c8ea584e21/href</a></iframe><p>They both use this script for running the emulator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a4755864aa5e6b6679c1e22c4e31ec3/href">https://medium.com/media/4a4755864aa5e6b6679c1e22c4e31ec3/href</a></iframe><p>I have included a line to kill the emulator when the tests are done. Without this you would get a new emulator running every time you run the script (and the tests would run on all of them simultaneously).</p><p>We do not currently pay for the fastest machines in Bitrise, so unfortunately the pipeline is too slow (25–30 minutes) to run it for every pull request. Running the snapshots themselves is not too bad, but creating and starting the emulator is quite slow. When running locally, if you have created and run the emulator before it just takes 1–2 minutes to run our ~50 tests and this number is not going up a lot when adding new tests.</p><p>Instead of every pull request, we run the pipeline nightly and have made it post to a Teams channel when it fails. Not ideal, but good enough:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5sxAV02LXghMMb9VLLcg8w.png" /><figcaption>Maybe I should be fixing our snapshot tests instead of writing blog posts?</figcaption></figure><h3>Not all snaps are created equal</h3><p>When comparing screenshots taken on emulators with different architectures there could be differences that are not visible to the human eye, but will still fail our snapshot test. My colleague’s new Apple silicon Mac cannot run an x86_64 emulator like my older Mac or Bitrise’s Ubuntu Docker image can.</p><p>Our sub-optimal solution to this is to add some tolerance to our snapshot tests. We let the tests succeed if the difference between the two screenshots are less than 2 %.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2bfeea651f3452f1578a7f17c4b4315/href">https://medium.com/media/d2bfeea651f3452f1578a7f17c4b4315/href</a></iframe><p>This might lead to small unintended changes in the UI going undetected by the tests. This can be a real problem and we have already had a few cases where the screenshot tests did not catch a change in the UI:</p><ol><li>Changing one icon on a whole screen</li><li>Slightly changing a margin</li></ol><h3>With the snap of a finger</h3><p>Writing snapshot tests can get tedious and feel repetitive, so we want to make them easy to write and not feel like a chore.</p><p>With Jetpack Compose we regularly create previews of our components showing different configurations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*MAQuMOqBQ6GgLKWrPSFpzw.png" /></figure><p>When creating snapshot tests we kind of want the same thing.<br>So we started creating snapshot tests of our previews:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6664616d1abf7e506997a171d221fbfb/href">https://medium.com/media/6664616d1abf7e506997a171d221fbfb/href</a></iframe><p>With the philosophy of making a screenshot test of every preview, we can see that the screenshot tests will all look very similar.</p><p>For now we have created a live template in Android Studio to help write them faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t8WFqz817Zv__JgYY18Wxg.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/675f4d18f316ee212432ded2b5e24fa3/href">https://medium.com/media/675f4d18f316ee212432ded2b5e24fa3/href</a></iframe><p>Just write snap, hit tab and write the name of your preview. This is faster than writing them manually every time, but a further improvement could be making a script that generates a test for every preview annotation.</p><h3>Conclusion</h3><p>Snapshot testing is still a bit more cumbersome to set up for Android than iOS, and the library we use does not have great integration with Android Studio. You can’t easily run/record a single screenshot test with the play button, you have to use the gradle task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*frtTPFd4bkR6EWKuH-5YFQ.png" /><figcaption>The Shot verification html report shows you when there are changes in your UI</figcaption></figure><p><strong>But for me these benefits still make it worth it:</strong></p><ol><li>We can be certain that our UI does not change unintentionally</li><li>Our pull requests get nice previews of the new UI we made and the UI changes we did “for free”</li><li>We can stop writing the type of Espresso tests that check if all the UI elements are shown on screen</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K0Z_mBH3R8stwCA1oGRKLw.png" /><figcaption>Not gonna miss these</figcaption></figure><p>Please leave a comment or reach out if you want to have a “snapchat”. If you have any questions, suggestions for improvements in our own setup or need any help setting this up for your own project. Either here or on Twitter: <a href="https://twitter.com/andersullnass">https://twitter.com/andersullnass</a>.</p><p><em>Thanks to Alex from the Dutch Android User Group slack (</em><a href="https://twitter.com/alex_caskey">https://twitter.com/alex_caskey</a>) <em>for a lot of help when I was setting this up originally and thanks to my colleague Cristan for working with me refining the way we do snapshot testing and for reviewing this blog post.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=966bd786b7a4" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/oh-snap-966bd786b7a4">Oh Snap!</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analysing your code with ktlint in Bitrise]]></title>
            <link>https://medium.com/@Zredna/analysing-your-code-with-ktlint-in-bitrise-d92109e516f9?source=rss-36f5dcbbde71------2</link>
            <guid isPermaLink="false">https://medium.com/p/d92109e516f9</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ktlint]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[bitrise]]></category>
            <dc:creator><![CDATA[Anders Ullnæss]]></dc:creator>
            <pubDate>Wed, 30 Sep 2020 18:51:13 GMT</pubDate>
            <atom:updated>2020-09-30T18:51:13.071Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*GQDYYJirk_9QsfFhQf4Ukg.png" /></figure><p>In my current project we use <a href="https://www.bitrise.io/">Bitrise</a> as our CI/CD and we use <a href="https://github.com/pinterest/ktlint">ktlint</a> as our static code analysis tool.</p><p>Our goal was for our Bitrise workflow to run the ktlint check and to be able to see the results in a readable format.</p><p>While figuring out how to do this, I stumbled upon a great <a href="https://blog.bitrise.io/automating-code-review-tasks-for-multi-module-android-projects">blog post</a> by <a href="https://twitter.com/hitherejoe">Joe Birch</a>, so be sure to check that out! His use case was way more advanced than what we are trying to do, but it helped me get started.</p><h3><strong>Step 1: Add ktlint to your app</strong></h3><p>If you haven’t already, first add ktlint to your app in your project level build.gradle:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db823269b7d9e3042824dc7bb5a857ef/href">https://medium.com/media/db823269b7d9e3042824dc7bb5a857ef/href</a></iframe><p>We choose the html reporter to get a visual representation of the issues detected by ktlint.</p><p>You can test that it’s working by running something like: <br>./gradlew clean app:ktlintDebugCheck</p><p>After running this you will find your report in:<br>app/build/reports/ktlint/ktlintMainSourceCheck.html</p><h3><strong>Step 2: Create a bitrise Script step</strong></h3><p>Now we want to do exactly the same, just in Bitrise. In your Bitrise workflow create a new Script step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*foAAnsAyxEAxS-avmScOJQ.png" /></figure><p>The script has to do <strong>2 things:</strong></p><ol><li>Run ktlint</li><li>Make the report available to view</li></ol><p><strong>The first thing</strong> we already know how to do:<br>./gradlew clean app:ktlintDebugCheck</p><p>For the <strong>second thing</strong> we need to move the report somewhere it is easy to find and then “deploy” it, so that it will be available as a build artifact.</p><p>Bitrise has an environment variable for its “deploy directory”, $BITRISE_DEPLOY_DIR and a “Deploy to Bitrise.io” step which by default makes all files in this folder available as artifacts when it is done.</p><p>So let’s copy the file to this folder:<br>cp /bitrise/src/app/build/reports/ktlint/ktlintMainSourceSetCheck.html $BITRISE_DEPLOY_DIR/ktlint-report-html</p><p>And let’s add a “Deploy to Bitrise.io” step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ukMyoUYBmf6pjomKtuqAbg.png" /></figure><p>We don’t need to configure this step. It does what we want by default:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*RsL3FSO1f1f8rwO6Z73L-Q.png" /></figure><p>So our script step will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98d11d255d2ee0ec087e9b4ffd4b327d/href">https://medium.com/media/98d11d255d2ee0ec087e9b4ffd4b327d/href</a></iframe><p>When everything works and ktlint is happy you will see a happy report in your build artifacts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VW64nxRviXd9EZD0mX7Apg.png" /></figure><p>And when ktlint detects something it will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W7ce6X3rvpQlRHLjL3iTcQ.png" /></figure><p><strong>One more thing:</strong></p><p>This works, but the script step succeeds and the build is green even if the ktlint check fails. We want the build to fail when the ktlint task fails, so we have to change our script a bit.</p><p>What actually happens is that the exit value of the script is 0 (no errors) because the last command in the script (the copying of the report) always succeeds.</p><p>We solve this by saving the exit code of the ktlint command and checking that at the end of the script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aae688dafa2ab9d1bb5c45955e1b8a1/href">https://medium.com/media/6aae688dafa2ab9d1bb5c45955e1b8a1/href</a></iframe><h3><strong>Bonus step: Pre-commit hook:</strong></h3><p>Some things, like the unused import in the example above, would be nice to catch and fix even before committing our code. ktlint can fix some issues for us automatically and this can be done in a pre-commit hook.</p><p>The one we use looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c7693b2c5b066bc4088a2b794c81329/href">https://medium.com/media/8c7693b2c5b066bc4088a2b794c81329/href</a></iframe><p>Place it in .git/hooks and it will run before every commit, autocorrect the issues it can fix and warn you about issues it cannot fix.</p><p>Hope this was helpful and please let me know if you see anything I can improve!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d92109e516f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Avoiding memory leaks when using Data Binding and View Binding]]></title>
            <link>https://proandroiddev.com/avoiding-memory-leaks-when-using-data-binding-and-view-binding-3b91d571c150?source=rss-36f5dcbbde71------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b91d571c150</guid>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[view-binding]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Anders Ullnæss]]></dc:creator>
            <pubDate>Sat, 22 Aug 2020 17:10:17 GMT</pubDate>
            <atom:updated>2021-03-28T12:35:29.768Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IGh2TgjcZcN4nurZLV4tjQ.jpeg" /></figure><p>In our current project we are using a lot of <a href="https://developer.android.com/topic/libraries/data-binding">Data Binding</a> and recently we have started using <a href="https://developer.android.com/topic/libraries/view-binding">View Binding</a> as well.</p><p>A little while back, we were inspired by <a href="https://twitter.com/Piwai">Pierre-Yves Ricau</a> and <a href="https://twitter.com/EmilyKager">Emily Kager</a>’s awesome <a href="https://www.youtube.com/watch?v=kHHOhPPRytc">video</a> covering how to find and fix memory leaks with <a href="https://square.github.io/leakcanary">LeakCanary</a>, so we added it to our project. We soon saw falling birds all over the place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p21I-cIuh-kIYmSy" /><figcaption>This bird couldn’t handle our Data Binding memory leaks :(</figcaption></figure><h3>Memory leaks everywhere</h3><p>When using Data Binding we used to add the binding as a lateinit var to our fragment like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c5d99acfc6f53b0e849a4f7b8dcab3e/href">https://medium.com/media/6c5d99acfc6f53b0e849a4f7b8dcab3e/href</a></iframe><p>This leads to a memory leak:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BRYTtxYEyroQg1UNmHhjig.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c9oE6PGd7do2t-vLUc9CoQ.jpeg" /></figure><p>LeakCanary is telling us our binding is keeping a reference to our view (mRoot) after the Fragment’s onDestroyView() was called and this might lead to a memory leak.</p><p>When it comes to View Binding, <a href="https://developer.android.com/topic/libraries/view-binding#fragments">Google’s own docs</a> are actually teaching us how to do it the proper way to avoid getting memory leaks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1509377f3526a0ec7cbba6fee4bd8143/href">https://medium.com/media/1509377f3526a0ec7cbba6fee4bd8143/href</a></iframe><p>This technique uses an optional backing field and a non-optionalval which is only valid between onCreateView and onDestroyView.<br>In onCreateView the optional backing field is set and in onDestroyView it is cleared. This fixes the memory leak!</p><p>This way of doing it is also covered in the <a href="https://youtu.be/kHHOhPPRytc?t=1269">video I mentioned earlier at this timestamp</a>.</p><p>You might feel a bit uneasy about the double bangs !!, but all we are saying here is that we are sure this field will not be null when we use it (in the lifecycle between onCreateView and onDestroyView), which is exactly the same we are saying when we use lateinit var.</p><h3>Cool, but now I have to add x lines to every fragment?</h3><p>Our app has a lot of fragments and it would be quite tedious to apply this fix everywhere. A while back I found <a href="https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719">this blog post by Kirill Rozov</a> where he talks about a small view binding delegate library he made which saves you quite some hassle/lines.</p><p>Read his blog post for the details, but using this we can now write our binding like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e2dcb6c8d0dde226b218895dfb044dd/href">https://medium.com/media/4e2dcb6c8d0dde226b218895dfb044dd/href</a></iframe><p>His view binding property delegate takes care of the setting and clearing of the binding at the proper time in the lifecycle, so that we don’t have to do that ourselves in every fragment.</p><p>Using another trick I learned from his examples, we can actually get rid of the whole onCreateView method. It turns out Fragment has a constructor that takes a reference to a layout and then inflates that layout for you in onCreateView (has this always been there?!):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb230ea2275d2bbaeaa2427f064a24f1/href">https://medium.com/media/cb230ea2275d2bbaeaa2427f064a24f1/href</a></iframe><h3>This is great for view binding, but how does this help me with data binding?</h3><p>Maybe this is common knowledge, but I had an aha moment when I found out that a data binding <strong>is </strong>a view binding:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd6a37055d401275b6872a6876e7c0b2/href">https://medium.com/media/dd6a37055d401275b6872a6876e7c0b2/href</a></iframe><p>This means that everything we did in this post can be applied no matter if we are using view binding or data binding!</p><p>Please let me know if this was helpful or if you have any questions or see any mistakes or things that could be done even better.</p><h3>Alternative for data binding</h3><p>If you are using data binding the <a href="https://developer.android.com/reference/android/databinding/ViewDataBinding#unbind()">unbind()</a> function is an alternative (and maybe nicer) way of doing this. Thanks to <a href="https://medium.com/u/151d53b49750">Mostafa Anter</a> for sharing this <a href="https://medium.com/@mostafaanter/just-call-binding-unbind-inside-ondestroy-method-378c93d4e1bd">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b91d571c150" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/avoiding-memory-leaks-when-using-data-binding-and-view-binding-3b91d571c150">Avoiding memory leaks when using Data Binding and View Binding</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Browsing Your Android App’s Database]]></title>
            <link>https://medium.com/@Zredna/browsing-your-android-apps-database-3c67aa3f4a3c?source=rss-36f5dcbbde71------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c67aa3f4a3c</guid>
            <category><![CDATA[realm]]></category>
            <category><![CDATA[room]]></category>
            <category><![CDATA[write-ahead-logging]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[sqlite]]></category>
            <dc:creator><![CDATA[Anders Ullnæss]]></dc:creator>
            <pubDate>Sun, 10 Jun 2018 14:18:55 GMT</pubDate>
            <atom:updated>2018-06-10T14:18:55.758Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zaiXAVv4sbKIVfu-pKSm2Q.jpeg" /><figcaption>Not much data in this room</figcaption></figure><p>As Android developers we often work with SQLite or libraries built on top of it such as <a href="https://realm.io/">Realm</a> or <a href="https://developer.android.com/topic/libraries/architecture/room">Room</a>. When doing so, I often find myself wanting to check what is actually stored in the database. Looking at the actual data in the database is also a good way to help debugging when our app is behaving strangely.</p><p>The database lives within our app’s folder on our device and is by default only accessible by our app. It is usually a file with a .db extension and its path looks something like this:</p><p>/data/data/com.example.app/app.db</p><p>To look at the data stored in our database we must first get hold of this file. Then we need to open it in a program capable of browsing its content.</p><h4>Extracting the Database File</h4><p>To get hold of the database file we need to be running a debug version of the app or have root access to the device.</p><p>Below API 24, we can extract the database file using adb pull. <br>On API 24 and above adb pull /data/data/com.example.app/app.db leads to Permission denied. This is due to the Permissions Changes in Android 7.0 mentioned <a href="https://developer.android.com/about/versions/nougat/android-7.0-changes">here</a>.</p><p>Instead we can use adb exec-out and run the command as our app:<br>adb exec-out run-as com.example.app cat databases/app.db &gt; app.db <br>When running the command as our app, we have access to the app’s folder and we can simply copy the contents of the database to our computer’s file system.</p><h4>Browsing the Data</h4><p>Once we have extracted the database file, we can look at the data within by using a SQLite browser. Personally I like the aptly named <a href="https://sqlitebrowser.org/">DB Browser for SQLite</a>.</p><h4>Write-Ahead Logging</h4><p>In most cases the above is enough, but recently my script “stopped working”.<br>I could see that my app was still working normally since the data stored was shown on the next launch, but when extracting the database like I was used to, I just got an empty file of exactly 4096 bytes, which is the size of one page.<br>When opening this file I saw that it had no tables or data to look at.</p><p>After some digging I found out that this was due to <a href="https://www.sqlite.org/wal.html">write-ahead logging</a>. I guess that Room has started using this in one of their newer versions.<br>The link explains it thoroughly, but in short write-ahead logging or WAL writes changes to a separate file app.db-wal before applying them to the database file at certain checkpoints. In my case the database file was empty because there had been no checkpoints yet. So the changes were still in the WAL file.</p><p>The fix is to extract the WAL file together with the database file and force a checkpoint to occur.</p><p>Our final script looks something like this:</p><pre># extractDatabase.sh</pre><pre># Extract database file, WAL file and shared memory file<br>adb exec-out run-as com.example.app cat databases/app.db &gt; app.db<br>adb exec-out run-as com.example.app cat databases/app.db-wal &gt; app.db-wal<br>adb exec-out run-as com.example.app cat databases/app.db-shm &gt; app.db-shm</pre><pre># Commit changes from WAL file to database file<br>sqlite3 app.db &quot;PRAGMA wal_checkpoint&quot;</pre><pre># Delete files that are not needed anymore<br>rm app.db-wal<br>rm app.db-shm</pre><pre># Open the database in a browser<br>open -a &quot;DB Browser For SQLite&quot; app.db</pre><p>Hope this is helpful to some of you. Feel free to ask questions or share some knowledge in the comments if I can improve something.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c67aa3f4a3c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>