使用AAR和源代码JAR将Android库发布到Maven

yacmzcpb  于 2022-12-16  发布在  Android
关注(0)|答案(7)|浏览(152)

有人能给予我一点提示,告诉我如何使用maven-publish Gradle插件发布包含AAR和source jar的com.android.library项目/模块吗?我可以使用旧的maven插件来完成此操作,但我希望使用新的maven-publish插件。

zxlwwiss

zxlwwiss1#

当前答案

使用Android Gradle Plugin 7.1,现在可以非常简单地执行此操作,无需任何复杂的脚本。AGP现在还可以处理源代码和javadocs jar的创建。
您不需要任何单独的脚本,只需将所有内容写入模块的build.gradle文件即可:

plugins {
    ...
    id 'maven-publish'
}
android {
    ...
    publishing {
        singleVariant("release") {
            // if you don't want sources/javadoc, remove these lines
            withSourcesJar()
            withJavadocJar()
        }
    }
}
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId 'com.example'
                artifactId 'mylibrary'
                version = android.defaultConfig.versionName // or manually '1.0'
            }
        }
    }
}

另请参阅:https://developer.android.google.cn/studio/build/maven-publish-plugin
∮旧答案∮

2020年3月3日更新:

自Android Studio 3.6发布以来,Android Gradle插件3.6.0(及更新版本)中实现了对构建AAR(甚至APK和AAB)的支持。
我们不再需要自己处理XML依赖项和其他东西。
下面是我更新的要点Android Studio 3.6.0:https://gist.github.com/Robyer/a6578e60127418b380ca133a1291f017
依据代码:

apply plugin: 'maven-publish'

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompileProvider.get().classpath
        }
    }
    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    archiveClassifier.set('javadoc')
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}

// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // Adds javadocs and sources as separate jars.
                artifact androidJavadocsJar
                artifact androidSourcesJar

                // You can customize attributes of the publication here or in module's build.gradle file (if you save this as script and include it build.gradle file, then you can just replicate this whole block there only with changed fields).
                //groupId = 'com.example'
                //artifactId = 'custom-artifact'
                version = android.defaultConfig.versionName // or just '1.0'
            }
        }
    }
}

旧答案:

下面是我根据其他答案改进的解决方案。

要点:
与其他答案相比的变化:

  • 已更改classifier-它必须是"sources"不是"source"
  • 处理相关性
  • 也支持@aartransitive: false。在这种情况下,我们在POM中设置exclusion以忽略此依赖项的所有传递依赖项。
  • 还支持自定义排除依赖项规则,例如:
compile('com.example:something:1.0', {
      exclude group: 'com.exclude.this', module: 'some-module'
  })
  • 不需要手动指定工件路径。
    更改日志:
    *2018年3月27日-在新Gradle中添加了对 API / * 实施 * 依赖项的支持
    *2018年11月23日-将bundleRelease重命名为bundleReleaseAar,因为它在新的Gradle中发生了更改(参见此答案)
    *23.11.2018-将getAllDependencies更改为getDependencies,以修复重复的结果条目(如我的要点评论中所述)。
    *2019年4月23日- Package 在project.afterEvaluate{...}中,以针对新Gradle进行修复。
apply plugin: 'maven-publish'

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompile.classpath
        }
    }
    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
    classifier = 'javadoc'
    from androidJavadocs.destinationDir
}

task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

project.afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                //groupId 'cz.example'
                //artifactId 'custom-artifact'
                //version = android.defaultConfig.versionName
    
                artifact bundleReleaseAar
                artifact androidJavadocsJar
                artifact androidSourcesJar
    
                pom.withXml {
                    final dependenciesNode = asNode().appendNode('dependencies')
    
                    ext.addDependency = { Dependency dep, String scope ->
                        if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                            return // ignore invalid dependencies
    
                        final dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', dep.group)
                        dependencyNode.appendNode('artifactId', dep.name)
                        dependencyNode.appendNode('version', dep.version)
                        dependencyNode.appendNode('scope', scope)
    
                        if (!dep.transitive) {
                            // If this dependency is transitive, we should force exclude all its dependencies them from the POM
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            exclusionNode.appendNode('groupId', '*')
                            exclusionNode.appendNode('artifactId', '*')
                        } else if (!dep.properties.excludeRules.empty) {
                            // Otherwise add specified exclude rules
                            final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                            dep.properties.excludeRules.each { ExcludeRule rule ->
                                exclusionNode.appendNode('groupId', rule.group ?: '*')
                                exclusionNode.appendNode('artifactId', rule.module ?: '*')
                            }
                        }
                    }
    
                    // List all "compile" dependencies (for old Gradle)
                    configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
                    // List all "api" dependencies (for new Gradle) as "compile" dependencies
                    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                    // List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
                    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                }
            }
        }
    }
}
tf7tbtn2

tf7tbtn22#

下面是一个使用新的maven-publish插件的示例。

apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "sources"
}

publishing {
    publications {
        bar(MavenPublication) {
            groupId 'com.foo'
            artifactId 'bar'
            version '0.1'
            artifact(sourceJar)
            artifact("$buildDir/outputs/aar/bar-release.aar")
        }
    }
    repositories {
        maven {
            url "$buildDir/repo"
        }
    }
}

使用./gradlew clean build publish发布

pbwdgjma

pbwdgjma3#

对dskinner进行一点调整,以正确生成依赖项:

apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "source"
}

publishing {
    publications {
        bar(MavenPublication) {
            groupId 'com.foo'
            artifactId 'bar'
            version '0.1'
            artifact(sourceJar)
            artifact("$buildDir/outputs/aar/bar-release.aar")
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
                configurations.compile.allDependencies.each {
                    if(it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null)
                    {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
    repositories {
        maven {
            url "$buildDir/repo"
        }
    }
}

您可以通过定义以下内容来更改versiongroupId

version = '1.0.0'
group = 'foo.bar'
eivgtgni

eivgtgni4#

如果您希望避免样板代码,因为maven-publish插件不会将依赖项写入pom.xml
试试这个插件:android-maven-publish

publishing {
    publications {
        mavenAar(MavenPublication) {
            groupId 'com.example'
            artifactId 'mylibrary'
            version '1.0.0'
            from components.android
        }
    }

    repositories {
        maven {
            url "$buildDir/releases"
        }
    }
}

更新日期:

android-maven-publish插件已弃用,因为AGP正式支持maven-publish

pcww981p

pcww981p5#

这就是我如何使用Kotlin DSL(build.gradle.kts)包含Dokka(查看online)和my Android Kotlin library的源代码JAR的方法:

plugins {
    // ...
    id("org.jetbrains.dokka") version "1.4.32"
    id("maven-publish")
}

lateinit var sourcesArtifact: PublishArtifact
lateinit var javadocArtifact: PublishArtifact

tasks {
    val sourcesJar by creating(Jar::class) {
        archiveClassifier.set("sources")
        from(android.sourceSets["main"].java.srcDirs)
    }
    val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class)
    val javadocJar by creating(Jar::class) {
        dependsOn(dokkaHtml)
        archiveClassifier.set("javadoc")
        from(dokkaHtml.outputDirectory)
    }
    artifacts {
        sourcesArtifact = archives(sourcesJar)
        javadocArtifact = archives(javadocJar)
    }
}

publishing {
    // ...
    publications {
        create<MavenPublication>("MyPublication") {
            from(components["release"])
            artifact(sourcesArtifact)
            artifact(javadocArtifact)
            // ...
        }
    }
}
waxmsbnn

waxmsbnn6#

您也可以使用android maven plugin,它创建.aar、javadoc.jar、sources.jar和.pom,并在将文件上传到maven存储库后更新maven-metadata.xml。

apply plugin: 'com.android.library'
apply plugin: 'maven'

//Your android configuration
android {
    //...
}

//maven repository info
group = 'com.example'
version = '1.0.0'

ext {
    //Specify your maven repository url here
    repositoryUrl = 'ftp://your.maven.repository.com/maven2'
    //Or you can use 'file:\\\\C:\\Temp' or 'maven-temp' for a local maven repository
}

//Upload android library to maven with javadoc and android sources
configurations {
    deployerJars
}

//If you want to deploy to an ftp server
dependencies {
    deployerJars "org.apache.maven.wagon:wagon-ftp:2.2"
}

// custom tasks for creating source/javadoc jars
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    destinationDir = file("../javadoc/")
    failOnError false
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

//Creating sources with comments
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.srcDirs
}

//Put the androidSources and javadoc to the artifacts
artifacts {
    archives androidSourcesJar
    archives javadocJar
}

uploadArchives {
    repositories {
        mavenDeployer {
            configuration = configurations.deployerJars
            repository(url: repositoryUrl) {
                //if your repository needs authentication
                authentication(userName: "username", password: "password")
            }
        }
    }
}

打电话给

./gradlew uploadArchives
9vw9lbht

9vw9lbht7#

使用Kotlinbuild.gradle.kts

publishing.publications {
    register<MavenPublication>("aar") {
        groupId = "com.foo"
        artifactId = "bar"
        version = "0.1"

        artifact("$buildDir/outputs/aar/bar-release.aar")

        pom.withXml {
            val dependencies = asNode().appendNode("dependencies")

            val addNode = { groupId: String, artifactId: String, version: String ->
                val dependency = dependencies.appendNode("dependency")
                dependency.appendNode("groupId", groupId)
                dependency.appendNode("artifactId", artifactId)
                dependency.appendNode("version", version)
            }

            addNode("com.example", "dependency-name", "1.0")
        }
    }
}

相关问题