Android Studio build dex jar

Gradle配置

Build配置文件gradle.build中添加如下task

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)

此方法是直接从把gradle build生成的classes.jar拷贝到指定目录并且重命名,必须依赖build task。

修改dex jar的MANIFEST.MF文件

由于目前gradle的”com.android.application/library”不能和”java” plugin同时使用,所以我们可以使用其他方法来修改,本文中使用python的zipfile来处理。

自定义MANIFEST.MF文件

文件内容如下:

Manifest-Version: 1.0
Gradle-Version: 2.2.1
Created-By: 1.8.0_20-b26 (Oracle Corporation)
Date: 2015-4-15
Author: Leo.Kang
jar-version: 1.0.0

Python脚本

例如:新建一个python脚本,命名为updateJarManifest.py

#!/usr/bin/python

import sys,zipfile,shutil,os

def 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_jar

generate()

执行python的shell脚本(mac/linux)/批处理文件(win)

win

新建一个文件命名updateJar.bat文件内容为python updateJarManifest.py

mac/linux

新建一个文件命名为:updateJar.sh,
文件内容为:

#!/bin/bash
#@author Leo.Kang
# 2015-4-15 20:47

python ./ updateJarManifest.py

更新build.gradle 配置

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

<a href=”http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html”>http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.html</a>

    原文作者:康熙微博私访记
    原文地址: https://www.jianshu.com/p/8594430dc5d1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞