title: How to get android gradle project to prefer library dependency AAR over a JAR?
body: <p>I have an android library project (call it my-lib) that produces both an AAR and a JAR using this trick in the <code>build.gradle</code> file:</p>
  
  <pre><code>android.libraryVariants.all { variant -&gt;
    def name = variant.buildType.name
    def task = project.tasks.create "jar${name.capitalize()}", Jar
    task.dependsOn variant.javaCompile
    task.from variant.javaCompile.destinationDir
    artifacts.add('archives', task);
  }
  </code></pre>
  
  <p>This library project contains no Android resources and a JAR is produced for convenient use by some other systems that do not use maven or gradle. Android apps which declare they depend on this android library in their gradle file like so in their gradle file:</p>
  
  <pre><code>dependencies {
    compile 'com.mycompany:my-lib:VERSION'
  }
  </code></pre>
  
  <p>But these android apps are picking up the JAR file instead of the AAR and thus missing out on the <code>proguard.txt</code> file in the AAR which was placed there using a declaration like so in the library gradle file:</p>
  
  <pre><code>android {
    buildTypes.all {
      consumerProguardFiles 'proguard-rules.pro'
    }
  }
  </code></pre>
  
  <p>The pom file produced by the android gradle plugin is missing the <code>packaging</code> entry, it just looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
      xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
    &lt;artifactId&gt;my-lib&lt;/artifactId&gt;
    &lt;version&gt;VERSION&lt;/version&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>The <code>packaging</code> entry should specify <code>aar</code> so that other projects get the AAR instead of the JAR, but how do I do that? Maybe there is a way to produce a JAR with a different name so it doesn't conflict with the AAR?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I can't specify <code>@aar</code> in all my dependencies because some of the dependencies are libraries which specify the project using <code>implementation project(':my-lib')</code> and gradle doesn't accept <code>@aar</code> there. Then when I try to add the dependency to the app I get this error: <code>D8: Program type already present</code> with the name of a class that is in my-lib. I made sure that all my dependencies are referencing my-lib via <code>implementation</code> and not <code>compile</code> and I see that in the intermediate POM files for dependent libraries the reference looks like this:</p>
  
  <pre><code>&lt;dependency&gt;
   &lt;groupId&gt;mycompany&lt;/groupId&gt;
    &lt;artifactId&gt;my-lib&lt;/artifactId&gt;
    &lt;version&gt;1&lt;/version&gt;
    &lt;scope&gt;runtime&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>So the runtime scope seems to be correct.</p>
  
  <p>I'm using the <a href="https://github.com/dcendents/android-maven-gradle-plugin/" rel="nofollow noreferrer">https://github.com/dcendents/android-maven-gradle-plugin/</a> plugin to publish the library to my local maven cache so many other Android apps in different git repos can access the android library. This appears at the top of my library build.gradle file:</p>
  
  <pre><code>apply plugin: 'com.android.library'
  apply plugin: 'android-maven'
  
  group = 'com.mycompany'
  version = '1'
  
  repositories {
      mavenLocal()
  }
  </code></pre>