<?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 Daniel Rodak on Medium]]></title>
        <description><![CDATA[Stories by Daniel Rodak on Medium]]></description>
        <link>https://medium.com/@daniel-rodak?source=rss-28c37b824cf4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_NWWlpvQIx4-Ec2orqnL2A.png</url>
            <title>Stories by Daniel Rodak on Medium</title>
            <link>https://medium.com/@daniel-rodak?source=rss-28c37b824cf4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:22:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel-rodak/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[Automating Android Lint Checks: A Guide to Pre-commit Hooks with Ktlint and Detekt]]></title>
            <link>https://daniel-rodak.medium.com/automating-android-lint-checks-a-guide-to-pre-commit-hooks-with-ktlint-and-detekt-d89b98898305?source=rss-28c37b824cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d89b98898305</guid>
            <category><![CDATA[linting]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[detekt]]></category>
            <category><![CDATA[pre-commit]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Daniel Rodak]]></dc:creator>
            <pubDate>Tue, 03 Dec 2024 22:19:17 GMT</pubDate>
            <atom:updated>2024-12-19T09:40:57.130Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>Consistent code quality is essential for successful Android development. Tools like Ktlint and Detekt automate enforcing Kotlin best practices, but integrating them seamlessly into your workflow is key. This article introduces pre-commit hooks as a bridge between code commits and quality checks, ensuring your codebase remains clean with minimal effort.</p><p>Pre-commit hooks are scripts that run automatically before you commit code, helping catch and fix issues early.</p><h3><strong>What You’ll Learn</strong></h3><p>Discover how to effortlessly integrate Ktlint and Detekt lint checks into your Android project using the <a href="https://pre-commit.com/">pre-commit framework</a>. This article focuses on setting up pre-commit hooks to run checks only on staged Kotlin files. You’ll see code snippets that can be directly implemented in your project.</p><p>This guide is perfect for Android developers who want to enforce consistent coding standards with minimal effort.</p><p>Let’s start with adding scripts that will be responsible for running lint checks for us. Those files are commonly placed in the <em>tools/scripts/</em> directory but this can be adjusted up to conventions in your project.</p><p>Note for <strong>Windows/Linux</strong> users: you need to adjust the first line of your script files to <em>#!/usr/bin/env bash</em></p><h3>Detekt script file</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d07e4544d8b16cf06f4226981abac75e/href">https://medium.com/media/d07e4544d8b16cf06f4226981abac75e/href</a></iframe><p><strong>Preparing the List of Files to Analyze</strong></p><pre>inputFiles=(&quot;$@&quot;)<br>formattedInputFiles=$(IFS=,;printf  &quot;%s&quot; &quot;${inputFiles[*]}&quot;)<br>echo &quot;Input files: $formattedInputFiles&quot;</pre><p>The script analyzes files passed as input, which are provided as arguments when the Git hook runs. These files are collected into an array called <em>inputFiles</em>. Only staged <em>.kt</em> files will be passed here thanks to pre-commit so we don’t need extra logic to find the correct files or exclude unstaged files.</p><p><strong>Running the detekt Check</strong></p><pre>run_detekt_check() {<br>    detekt --input &quot;$1&quot; 2&gt;&amp;1<br>}</pre><pre>detektOutput=$(run_detekt_check &quot;$formattedInputFiles&quot;)<br>detektExitCode=$?</pre><p>With the input files ready, the script defines a helper function called <em>run_detekt_check</em>. This function invokes detekt, passing the baseline configuration and the list of input files to be analyzed. The output of detekt is captured into a variable called <em>detektOutput</em>, and the exit code is saved into <em>detektExitCode</em>.</p><p>If you are using detekt baseline in your project you might want to modify the function a little:</p><pre>run_detekt_check() {<br>    detekt --baseline config/detekt-baseline.xml --input &quot;$1&quot; 2&gt;&amp;1<br>}</pre><p><strong>Handling the Analysis Results</strong></p><pre>if [ $detektExitCode -ne 0 ]; then<br>  echo &quot;$detektOutput&quot;<br>  echo &quot;***********************************************&quot;<br>  echo &quot;                 detekt failed                 &quot;<br>  echo &quot; Please fix the above issues before committing &quot;<br>  echo &quot;***********************************************&quot;<br>  exit $detektExitCode<br>fi</pre><p>After running detekt, the script checks the exit code to determine whether the analysis was successful. If the exit code is non-zero (indicating a failure), the script does the following:</p><p>1. Prints the captured detekt output to show the issues found.</p><p>2. Displays a clear error message explaining that detekt has failed and asking you to fix the issues before committing.</p><p>3. Exits with the same non-zero code, effectively blocking the commit.</p><h3><strong>Ktlint script file</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bef0d6ce96b7983c87a1f10a9e1627f3/href">https://medium.com/media/bef0d6ce96b7983c87a1f10a9e1627f3/href</a></iframe><p><strong>Preparing the List of Files to Analyze</strong></p><pre>inputFiles=(&quot;$@&quot;)<br>formattedInputFiles=$(printf &quot;%s\n&quot; &quot;${inputFiles[@]}&quot;)<br>echo &quot;Input files: $formattedInputFiles&quot;</pre><p>Similar to the detekt script, the ktlint script starts by preparing a list of files to analyze. These files are passed as arguments to the script, thanks to the pre-commit hook setup. The script formats these files to be later passed into ktlint run function.</p><p><strong>Running the Ktlint Check</strong></p><pre>run_ktlint_check() {<br>    ./gradlew ktlintCheck -PinternalKtlintGitFilter=&quot;$1&quot;<br>}<br>ktlintOutput=$(run_ktlint_check &quot;$formattedInputFiles&quot;)<br>ktlintExitCode=$?</pre><p>Here, the script defines a helper function, <em>run_ktlint_check</em>, which runs the <em>ktlintCheck</em> Gradle task. It uses the <em>-PinternalKtlintGitFilter</em> parameter to specify which files should be checked. This approach ensures that ktlint runs only on the staged files.</p><p><strong>Handling the Analysis Results</strong></p><pre>if [ $ktlintExitCode -ne 0 ]; then<br>  echo &quot;$ktlintOutput&quot;<br>  echo &quot;***********************************************&quot;<br>  echo &quot;                 ktlint failed                 &quot;<br>  echo &quot; Please fix the above issues before committing &quot;<br>  echo &quot;***********************************************&quot;<br>  exit $ktlintExitCode<br>fi</pre><p>Just like the detekt script, this part deals with the results of the analysis.</p><p>Now let’s use scripts we prepared in the previous section. Please create file called <em>.pre-commit-config.yaml</em> directly in the project root directory.</p><h3>Pre-commit config file</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/434212aa7cb4a5f24b0443f290ec1a28/href">https://medium.com/media/434212aa7cb4a5f24b0443f290ec1a28/href</a></iframe><p><strong>Entry point</strong></p><pre>repos:<br>  - repo: local</pre><p>This means the hooks are local to your project, not fetched from an external source.</p><p><strong>Hooks</strong></p><pre>hooks:<br>  - id: ktlint<br>    name: ktlint check<br>    description: Runs `ktlint` on modified .kt files.<br>    language: script<br>    entry: tools/scripts/ktlint.sh<br>    files: \.kt</pre><p>Each hook has an <em>id</em> for unique identification, a <em>name</em> describing its purpose, and a <em>description</em> providing additional context. The <em>language: script</em> field indicates the hook uses a shell script, while <em>entry</em> points to the script location. Mind that this path might be different if you put script files in other directory. To specify that this hook should only run on files with the .kt extension (Kotlin files) we need to specify <em>files: \.kt:</em>.</p><p>Make sure to commit those files now! When you make changes to the <em>ktlint.sh</em> or <em>detekt.sh</em> scripts, you need to commit those changes for the pre-commit framework to use the updated versions of the scripts.</p><h3>Running hooks</h3><p><strong>Pre-commit installation</strong></p><p>Check if you have Ktlint and Detekt CLI installed, run those commands in the terminal:</p><ul><li>ktlint --version</li><li>detekt --version</li></ul><p>Install required tools, for example, using <a href="https://brew.sh/">Homebrew</a>:</p><ul><li>brew install ktlint</li><li>brew install detekt</li><li>brew install pre-commit</li></ul><p>Using terminal, navigate to the root directory of the repository and run pre-commit install. This step is necessary in order to make precommit work.</p><p>You can verify its success by checking if <em>.git/hooks/pre-commit</em> file was created in your project directory.</p><p><strong>Finish line</strong></p><p>With the pre-commit hooks in place, your codebase is now equipped for automated lint check. It will be triggered both from CLI and Android Studio if you have <em>Run git hooks</em> checkbox selected in the Commit checks section (selected by default).</p><h3><strong>Downsides of Adding Pre-commit Hooks</strong></h3><p>While pre-commit hooks are valuable for automating lint checks, they do come with some trade-offs:</p><ul><li><strong>Performance Impact</strong>: On slower machines, running lint checks for each commit might be time-consuming and interrupt your workflow.</li><li><strong>Missed Commit Notifications</strong>: If a commit is rejected due to lint issues, you might miss the notification and assume your work has been committed. This can lead to further changes being made without realizing the initial commit never went through.</li></ul><h3>Summary</h3><p>Pre-commit hooks offer an elegant solution to automate lint checks in your Android project, streamlining your workflow and improving code quality. In this guide, we walked through setting up custom scripts for ktlint and detekt, integrating them seamlessly into your Git workflow using the pre-commit framework. With minimal effort, you can now catch issues early, save time during reviews, and maintain a cleaner codebase. By integrating these hooks, you can simplify your workflow, enforce coding standards, and focus on writing great code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d89b98898305" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refreshing token together with Android Apollo]]></title>
            <link>https://daniel-rodak.medium.com/refreshing-token-together-with-android-apollo-df91072de7ad?source=rss-28c37b824cf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/df91072de7ad</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Daniel Rodak]]></dc:creator>
            <pubDate>Mon, 18 Jan 2021 20:46:33 GMT</pubDate>
            <atom:updated>2021-01-18T20:46:33.847Z</atom:updated>
            <content:encoded><![CDATA[<h3>Refreshing access token together with Android Apollo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kz77FG4Q5qsjOUMvAS7IeA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@maxcodes?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Maxwell Nelson</a> on <a href="https://unsplash.com/s/photos/code?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>GraphQL every day becomes more popular as the backend API choice so it’s natural that Android Devs needs to deal with it more often. Apollo is commonly used as the GraphQL client on the mobile side that makes it easier to manage the communication but there are always some use cases that we need to handle by ourselves. One of those cases is refreshing access token expiration with the refresh token for GraphQL APIs using Apollo and this is what this article is about. If you are interested only in code and you want to analyze it by yourself, you can go straight to the final code section.</p><p><em>Note: this article was based on the Apollo 2.4.0 version, Apollo API may be changed in future versions.</em></p><h3>Re-authentication flow</h3><ol><li>A request is being rejected with an Unauthorized error message.</li><li>Use a refresh token to gain a new access token.</li><li>Retry sending the request with a new access token.</li></ol><h3>Using Apollo Interceptor</h3><p>First of all, we will need to create and add Apollo Interceptor to our Apollo Client instance. If you are already familiar with REST API token handling, this concept should be already well-known to you. Let’s see the docs:</p><blockquote>“ApolloInterceptor is responsible for observing and modifying the requests going out and the corresponding responses coming back in.”</blockquote><p>Sounds promising for us! Let’s create a class that implements an interceptor interface and override both `<em>interceptAsync</em>` and `<em>dispose</em>` functions. We will also need here our tokens storage and class that will manage our refresh request. In my case, I will pass them by constructor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb7cba48773841e9ea8c6010d18cf285/href">https://medium.com/media/fb7cba48773841e9ea8c6010d18cf285/href</a></iframe><p>Now you can call `<em>addApplicationInterceptor</em>` directly on Apollo Client builder and pass freshly created class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb9c44b1e88d6cf8199244801b6ebd68/href">https://medium.com/media/cb9c44b1e88d6cf8199244801b6ebd68/href</a></iframe><h3>Asynchronous interception</h3><p>Go back inside your interceptor. You are overriding two functions, let’s focus on the `<em>interceptAsync</em>`, we will cover `<em>dispose</em>` later. First of all, we will use the chain to proceed with the ongoing request:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af99d6639288b36b428ece3857c8dcb1/href">https://medium.com/media/af99d6639288b36b428ece3857c8dcb1/href</a></iframe><p>We need to pass three arguments here, `<em>request: ApolloInterceptor.InterceptorRequest</em>`, `<em>dispatcher: Executor</em>`, `<em>callback: ApolloInterceptor.CallBack</em>`.</p><p><strong>ApolloInterceptor.InterceptorRequest</strong></p><p>Basically, we need to add our authorization header to the request that is going out of our app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46c2424f72f8eee2ce37450e5447e0a1/href">https://medium.com/media/46c2424f72f8eee2ce37450e5447e0a1/href</a></iframe><p><strong>Executor</strong></p><p>We simply pass the already received dispatcher here.</p><p><strong>ApolloInterceptor.CallBack</strong></p><p>Finally, we reached our refreshing token handling. In this callback, we will handle the response, here we will check if it was successful and if it wasn’t, we will handle the reauthentication process. Let’s implement ApolloInterceptor.CallBack!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea1e80bbc1be6607312f0b7cd10f878c/href">https://medium.com/media/ea1e80bbc1be6607312f0b7cd10f878c/href</a></iframe><p>All arguments passed to that method are the same as for `<em>interceptAsync</em>`. So for fetching and failure events we are not doing anything special. It’s up to you but in this case we are simply pushing the events upstream and on complete, we won’t do anything with it. Everything will be covered inside the `<em>onResponse</em>` method. To catch only unauthorized response error here we added `<em>isUnauthenticated</em>` and its implementation will be based on your backend response, e.g. you will check if in response errors there’s some tag or code.</p><p>If it’s true, then you need to handle re-authentication. Here you need to use your refresh token to ask your authentication service for the new access token. When you successfully receive a new token, then you proceed with the request again. Otherwise, you need to propagate the response up, so you will handle it by the upper layer, e.g. by logging out the user. Example code with rxJava but this method will be based on your implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fabaeaa5579aad20bc69218e0f2b0666/href">https://medium.com/media/fabaeaa5579aad20bc69218e0f2b0666/href</a></iframe><p>That’s simply it! Important note: be aware of all the callback invocations, don’t miss them!</p><h3>Dispose method</h3><p>Be careful with dealing with the `<em>dispose</em>` method, especially when you will have singleton instance of the ApolloClient cause this will be called even when any error occurs, and if you add any real disposing here, you may not be able to make any other requests in the application lifetime. So I suggest leaving it empty or dispose only temporary objects(e.g. clear your rx disposables). This can be tricky so if you want to post any code here, be sure to deeply debug it, especially in the failure case.</p><h3>Retry counter</h3><p>You can also add a retry counter for the reauthentication to prevent making too many calls. To do that, firstly declare a var int field, globally in the class. I suggest you adding `<em>@Volatile</em>` annotation to it, to make sure that the counter will be refreshed on all threads. You could also define const max retries count in your companion object but it’s up to you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83a2b84e1ad3cbf649b8098ee23baa26/href">https://medium.com/media/83a2b84e1ad3cbf649b8098ee23baa26/href</a></iframe><p>There will be several places where the counter will be called, please check the final code section to see where it appears.</p><h3>The final code</h3><p>Let’s sum up the code that we wrote. You need to adjust it by your Token storage implementation and refresh token call. Be aware of the callback invocations!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/082a23889a91124fc8755bd2370b7551/href">https://medium.com/media/082a23889a91124fc8755bd2370b7551/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df91072de7ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>