Gradle Configuration
Add the following task to the build configuration file Gradle.build
Task Clearjar (type:delete) { Delete ' Build/outputs/mylib.jar '}task Copyjar (type:copy) {from (' build/ intermediates/bundles/release/') into (' build/outputs/libs/') include (' Classes.jar ') rename (' Classes.jar ', ' Mylib.jar ')}copyjar.dependson (Clearjar, build)
This method is copied directly from the Classes.jar generated by the Gradle build to the specified directory and renamed, and must depend on the build task.
Modify the MANIFEST.MF file for the Dex jar
Since the current Gradle "com.android.application/library" cannot be used in conjunction with the "Java" plugin, we can use other methods to modify it, and this article uses Python's zipfile to handle it.
Customizing the MANIFEST.MF file
The contents of the file are as follows:
Manifest-version:1.0gradle-version:2.2.1created-by:1.8.0_20-b26 (Oracle Corporation) Date:2015-4-15author: leo.kangjar-version:1.0.0
Python script
For example: Create a new Python script named updatejarmanifest.py
#!/usr/bin/pythonimport sys,zipfile,shutil,osdef Generate (): jar_file = "./Mylib_temp.jar" Target_jar = Os.path.dirname (jar_file) + "/mylib.jar" shutil.copy (Jar_file, Target_jar) zipped = ZipFile. ZipFile (Target_jar, ' a ', ZipFile. zip_deflated) content_file = "meta-inf/manifest. MF " mf_file ="./manifest. MF " zipped.write (Mf_file, Content_file) zipped.close () return target_jargenerate ()
Execute Python shell script (mac/linux)/batch file (win) win
Create a new file name Updatejar.bat file contents as Python updatejarmanifest.py
Mac/linux
Create a new file named: updatejar.sh,
The contents of the file are:
#!/bin/bash# @author leo.kang# 2015-4-15 20:47python./updatejarmanifest.py
Update Build.gradle Configuration
Task Clearjar (type:delete) { Delete ' Build/outputs/mylib.jar '} task Copyjar (type:copy) {from (' build/ intermediates/bundles/release/') into (' build/outputs/libs/') include (' Classes.jar ') rename (' Classes.jar ', ' Mylib_temp.jar ')}copyjar.dependson (Clearjar, build) Releasejar.dependson (Clearjar, build) task Execpython (type:exec) { //workingdir file ('./') commandLine './updatejar.sh '}execpython.dependson ( Releasejar)
Reference
Http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html
Android Studio build dex jar