6

I'm trying to build .Jar file out of Android Library Project (Non-executable) using gradle with dependencies, but I'm getting NoClassDefFoundError because it is accessing one of the files from the dependency modules.

So far i've tried FatJar method but it includes everything in the Jar file except the Dependant libraries.

What should i do?

UPDATE

My Gradle.build file

apply plugin: 'android'

    android {
        compileSdkVersion 22
        buildToolsVersion "21.1.2"

        defaultConfig {
            applicationId "com.myapplication"
            minSdkVersion 9
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        sourceSets {
            main {
                java {
                    srcDir 'src/main/java'
                }
                resources {
                    srcDir 'src/../lib'
                }

            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.0.0'
        compile 'com.google.code.gson:gson:2.2.4'
    }

    task deleteOldJar(type: Delete) {
        delete 'build/libs/AndroidPlugin.jar'
    }
    task exportJar(type: org.gradle.api.tasks.bundling.Jar) {
        //from('build/intermediates/bundles/release/')
        //from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
       // archiveName = "yourjar.jar"

        from {

            configurations.runtime.collect {
                it.isDirectory() ? it : zipTree(it)
            }

            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }
        into('release/')
        include('classes.jar')
        ///Give whatever name you want to give
        rename('classes.jar', 'AndroidPlugin.jar')
    }

    exportJar.dependsOn(deleteOldJar, build)
Ahsan Zaheer
  • 646
  • 1
  • 12
  • 18
  • DId you got any solution? I am also building a jar library file. Please check my post [here](http://stackoverflow.com/questions/32373117/error-while-creating-own-jar-library-for-android-project) – Kunu Sep 05 '15 at 13:38

2 Answers2

3

There is a nice plugin which can produce Fat Jars easy: https://github.com/musketyr/gradle-fatjar-plugin . But it works with java plugin which conflicts with com.android.library btw.

So, I have found a little workaround. Every Jar file is a Zip archive actually. It means that we can unzip it and add its content to a new archive.

There is a snippet of code which produces Fat Jar and pushes it to the defined local/remote Maven repository:

...

apply plugin: 'maven-publisher'

publishing {
    publications {
        maven(MavenPublication) {
            artifact bundleRelease
        }

        mavenJava(MavenPublication) {
            artifact fatJar
        }
    }
}

task fatJar(type: Jar) {
    from (zipTree('libs/library-one.jar'))
    from (zipTree('libs/library-two.jar'))
    from (zipTree('libs/library-three.jar'))
    from ('build/intermediates/classes/release/') {
        exclude '**/BuildConfig.class'
        exclude '**/R$*.class'
        exclude '**/R.class'
    }
}

...

Than you need just run:

$ ./gradlew clean build publishToMavenLocal

P.S. Also you can iterate through your libs (including *.jar) folder if you do not want to write every lib separately.

ddmytrenko
  • 796
  • 7
  • 16
1

In your Project , creates folder inside APP named 'libs' , and copy the jar of playservices. This is for Android Studio.

APP
 -libs
 -src
 -build

In your build gradle:

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


    compile files('libs/NameOfJar.jar')



}
Gilberto Ibarra
  • 2,849
  • 2
  • 27
  • 38
  • I don't know if you understood the question correctly. I want to : Export a jar of my Android Library Project, with maven dependancies packeged inside because i'm trying to use this Jar in another project, but i'm getting NoClassDefFoundError. I'm pasting my current gradle file above please check. – Ahsan Zaheer Apr 02 '15 at 09:25