How to create single jar with FatJar (Gradle)

11,472

Solution 1

Well, I found solution:

task createJar(type: Jar) {
    from {
        List<File> allFiles = new ArrayList<>();
        configurations.compile.collect {
            for (File f : zipTree(it).getFiles()) {
                if (f.getName().equals("classes.jar")) {
                    allFiles.addAll(zipTree(f).getAt("asFileTrees").get(0).getDir())
                }
            }
        }
        allFiles.add(new File('build/intermediates/classes/release'))
        allFiles // To return the result inside a lambda
    }
    archiveName('MySDK.jar')
}

This code generate a single jar with all classes.

Solution 2

Very simple solution is to use the shadow plugin.
Using the plugin is very straightforward:

  1. Declare the plugin as the first statement in your build.grade:

plugins {
id "com.github.johnrengelman.shadow" version "1.2.3"
}

  1. Apply either java or groovy plugin.

apply plugin: 'java'

or

apply plugin: 'groovy'

  1. Refresh the project and run the newly 'shadowJar' task.

This plugin also enable you to exclude dependencies, redirect (rename) packages' names and much more.

Share:
11,472
Patrick
Author by

Patrick

I’m an iOS/Android software engineer who thrives on creating apps with optimal user experiences. With a background in human-computer interaction, I specialize in a esthetically pleasing interfaces that are intuitive, seamless, and functional.

Updated on June 05, 2022

Comments

  • Patrick
    Patrick almost 2 years

    I want to generate a single Jar with multiple modules and dependencies. I installed FatJar Plugin but I got the below error:

    enter image description here

    My Code:

    enter image description here

    Trying shadowJar

    Error:(61, 0) Could not find method shadowJar() for arguments [build_eqgfg4x39smehqcteaccdy4k6$_run_closure4@780b32c6] on project ':SDKFramework' of type org.gradle.api.Project. Open File

    My build.gradle (module)

    apply plugin: 'com.github.johnrengelman.shadow'
    apply plugin: 'com.android.library'
    apply plugin: 'groovyx.grooid.groovy-android'
    
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"
    
        defaultConfig {
            minSdkVersion 9
            targetSdkVersion 19
            versionCode 1
            versionName "0.1.1"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions {
            abortOnError false
        }
    }
    
    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.1.0'
            classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.10'
            classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
        }
    }
    
    dependencies {
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile project(':framework2')
        compile 'com.google.code.gson:gson:2.6.1'
        compile 'junit:junit:4.12'
    }
    
    shadowJar {
    
    }
    
  • Patrick
    Patrick almost 8 years
    I tried. The 'java' plugin has been applied, but is not compatible with de android plugins
  • Ivan
    Ivan almost 8 years
    just add clear android plugin: apply plugin: 'com.android.library'
  • Patrick
    Patrick almost 8 years
    apply plugin: 'android' is deprecated and you cannot combine apply plugin: 'com.android.library' with apply plugin: 'java'
  • Ivan
    Ivan almost 8 years
    Of cause, use just apply plugin: 'com.android.library'. Okay, look at stackoverflow.com/questions/19307341/…
  • Patrick
    Patrick almost 8 years
    If I use: shadowJar { mergeServiceFiles() } Give me next error: the proyect may be using a version of the Android Gradle Plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0)
  • yonisha
    yonisha almost 8 years
    what is mergeServiceFiles function?
  • Patrick
    Patrick almost 8 years
    It´s a shadow function for test. Gradle don´t recognize shadowJar methods but plugin was installed correctly
  • yonisha
    yonisha almost 8 years
    Do you have java plugin or groovy plugin enabled? it is required. updated my answer
  • Patrick
    Patrick almost 8 years
    Updated my question too