AndroidStudio4.0+ 打Jar包

1.在项目根目录下新建toJar.gralde文件:

def jarName = "lxlibs1.0"

task deleteOldJar(type: Delete) {
    //删除旧包
    delete 'build/libs/' + jarName + ".jar"
}

//Copy类型,请在Terminal中运行gradlew makeJar
task makeJar(type: Copy) {
    //设置拷贝的文件
    from("build/intermediates/aar_main_jar/release")
    //打进jar包后的文件目录,将classes.jar放入build/libs/目录下
    into('build/libs/')
    //要打包的jar文件
    include('classes.jar')
    //重命名
    rename('classes.jar', jarName + ".jar")
}

makeJar.dependsOn(deleteOldJar, build)

// 生成jar包的sourcesJar,不需要修改。
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
    archiveName = jarName + '-sources.jar'
}


//-encoding UTF-8  -charset UTF-8
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.compilerArgs << "-Xlint:unchecked"
}
artifacts {
    archives sourcesJar
//    archives javadocJar
}
1603183370(1).jpg

2.Termminal执行命令:

gradlew makeJar

打包完生成jar包

1603183494(1).jpg

你可能感兴趣的:(AndroidStudio4.0+ 打Jar包)