JarJar没有重命名引用

时间:2017-04-09 08:20:28

标签: java gradle jarjar

我正在使用谷歌的jarjar来重新打包我应用程序中的依赖项。在运行重新打包的jar时,我得到NoClassDefFoundError例如:

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

我反编译了重新打包的jar,发现在import语句和我的代码中对依赖项的引用没有被重构。我哪里错了?

这是我如何运行jarjar:

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

我在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

我的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
}

我尝试使用java -jar jarjar-1.4.jar find class myApp.jar

检查jarjar是否正确检测了类之间的依赖关系

我的代码中的所有类都出现了这样的错误:Error reading com/company/Main.class: null

导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:1)

导致错误是因为JarJar使用的ASM 4.0不支持JDK 8字节码。这可以通过构建此fork of JarJar来解决,该this被修改为使用ASM 5.0。