如何发布android-library
项目的Jar包装?
我发现,classes.jar位于build/bundles/release/classes.jar
下,我想这是正确的Jar包(包含*.class
个文件)。
是否有某种官方方式,将库作为JAR而不是AAR发布?
修改
我使用Gradle来发布Maven工件,我想发布JAR和AAR包。所以带有签名的JAR,md5,manifest,...
基于https://chris.banes.me/2013/08/27/pushing-aars-to-maven-central/
apply plugin: 'maven'
apply plugin: 'signing'
configurations {
archives {
extendsFrom configurations.default
}
}
def sonatypeRepositoryUrl
if (isReleaseBuild()) {
println 'RELEASE BUILD'
sonatypeRepositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
println 'DEBUG BUILD'
sonatypeRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
}
if(!hasProperty('nexusPassword')) {
ext.set('nexusPassword', System.console().readPassword("\n\$ Type in password for Sonatype nexus account " + nexusUsername + ": "))
}
if(!signing.hasProperty('password')) {
ext.set('signing.password', System.console().readPassword("\n\$ Type in GPG key password: "))
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
pom.artifactId = POM_ARTIFACT_ID
repository(url: sonatypeRepositoryUrl) {
authentication(userName: nexusUsername, password: nexusPassword)
}
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
developers {
developer {
id "loopj"
name "James Smith"
}
developer {
id "smarek"
name "Marek Sebera"
}
}
}
}
}
}
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
}
task androidJavadocsJar(type: Jar) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
}
使用
task androidJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
}
将只封装java文件,而不是针对android sdk编译和链接
答案 0 :(得分:93)
虽然我没有尝试将部署的工件上传到Sonatype(甚至是本地存储库),但在尝试解决同样的问题时,我设法提出a few weeks ago。
android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
artifacts.add('archives', task);
}
然后运行以下命令:
./gradlew jarRelease
答案 1 :(得分:2)
通过gradle从库项目生成jar的另一种方法如下:
在你的图书馆的build.gradle中:
def jarName = 'someJarName.jar'
task clearJar(type: Delete) {
delete "${project.buildDir}/libs/" + jarName
}
task makeJar(type: Copy) {
from("${project.buildDir}/intermediates/bundles/release/")
into("${project.buildDir}/libs/")
include('classes.jar')
rename('classes.jar', jarName)
}
makeJar.dependsOn(clearJar, build)
我们在这里做的只是复制Android Gradle插件生成的classes.jar。请务必查看此文件的构建目录,并查看其内容是否符合您的要求。
然后运行makeJar
任务,生成的jar将在library / build / libs / ${jarName}
。jar
将根据您的发布配置包含类文件。如果你对它进行模糊处理,那么jar中的文件将被混淆。
答案 2 :(得分:0)
仅仅因为之前的答案不符合我的需求,我分享了我的棘手版本,以生成一个包含项目类和aar / jar依赖项的jar。
// A tricky jar operation, we want to generate a jar files that contains only the required classes used.
// Dependencies are in jar and aar, so we need to open the aar to extract the classes and put all at the same level
// (aar is a zip that contains classes.jar, the latter is a zip that contains .class files)
task jar(type: Jar) {
from {
List<File> allFiles = new ArrayList<>();
configurations.compile.collect {
for (File f : zipTree(it).getFiles()) {
if (f.getName().equals("classes.jar")) {
allFiles.addAll(zipTree(f).getAt("asFileTrees").get(0).getDir())
}
}
}
allFiles.add(new File('build/intermediates/classes/release'))
allFiles // To return the result inside a lambda
}
archiveName( project.ext.appName + '.jar' )
}
这不管理构建类型/风格,但可以进行调整(它可以构建在没有风味的构建类型版本上)。
如果您拥有更智能或更优雅的解决方案,请分享!