Ahsan Zaheer
Ahsan Zaheer

Reputation: 676

Creating Android LIbrary Project Jar Using gradle with dependencies

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)

Upvotes: 6

Views: 3679

Answers (2)

ddmytrenko
ddmytrenko

Reputation: 824

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.

Upvotes: 3

Gilberto Ibarra
Gilberto Ibarra

Reputation: 2927

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')



}

Upvotes: 1

Related Questions