0

I build a jar for the Android platform with Gradle. When I use the dx command, it works well building a jar with manifest, but dx can't support Java 8 syntax. So I have to change compile tool to d8, it supports Java 8 syntax but it removes the MANIFEST.MF file. So how to keep manifest with the d8 compile tool?

Build with dx: enter image description here

Build with d8: enter image description here

Gradle build script codes:

def jarPath = buildDir.absolutePath + "/libs"
def jarBaseName = "plugin"

task buildJar(dependsOn: build, type: Jar) {
    doFirst {
        manifest {
            attributes 'Jar-VersionCode': jarVersionCode
        }
    }
    from zipTree(file('build/intermediates/aar_main_jar/release/classes.jar'))
    // [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
    archiveBaseName = jarBaseName
    archiveAppendix = null
    archiveVersion = "$jarVersionName-$jarVersionCode"
    archiveClassifier = "release"
    archiveExtension = "jar"
    destinationDirectory = file(jarPath)
}

// dx --dex --output=output.jar input.jar
task buildDex1(dependsOn: buildJar, type: Exec) {
    workingDir jarPath
    executable "dx"
    args "--dex"
    args "--output=" + buildJar.archiveFileName.get()
    args buildJar.archiveFileName.get()
}

// d8 --release --output output.jar input.jar
task buildDex2(dependsOn: buildJar, type: Exec) {
    workingDir jarPath
    executable "d8"
    args "--release"
    args "--output"
    args buildJar.archiveFileName.get()
    args buildJar.archiveFileName.get()
}
DysaniazzZ
  • 825
  • 15
  • 29
  • I'm confused, why trying to build a `jar` with `.dex` files in it? `jar`files are as its name describes *Java Application Resources* files, should have a tree structures with packages and `.class` files(and other resources). On the other hand `.aar` (*Android Application Resources* ) are for storing `.dex` files. – Facundo Larrosa Sep 15 '21 at 03:05
  • @FacundoLarrosa The jar is built to hot update for Android App. The App will check to load a new version jar when opens. – DysaniazzZ Sep 15 '21 at 06:14
  • Can't you achieve the same with an `.aar`? – Facundo Larrosa Sep 15 '21 at 06:21
  • @FacundoLarrosa I think `aar` is so fat to use, it has so many useless files. Hot update only needs bytecode and manifiest resource to tell the current version. – DysaniazzZ Sep 15 '21 at 07:08
  • Why not building a good old `.jar` file with the bytecode in `.class` files as it is the `jar` lib standard? – Facundo Larrosa Sep 15 '21 at 11:37
  • @FacundoLarrosa dex jar is smaller than the standard jar in size – DysaniazzZ Sep 30 '21 at 03:12
  • Is that much smaller to make it worthy? how much smaller are we talking about? – Facundo Larrosa Sep 30 '21 at 03:13

0 Answers0