最近在研究sdk的开发,遇到了AndroidStudio打jar的问题,,,执行打jar包的操作我们都可以生成一个jar包,但是这个jar包里面的呈现内容 和 eclipse打jar包的呈现内容是不是一样呢,有三种执行操作,但是打的jar包内容不同(eclipse 打jar包非常简单)
//1.此种打包方式 jar包没有META-INF文件 不会存在
task makeJar(type: Copy) {
delete 'build/libs/demo.jar'
from('build/intermediates/bundles/default/')
into('build/libs/')
include('classes.jar')
task makeJar(type: Copy) {
delete 'build/libs/demo.jar'
from('build/intermediates/bundles/default/')
into('build/libs/')
include('classes.jar')
rename('classes.jar', 'smarters.jar')
}
}
makeJar.dependsOn(build)
//2.此种方式打包 会将META-INF文件 放在包名下 即:包 包含 META-INF文件
task clearJar(type: Delete) {
delete 'build/libs/smarter.jar'
delete 'libs/smarter.jar'
}
task clearJar(type: Delete) {
delete 'build/libs/smarter.jar'
delete 'libs/smarter.jar'
}
//打包任务
task makeJar(type: org.gradle.api.tasks.bundling.Jar) {
//指定生成的jar名
baseName 'smarter'
//从哪里打包class文件
from('build/intermediates/classes/debug/com/smarter/mylibrary')
//打包到jar后的目录结构
into('com/smarter/mylibrary/')
//去掉不需要打包的目录和文件
exclude('BuildConfig.class', 'R.class')
//去掉R$开头的文件
exclude { it.name.startsWith('R$'); }
//此段代码将相关信息写到META-INF文件中的MANEFEST.MF文件
manifest {
attributes(
'Implementation-Title': "${project.name}",
//'Implementation-Version': "${version}",
'Implementation-Version': "2.0",
'Built-Date': new Date().getDateTimeString(),
'Built-With':
"gradle-${project.getGradle().getGradleVersion()},groovy-${GroovySystem.getVersion()}",
'Created-By':
'Java ' + System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')')
}
}
makeJar.dependsOn(clearJar, build)
task makeJar(type: org.gradle.api.tasks.bundling.Jar) {
//指定生成的jar名
baseName 'smarter'
//从哪里打包class文件
from('build/intermediates/classes/debug/com/smarter/mylibrary')
//打包到jar后的目录结构
into('com/smarter/mylibrary/')
//去掉不需要打包的目录和文件
exclude('BuildConfig.class', 'R.class')
//去掉R$开头的文件
exclude { it.name.startsWith('R$'); }
//此段代码将相关信息写到META-INF文件中的MANEFEST.MF文件
manifest {
attributes(
'Implementation-Title': "${project.name}",
//'Implementation-Version': "${version}",
'Implementation-Version': "2.0",
'Built-Date': new Date().getDateTimeString(),
'Built-With':
"gradle-${project.getGradle().getGradleVersion()},groovy-${GroovySystem.getVersion()}",
'Created-By':
'Java ' + System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')')
}
}
makeJar.dependsOn(clearJar, build)
强烈建议此种方式打jar包 与eclipse打的 jar包 一模一样
//3.META-INF文件 与 包文件 并列(与eclipse打包之后的 jar包目录方式一致)
task jarMyLib(type: Jar, dependsOn: ['build']) {
archiveName = 'smarter.jar'
from('build/intermediates/classes/release')
destinationDir = file('build/libs')
exclude('android')//去掉android这个文件夹
exclude('com/smarter/mylibrary/BuildConfig.class')
exclude('com/smarter/mylibrary/BuildConfig\$*.class')
exclude('**/R.class')
exclude('**/R\$*.class')
task jarMyLib(type: Jar, dependsOn: ['build']) {
archiveName = 'smarter.jar'
from('build/intermediates/classes/release')
destinationDir = file('build/libs')
exclude('android')//去掉android这个文件夹
exclude('com/smarter/mylibrary/BuildConfig.class')
exclude('com/smarter/mylibrary/BuildConfig\$*.class')
exclude('**/R.class')
exclude('**/R\$*.class')
manifest {
attributes(
'Implementation-Title': "${project.name}",
'Implementation-Version': "${version}",
'Built-Date': new Date().getDateTimeString(),
'Built-With':
"gradle-${project.getGradle().getGradleVersion()},groovy-${GroovySystem.getVersion()}",
'Created-By':
'Java ' + System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')')
}
}
attributes(
'Implementation-Title': "${project.name}",
'Implementation-Version': "${version}",
'Built-Date': new Date().getDateTimeString(),
'Built-With':
"gradle-${project.getGradle().getGradleVersion()},groovy-${GroovySystem.getVersion()}",
'Created-By':
'Java ' + System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')')
}
}