<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rob R</title>
    <description>The latest articles on DEV Community by Rob R (@hszlangbaum).</description>
    <link>https://dev.to/hszlangbaum</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F600464%2F6fe9ab8f-b3a7-4643-a621-b53cbb03fe59.jpg</url>
      <title>DEV Community: Rob R</title>
      <link>https://dev.to/hszlangbaum</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hszlangbaum"/>
    <language>en</language>
    <item>
      <title>Quick guide to safelisting repository artifacts in Gradle </title>
      <dc:creator>Rob R</dc:creator>
      <pubDate>Sat, 20 Mar 2021 13:50:10 +0000</pubDate>
      <link>https://dev.to/hszlangbaum/quick-guide-to-safelisting-repository-artifacts-in-gradle-1bfa</link>
      <guid>https://dev.to/hszlangbaum/quick-guide-to-safelisting-repository-artifacts-in-gradle-1bfa</guid>
      <description>&lt;h3&gt;
  
  
  The background
&lt;/h3&gt;

&lt;p&gt;The typical &lt;code&gt;build.gradle&lt;/code&gt; files list repositories in a way similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;repositories&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;google&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="nf"&gt;mavenCentral&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a quick way to specify that dependencies we're using should be grabbed from one of the repositories above.&lt;/p&gt;

&lt;p&gt;This approach has a few potential issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security, e.g: if a malicious actor placed a dependency impersonating the one we need, the script could download it and we wouldn't know. &lt;/li&gt;
&lt;li&gt;Slowness: the script doesn't know the exact coordinates of each dependency and would be searching in each repository starting from the top one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A better way
&lt;/h3&gt;

&lt;p&gt;A few years ago, Gradle added a functionality that allows to specify where each of the dependencies is precisely located.&lt;br&gt;
These APIs include: &lt;code&gt;includeModule&lt;/code&gt;, &lt;code&gt;includeGroup&lt;/code&gt;, &lt;code&gt;includeGroupByRegex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The good news is that we can gradually migrate to these APIs and make the dependency resolution more secure and faster straight away.&lt;/p&gt;
&lt;h3&gt;
  
  
  1st approach
&lt;/h3&gt;

&lt;p&gt;The easiest way is to replace each repository with a set of &lt;code&gt;includeGroupByRegex&lt;/code&gt; entries.&lt;/p&gt;

&lt;p&gt;Let's imagine that our project is using Retrofit and OkHttp.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Remove &lt;code&gt;mavenCentral()&lt;/code&gt; from the repositories section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Force refresh dependencies in one of the following ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sync the project,&lt;/li&gt;
&lt;li&gt;execute &lt;code&gt;./gradlew --refresh-dependencies&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;run the app/tests,&lt;/li&gt;
&lt;li&gt;remove &lt;code&gt;.m2&lt;/code&gt; &amp;amp; &lt;code&gt;.gradle/caches&lt;/code&gt; folders from the machine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Android Studio, we will see errors similar to these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Could not find com.squareup.okhttp3:okhttp:3.12.1.
     Required by:
         project :app
   &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Could not find com.squareup.okhttp3:okhttp-urlconnection:.
     Required by:
         project :app
   &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Could not find com.squareup.retrofit2:retrofit:2.9.0.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Notice that all these missing files have the same group id: &lt;code&gt;com.squareup&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; In place of &lt;code&gt;mavenCentral()&lt;/code&gt;, add the below and refresh dependenices again (see step 2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;   &lt;span class="nf"&gt;mavenCentral&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nf"&gt;includeGroupByRegex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.*"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Continue the same way till every dependency has been safe-listed. Do the same for &lt;code&gt;google()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We could stop at this stage. The dependency resolution is safer and faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Going a bit further with &lt;code&gt;includeGroup&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;includeGroup&lt;/code&gt; is a more precise version of &lt;code&gt;includeGroupByRegex&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's remove &lt;code&gt;includeGroupByRegex("com.squareup.*")&lt;/code&gt; and inspect the errors again. We can see the group names. Let's use them and replace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;includeGroupByRegex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.*"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="nf"&gt;includeGroup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.retrofit2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="nf"&gt;includeGroup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.okhttp3"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repeat that for every &lt;code&gt;includeGroupByRegex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The dependency resolution is yet safer and faster. We could stop here. &lt;/p&gt;

&lt;h3&gt;
  
  
  An ultimate version - using: &lt;code&gt;includeModule&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now, start removing each &lt;code&gt;includeGroup&lt;/code&gt; one by one and inspect the errors again. The groups and modules names shown in the error console are all we need to use &lt;code&gt;includeModule()&lt;/code&gt;. Let's use these and replace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;includeGroup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.retrofit2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;includeModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.retrofit2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"retrofit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;includeModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.retrofit2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"converter-gson"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;includeModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.squareup.retrofit2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"retrofit-mock"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repeat that for every &lt;code&gt;includeGroup&lt;/code&gt;.&lt;br&gt;
The resulting build script files will be quite large but we'll have full control over the repositories and what gets included in the app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We can migrate at our own pace. We can either jump right into using &lt;code&gt;includeModule&lt;/code&gt; or gradually transition from general repositories to &lt;code&gt;includeGroupByRegex&lt;/code&gt; to &lt;code&gt;includeGroup&lt;/code&gt; and then finally &lt;code&gt;includeModule&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also use a mix of the &lt;code&gt;include&lt;/code&gt;s above. &lt;/p&gt;

&lt;p&gt;As long as we replace general repositories with a set of &lt;code&gt;include&lt;/code&gt;s - our dependency resolution will be faster and safer. And we will know exactly what goes into our apps. &lt;/p&gt;

&lt;p&gt;Further reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.gradle.org/5.1.1/userguide/declaring_repositories.html"&gt;https://docs.gradle.org/5.1.1/userguide/declaring_repositories.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://commonsware.com/blog/2021/02/20/using-repository-safelist-gradle.html"&gt;https://commonsware.com/blog/2021/02/20/using-repository-safelist-gradle.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jebware.com/blog/?p=573"&gt;https://jebware.com/blog/?p=573&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gradle</category>
      <category>android</category>
      <category>jcenter</category>
      <category>maven</category>
    </item>
  </channel>
</rss>
