Tez
Tez

Reputation: 773

Android Studio: Jar files included within Java module aren't found at runtime

I have a "java module" which includes some jar files (lib.jar). They are included within my module build.gradle file using the fileTree method.

compile fileTree(dir: 'libs', include: ['*.jar'])

When I include the "java module" in the main app as a dependency, I always get ClassDefNotFoundException when the module tries to access classes within the lib.jar.

Following is the project hierarchy:

|--mylibrary (java module)
   |
   --libs/
     |
     --lib.jar
   --build.gradle
|--app
   |
   --src/
   --libs/
   --build.gradle

This issue only happens when the module is a "java module". I've tried with an "android library module" as the dependency and the jars are included fine. What am I missing?

Java module build.gradle:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

App build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.macolighting.mncp.myapplication"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':mylibrary')
}

Upvotes: 1

Views: 577

Answers (1)

Tez
Tez

Reputation: 773

In case any was wondering how I got around this, I had to manually extract the jar dependency into the build jar. It's quite abit hacky but it works. Submitted a bug to google and hopefully they'll figure something out. https://code.google.com/p/android/issues/detail?id=186012

apply plugin: 'java'

configurations {
    libDependency
}

jar {
    from {
        configurations.libDependency.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

dependencies {
    FileTree libs = fileTree(dir: 'libs', include: '*.jar')
    libDependency libs
    compile libs
}

Upvotes: 2

Related Questions