<?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 Emre KOÇ on Medium]]></title>
        <description><![CDATA[Stories by Emre KOÇ on Medium]]></description>
        <link>https://medium.com/@emrekoc?source=rss-f5175eecf92b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HRVaYISdHg0LwMBtizG6ig.jpeg</url>
            <title>Stories by Emre KOÇ on Medium</title>
            <link>https://medium.com/@emrekoc?source=rss-f5175eecf92b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 14:38:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@emrekoc/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[Detekt - How To Make Your Own Custom Rule]]></title>
            <link>https://medium.com/@emrekoc/detekt-for-android-how-to-make-your-own-custom-rule-2861eb60e4ba?source=rss-f5175eecf92b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2861eb60e4ba</guid>
            <category><![CDATA[detekt]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[static-code-analysis]]></category>
            <dc:creator><![CDATA[Emre KOÇ]]></dc:creator>
            <pubDate>Thu, 15 Jun 2023 07:14:22 GMT</pubDate>
            <atom:updated>2023-06-15T11:33:32.961Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oVTGmFqDNe5hsh0gOXRzYw.png" /></figure><h3>Detekt for Android— How To Make Your Own Custom Rule</h3><p>Let’s continue with the <a href="https://github.com/e-koc/detekt"><strong>project</strong></a> we created on <a href="https://medium.com/@emrekoc/detekt-static-code-analyzer-for-android-c4c9bfd42a8c"><strong>previous post</strong></a></p><h3>Step 1: Setup</h3><p>First of all, a different module is required rather than app module to write custom rules for detekt. Create a module named “rules” and modify <a href="https://github.com/e-koc/detekt/blob/main/rules/build.gradle"><strong>rules/build.gradle</strong></a> like below.</p><pre>plugins {<br>    id &quot;kotlin&quot;<br>    id(&quot;io.gitlab.arturbosch.detekt&quot;).version(&quot;1.22.0&quot;)<br>}<br><br>dependencies {<br>    compileOnly &quot;io.gitlab.arturbosch.detekt:detekt-api:1.22.0&quot;<br>}</pre><blockquote>Note: <strong>detek-api</strong> dependency is required for extending detekt’s rule classes.</blockquote><p>As described on the <a href="https://medium.com/@emrekoc/detekt-static-code-analyzer-for-android-c4c9bfd42a8c"><strong>previous post</strong></a>, let’s continue to modify root level <a href="https://github.com/e-koc/detekt/blob/main/build.gradle"><strong>build.gradle</strong></a><strong> </strong>file. At this step, <strong>rules module</strong> will be added as dependency into the <strong>subprojects</strong> tag for the purpose of giving dependency to the all modules so the custom rules will be able to work throughout the project.</p><pre>subprojects {<br>    afterEvaluate {<br>        //Apply detekt plugin for every module <br>        plugins.apply(&quot;io.gitlab.arturbosch.detekt&quot;)<br>        //Add rules module as custom-detekt dependency for every module<br>        dependencies {<br>            detektPlugins(project(&quot;:rules&quot;))<br>        }<br><br>        def detektTaskProvider = tasks.named(&quot;detekt&quot;)<br>        detektTaskProvider.configure {<br>            reports {<br>                html.required.set(true)<br>                sarif.required.set(false)<br>                xml.required.set(false)<br>                txt.required.set(false)<br>                md.required.set(false)<br>                html.outputLocation.set(file(&quot;${project.rootDir.absolutePath}/build/reports/detekt/report.html&quot;))<br>            }<br>        }<br><br>        if (project.name == &quot;rules&quot;) return<br>        tasks.getByName(&quot;preBuild&quot;).finalizedBy(detektTaskProvider)<br><br>    }<br>}</pre><p>After making rules module available throughout the project, create <strong>RuleProvider </strong>class as shown below. RuleProvider is responsible from adding the custom rules into to the detekt library thus they will be triggered automatically.</p><pre>package com.ekoc.detekt.rules.provider<br><br>import io.gitlab.arturbosch.detekt.api.Config<br>import io.gitlab.arturbosch.detekt.api.RuleSet<br>import io.gitlab.arturbosch.detekt.api.RuleSetProvider<br><br>class RuleProvider : RuleSetProvider {<br><br>    override val ruleSetId: String = &quot;extra-rules&quot;<br>    override fun instance(config: Config): RuleSet = RuleSet(ruleSetId, emptyList())<br>}</pre><blockquote><strong>override val ruleSetId: </strong>Defines the group name of the custom rules that will be used later on config.yml and detekt.yml</blockquote><blockquote><strong>override fun instance(config: Config):</strong> Associates ruleSetId with the given rule list (empty for now). Thanks to the <strong>config</strong> parameter, any parameter about the custom rules can be passed from the <strong>detekt.yml. </strong>For example you can exclude classes from the processing custom rules.</blockquote><p>Continue with creating <strong>config.yml </strong>and <strong>RuleSetProider </strong>files as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*JvuCLyKqwxw6TmQAodUoWQ.png" /></figure><p>Add the code below into the <a href="https://github.com/e-koc/detekt/blob/main/rules/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider"><strong>io.gitlab.arturbosch.detekt.api.RuleSetProvider</strong></a><strong> </strong>file<strong>.</strong></p><pre>com.ekoc.detekt.rules.provider.RuleProvider</pre><p>After that, add the code below into the <a href="https://github.com/e-koc/detekt/blob/main/rules/src/main/resources/config/config.yml"><strong>config.yml</strong></a><strong> </strong>file.</p><pre>extra-rules:<br>  active: true<br>  ContextOrder:<br>    active: true</pre><p>By adding the code above, <strong>detekt</strong> understands that there is a rule named <strong>ContextOrder</strong> in the <strong>extra-rules </strong>ruleset and it is active.</p><blockquote><strong>Not:</strong> At this stage, run <strong>gradle sync </strong>and check <a href="https://github.com/e-koc/detekt/blob/main/rules/src/main/java/com/ekoc/detekt/rules/provider/RuleProvider.kt"><strong>RuleProvider</strong></a> class usage<strong>. </strong>It shouldn’t show unused class warning after running <strong>gradle sync.</strong></blockquote><h3>Step 2: Custom Rule</h3><p>On the first step, <strong>ContextOrder </strong>rule was added into the <a href="https://github.com/e-koc/detekt/blob/main/rules/src/main/resources/config/config.yml"><strong>config.yml</strong></a> file. Let’s move on to this rule. The <strong>ContextOrder</strong> rule checks if any function has <a href="https://developer.android.com/reference/android/content/Context"><strong>Context</strong></a> in its parameters, whether it is in the first order.</p><pre>//ERROR. Context is the second parameter<br>fun foo(bar: String, context: Context){<br>    <br>}<br><br>//SUCCESS. Context is the first parameter<br>fun foo2(context: Context, bar: String){<br><br>}</pre><p>According to the ContextOrder rule, <strong>foo </strong>function will throw error because the Context parameter is used in the second row. <strong>foo2 </strong>function won’t throw error because the Context parameter is used in the first row.</p><p>Let’s keep continue by writing that rule.</p><pre>class ContextOrder(config: Config) : Rule(config) {<br><br>    companion object {<br>        const val ISSUE_DESCRIPTION = &quot;This rule reports the method which doesn&#39;t use Context as the first parameter&quot;<br>    }<br><br>    override val issue = Issue(javaClass.simpleName, Severity.Minor, ISSUE_DESCRIPTION, Debt.FIVE_MINS)<br>}</pre><blockquote><strong>override val issue: </strong>Can be think of it as an exception that will be thrown when an error is detected. For more information check the <a href="https://detekt.dev/kdoc/detekt-api/io.gitlab.arturbosch.detekt.api/-issue/index.html"><strong>Issue</strong></a><strong> </strong>page.</blockquote><p>Then add <strong>visitNamedFunction </strong>function into the ContextOrder class. This function will be triggered for every function that you written in your project.</p><pre><br>/**<br>* //Triggers for every function in the project<br>* @param func: A function in your project<br>*/<br>override fun visitNamedFunction(func: KtNamedFunction) {<br>    super.visitNamedFunction(func)<br>}</pre><p>Later on, add the flow that checks whether Context is at the first parameter and complete the class.</p><pre>class ContextOrder(config: Config) : Rule(config) {<br><br>    companion object {<br><br>        const val CONTEXT = &quot;Context&quot;<br>        const val CONTEXT_WITH_QUESTION_MARK = &quot;$CONTEXT?&quot;<br>        const val REPORT_MESSAGE = &quot;Context must be the first parameter&quot;<br>        const val ISSUE_DESCRIPTION = &quot;This rule reports the method which doesn&#39;t use Context as the first parameter&quot;<br>    }<br><br>    override val issue = Issue(javaClass.simpleName, Severity.Minor, ISSUE_DESCRIPTION, Debt.FIVE_MINS)<br><br>    //Triggers for every function<br>    override fun visitNamedFunction(function: KtNamedFunction) {<br>        super.visitNamedFunction(function)<br>        val parameters = function.valueParameters<br>        //If parameter size smaller than 2, no need to check<br>        if (parameters.size &lt; 2) {<br>            return<br>        }<br>        //Get parameter types like [String,Context,Fragment] etc<br>        val parameterTypeList = parameters.map { x -&gt; x.children[0].text }<br>        //If Context is the first parameter, no need to check<br>        if (isContext(parameterTypeList[0])) {<br>            return<br>        }<br>        //If Context is not the first parameter, report issue<br>        else if (isContext(parameterTypeList)) {<br>            report(CodeSmell(issue, Entity.from(function), REPORT_MESSAGE))<br>        }<br>    }<br><br>    private fun isContext(s: String?): Boolean {<br>        return (s == CONTEXT || s == CONTEXT_WITH_QUESTION_MARK)<br>    }<br><br>    private fun isContext(s: List&lt;String&gt;): Boolean {<br>        return (s.contains(CONTEXT) || s.contains(CONTEXT_WITH_QUESTION_MARK))<br>    }<br>}</pre><h3>Step 3: Rule Introducing</h3><p>Add the <strong>ContextOrder </strong>rule into the <a href="https://github.com/e-koc/detekt/blob/main/rules/src/main/java/com/ekoc/detekt/rules/provider/RuleProvider.kt"><strong>RuleProvider</strong></a> class as shown below.</p><pre>class RuleProvider : RuleSetProvider {<br><br>    override val ruleSetId: String = &quot;extra-rules&quot;<br>    override fun instance(config: Config): RuleSet = RuleSet(ruleSetId, listOf(ContextOrder(config)))<br>}</pre><h3>Step 4: Test</h3><blockquote><strong>Not: </strong>When a custom rule has been added, sometimes detekt does not run as expected even you did everything right. After writing your rule and completing everything, it’s a good idea to <strong>invalidate cache &amp; restart in Android Studio</strong> before testing it.</blockquote><p>Create a class like below to trigger <strong>ContextOrder</strong> rule.</p><pre>package com.ekoc.detekt.sample<br><br>import android.content.Context<br><br>class ContextOrderSample {<br><br>    //FAIL<br>    fun foo(bar: String, context: Context){<br><br>    }<br><br>    //SUCCESS<br>    fun foo(context: Context, bar: String){<br><br>    }<br>}</pre><blockquote>After that a report will be created by running <strong>./gradlew detekt</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8j_V_VKl3Q-Fw4aXd2aIQ.png" /></figure><h3>Conclusion</h3><p>Although it may seem difficult to write a custom rule, once you complete the installation, you can quickly write as many rules as you want.</p><p>I tried to write simplest rule as I can to make it understandable because every project has its own requirements. I hope you enjoyed this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2861eb60e4ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detekt — Static Code Analyzer for Android]]></title>
            <link>https://medium.com/@emrekoc/detekt-static-code-analyzer-for-android-c4c9bfd42a8c?source=rss-f5175eecf92b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4c9bfd42a8c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mvvm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[static-code-analysis]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Emre KOÇ]]></dc:creator>
            <pubDate>Mon, 22 May 2023 18:16:45 GMT</pubDate>
            <atom:updated>2023-06-15T08:49:29.650Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oVTGmFqDNe5hsh0gOXRzYw.png" /></figure><p>Welcome, dear readers, to a journey through the world of code.</p><p>As you know code review process takes more time as development team growing. In this article I am going to talk about a tool that makes code review process easier.</p><p>With the tool called <a href="https://github.com/detekt/detekt"><strong>Detekt</strong></a>, you can perform check anything you want in the classes written in <a href="https://kotlinlang.org/"><strong>Kotlin</strong></a>, start the code review process anytime (e.g. before commit) and save time.</p><h3>Pros</h3><h4>Reporting Types</h4><p>Detekt provides legible reports in various types like html, xml, sarif, md, txt</p><h4>Predefined Rules</h4><p>You almost don’t need to write any rule because <strong>Detekt</strong> has more than 250 predefined rules which scans entire project.</p><h4>Custom Rules</h4><p><strong>Detekt</strong> supports custom rule writing in anyway you want when predefined rules doesn’t match your needs.</p><h3>Cons</h3><h4>Limited File Support</h4><p><strong>Detekt</strong> only works for <a href="https://kotlinlang.org/"><strong>Kotlin</strong></a> files so you can’t perform check on Java or xml files.</p><h3>Setup</h3><p><strong>Step 1: </strong>Add code block below to the root level <strong>build.gradle</strong> file.</p><pre>buildscript {<br>    repositories {<br>        mavenCentral()<br>    }<br>}<br><br>plugins {<br>    id(&quot;io.gitlab.arturbosch.detekt&quot;).version(&quot;1.22.0&quot;)<br>}</pre><p><strong>Step 2: </strong>Add code block below to the app level <strong>build.gradle</strong> file. You can add the detekt fraction as shown.</p><pre><br><br>plugins {<br>    id(&quot;io.gitlab.arturbosch.detekt&quot;).version(&quot;1.22.0&quot;)<br>}  <br><br>android {<br>}<br><br>detekt {<br>    toolVersion = &quot;1.22.0&quot;<br>    config = files(&quot;config/detekt/detekt.yml&quot;)<br>    buildUponDefaultConfig = true<br>}</pre><p><strong>Step 3:</strong> Run code below after gradle sync</p><pre>./gradlew detektGenerateConfig</pre><p><strong>Result: </strong>After the successful execution of step 3, <strong>detekt.yml</strong> config file will be generated automatically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*bNLNR38sFOWOihadnE40Vw.png" /></figure><p><strong>Note: buildUponDefaultConfig = true </strong>means that you can delete any option that you will not change in <strong>detekt.yml</strong>. In this case detekt will use predefined values for them.</p><h3><strong>How to Use?</strong></h3><p>Let’s add 2 classes.</p><pre>class EmptyClass {<br>}</pre><pre>import com.google.*<br><br>class WildCardImportClass {<br>    val count = 0<br>}</pre><p>And then let’s run the command below to run Detekt.</p><pre>./gradlew detekt</pre><p>If you get “<strong>Unable to locate a Java Runtime” </strong>error than try command below. <strong><em>-Dorg.gradle.java.home </em></strong><em>tells to the gradle about your java home location</em></p><pre>./gradlew -Dorg.gradle.java.home=YOUR_JAVA_HOME_PATH detekt<br><br>./gradlew -Dorg.gradle.java.home=&quot;/Applications/Android Studio.app/Contents/jbr/Contents/Home&quot; detekt</pre><p>After the successful execution of detekt task, you are supposed to see command line like below.</p><figure><img alt="image of detekt error message" src="https://cdn-images-1.medium.com/max/1024/1*FEIeoCmTDEnkFmp9qnnXDg.png" /></figure><p>You can see <a href="https://detekt.dev/docs/rules/empty-blocks/#emptyclassblock"><strong>EmptyClassBlock</strong></a> and <a href="https://detekt.dev/docs/rules/style/#wildcardimport"><strong>WildcardImport</strong></a><strong> </strong>texts showing up in the square brackets. Those are the predefined Detekt rules that we need to fix it in our project. If we did more mistakes, more violation warnings would’ve show up here.</p><blockquote>Note: You can access all rules through <strong>detekt.yml.</strong></blockquote><h3>Reporting</h3><p><strong>Detekt</strong> provides reports in html, txt, md, sarif, xml types as shown image below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*nZ6IOkdABZfiya2pYitkFQ.png" /></figure><h4><strong>Sample Html Report</strong></h4><figure><img alt="Detek html report sample" src="https://cdn-images-1.medium.com/max/1024/1*h20OJmMmrkLiItUEQzV5IA.png" /></figure><p>If you need only html reports, modify root level <strong>build.gradle</strong> file like below</p><pre>subprojects {<br>    afterEvaluate {<br>        tasks.named(&quot;detekt&quot;).configure {<br>            reports {<br>                html.required.set(true)<br>                sarif.required.set(false)<br>                xml.required.set(false)<br>                txt.required.set(false)<br>                md.required.set(false)<br>                html.outputLocation.set(file(&quot;${project.rootDir.absolutePath}/build/reports/detekt/report.html&quot;))<br>            }<br>        }<br>    }<br>}</pre><h3><strong>Conclusion</strong></h3><p>Detekt has highly configurable options so probably this will be like the first article of Detekt series. I can say Detekt is very helpful even it doesn’t support Java or xml files.</p><p>Normally, modifications on the root level <strong>build.gradle </strong>are not welcome but in this case <strong>Detekt</strong> needs to run on whole project so if you have multi module project, modifying only root level <strong>build.gradle</strong> file is better than editing all app level <strong>build.gradle</strong> files.</p><p>In the next article I’ll show how to run detekt automatically before commit but you can take the bonus below until that time come.</p><h3>Bonus: Running Detekt before every build</h3><p>By applying modifications below in the root level <strong>build.gradle</strong>, you can run Detekt task automatically before every build.</p><pre>subprojects {<br>    afterEvaluate {<br>        def detektTaskProvider = tasks.named(&quot;detekt&quot;)<br>        detektTaskProvider.configure {<br>            reports {<br>                html.required.set(true)<br>                sarif.required.set(false)<br>                xml.required.set(false)<br>                txt.required.set(false)<br>                md.required.set(false)<br>                html.outputLocation.set(file(&quot;${project.rootDir.absolutePath}/build/reports/detekt/report.html&quot;))<br>            }<br>        }<br>        tasks.getByName(&quot;preBuild&quot;).finalizedBy(detektTaskProvider)<br>    }<br>}</pre><blockquote>You can reach at the codes in this article <a href="https://github.com/e-koc/detekt"><strong>here</strong></a></blockquote><p><strong>Part 2: </strong><a href="https://medium.com/@emrekoc/detekt-for-android-how-to-make-your-own-custom-rule-2861eb60e4ba">How to create your own custom rules</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4c9bfd42a8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>