0

I have a utilities.aar in a intranet maven repo. It uses a few well known jars such as gson. I have defined them as compile dependencies in utilities module's build.gradle as follows

dependencies {
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'commons-codec:commons-codec:1.4'
    compile 'org.apache.commons:commons-lang3:3.4'
}

It has been uploaded Artifactory repo on our intranet server using uploadArchives gradle task. The pom file shown on Artifactory is as follows

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>adsf.asdf.utils</groupId>
  <artifactId>utilities</artifactId>
  <version>2.0.0.0</version>
  <packaging>aar</packaging>
  <dependencies>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.3.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>support-v4</artifactId>
      <version>23.1.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

I am building an android app using Android Studio v1.5 build tools 23.0.2 and gradle v2.8. This ADT bundle was downloaded on Jan 2016. My app depends on utilities.aar. It is added to build.gradle of the app using the snippet from the webpage as follows

compile(group: 'asdf.asdf.utils', name: 'utilities', version: '2.0.0.0', ext: 'aar')

Android Studio doesn't show any errors. It compiles into an apk fine but when I run it, the app immediately crashes with java.lang.NoClassDefFoundError: com.google.gson.GsonBuilder.

Shouldn't gradle automatically download gson and compile it into the apk as per the pom in the Artifactory repo? I am new to maven, android studio, gradle etc. I am used to developing in Eclipse ADT. I would appreciate any help.

adithyag
  • 35
  • 7
  • 1
    Can you add transitive : true option in your dependency and try again – Fuat Coşkun Feb 25 '16 at 05:37
  • Using the @aar notation if you want to download the dependencies, you should add transitive=true. – Gabriele Mariotti Feb 25 '16 at 06:40
  • @GabrieleMariotti `ext: 'aar'` is the same as `@aar`. – RaGe Feb 25 '16 at 10:52
  • @RaGe Exactly. If you use the notation you should also add the transitive=true. Check http://stackoverflow.com/questions/31731014/what-does-transitive-true-in-gradle-exactly-do-w-r-t-crashlytics/31753839#31753839 – Gabriele Mariotti Feb 25 '16 at 10:54
  • @GabrieleMariotti Looks like removing the ext: aar part from the dependency fixed it. It is now downloading dependencies. I started having issues with META-INF/*. So added the following packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/dependencies' exclude 'META-INF/notice' exclude 'META-INF/license' exclude 'META-INF/license.txt' exclude 'META-INF/notice.txt' } – adithyag Feb 26 '16 at 17:17
  • @adithyag The reason is in the answer linked in my previous comment. – Gabriele Mariotti Feb 26 '16 at 17:22

1 Answers1

0

Buddy most of these libraries includes gson are available via remote repository. Please try approach 3 of this post:

https://stackoverflow.com/a/35369267/5475941

For example, gson search result:

gson search result

I hope it helps.

Community
  • 1
  • 1
Mohammad
  • 6,024
  • 3
  • 22
  • 30