Java Android 库 Gradle 发布 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19307341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Library Gradle release JAR
提问by Marek Sebera
How can I release Jar packaging of android-library
project?
I've found, classes.jar is located under build/bundles/release/classes.jar
and I suppose this is correct Jar package (contains *.class
files).
如何发布android-library
项目的Jar 包?
我发现,classes.jar 位于下面build/bundles/release/classes.jar
,我想这是正确的 Jar 包(包含*.class
文件)。
Is there some official way, to release library as JAR instead of AAR ?
是否有一些官方方法可以将库作为 JAR 而不是 AAR 发布?
Edit
I use Gradle to release Maven artifacts, and I'd like to release JAR along with AAR package. So JAR with signature, md5, manifest, ...
based on https://chris.banes.me/2013/08/27/pushing-aars-to-maven-central/
编辑
我使用 Gradle 发布 Maven 工件,我想与 AAR 包一起发布 JAR。所以 JAR 带有签名、md5、清单……
基于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
}
}
using
使用
task androidJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
}
will package only java files, not compiled and linked against android sdk
将只打包 java 文件,而不是针对 android sdk 编译和链接
采纳答案by Jake Wharton
While I haven't tried uploading the artifacts with a deployment to Sonatype (or even a local repo), here's what I managed to come up with a few weeks agowhen trying to tackle the same problem.
虽然我还没有尝试通过部署将工件上传到 Sonatype(甚至是本地存储库),但这是我几周前在尝试解决相同问题时设法提出的。
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);
}
Then run the following:
然后运行以下命令:
./gradlew jarRelease
回答by Olinasc
Another way to generate a jar from a library project through gradle is as follows:
另一种通过gradle从库项目生成jar的方法如下:
In your library's build.gradle:
在您库的 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)
What we are doing here is just copying the classes.jar generated by the Android Gradle plugin. Be sure to look into your build directory for this file and see if its contents are in the way you want.
我们在这里所做的只是复制 Android Gradle 插件生成的 classes.jar。请务必查看此文件的构建目录,并查看其内容是否符合您的要求。
Then run the makeJar
task and the resulting jar will be in library/build/libs/${jarName}
.jar
然后运行makeJar
任务,生成的 jar 将在 library/build/libs/ ${jarName}
.jar 中
The will have the class files according to your configuration for release. If you are obfuscating it, then the files in the jar will be obfuscated.
将根据您的配置发布类文件。如果您对其进行混淆,那么 jar 中的文件将被混淆。
回答by Kikiwa
Just because the previous answers were not fitting my needs, I share my tricky version to generate a jar with the project classes and the aar/jar dependencies.
仅仅因为之前的答案不符合我的需求,我分享了我的棘手版本来生成一个带有项目类和 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' )
}
This does NOT manage the build types / flavours but can be adapted (it's just ok to build on build-type release without flavour).
这不管理构建类型/风格,但可以进行调整(可以在没有风格的构建类型版本上构建)。
If ever you've a smarter or more elegant solution, please share!
如果您有更聪明或更优雅的解决方案,请分享!