Search code examples
javagradlejarjar

JarJar not renaming references


I'm using Google's jarjar to repackage dependencies in my application's fat jar. On running the repackaged jar, I get NoClassDefFoundError eg:

Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.classic.LoggerContext

I decompiled the repackaged jar and saw that references to dependencies in import statements and in my code were not refactored. Where am I going wrong?

Here's how I run jarjar:

java -jar jarjar-1.4.jar process repackageRules.txt myApp.jar app_repackaged.jar

Rules I've listed in repackageRules.txt:

rule javassist.** company.@0
rule org.eclipse.** company.@0
rule ch.qos.** company.@0
rule com.fasterxml.** company.@0
rule biz.paluch.** company.@0

My build.gradle:

/* Gradle version: 3.0 */
apply plugin: 'java'
apply plugin: 'jacoco'

group='com.company'
version=0.1

repositories {
    jcenter()
}

dependencies {
    compile group: 'org.javassist', name: 'javassist', version: '3.20.0-GA'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
    compile group: 'org.eclipse.jdt', name: 'org.eclipse.jdt.core', version: '3.12.2'
    compile 'biz.paluch.logging:logstash-gelf:1.11.0'
    testCompile 'junit:junit:4.12'
}

// Create jar with manifest as described
jar {
    manifest {
        attributes ('Premain-Class': 'com.company.instrumentation.Agent',
                'Can-Redefine-Classes': true,
                'Can-Retransform-Classes': true,
                'Can-Set-Native-Method-Prefix': true,
                'Main-Class': 'com.company.Main')
    }
}

//create a single Jar with all dependencies
task fatJar(type: Jar) {
    manifest.from jar.manifest
    classifier = 'all'
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    } {
            // to prevent duplicate files in jar
            exclude "META-INF/*"
            exclude '.api_description'
            exclude '.options'
            exclude '*.html'
            exclude 'plugin.properties'
            exclude 'plugin.xml'
            exclude 'about_files/*'
    }
    with jar
}

I tried to check if jarjar is detecting dependencies between classes correctly using java -jar jarjar-1.4.jar find class myApp.jar

And I get errors like this for all classes in my code: Error reading com/company/Main.class: null

What causes this problem?


Solution

  • The error is caused because JarJar uses ASM 4.0 which doesn't support JDK 8 bytecode. This can be solved by building this fork of JarJar which is modified to use ASM 5.0.