【问题标题】:Gradle Maven plugin "install" task does not work with Android library projectGradle Maven 插件“安装”任务不适用于 Android 库项目
【发布时间】:2013-09-04 18:41:01
【问题描述】:

我想将 android 库项目安装到本地 maven 存储库。 这是build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'
apply plugin: 'maven'

version = "1.0.0-SNAPSHOT"
group = "com.example"

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 18
    }
}

当我跑步时:

gradle install -i

这里卡住了:

Executing task ':project:installTest' due to:
  Task has not declared any outputs.
Starting process 'command 'd:\android-sdk-windows\platform-tools\adb.exe''. Working directory: D:\Projects\java\....... Command: d:\android-sdk-windows\platform-tools\adb.exe install -r D:\Projects\java\.......\build\apk\project.apk
An attempt to initialize for well behaving parent process finished.
Successfully started process 'command 'd:\android-sdk-windows\platform-tools\adb.exe''
> Building > :project:installTest

所以我注意到的第一件事是它出于某种奇怪的原因尝试将其作为 APK 部署在设备上。

我做错了什么还是只是 android-library 插件与 maven 插件不兼容?

【问题讨论】:

    标签: java android maven installation gradle


    【解决方案1】:

    编辑:请参考 github 页面 (https://github.com/dcendents/android-maven-gradle-plugin) 获取最新说明并找到要使用的正确版本。原始说明不再适用于最新的 gradle 版本。

    原帖:

    我已经修改了 maven 插件以与 android 库项目兼容。项目见github:https://github.com/dcendents/android-maven-gradle-plugin

    配置您的 android 库项目以使用它:

    buildscript {
        repositories {
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.github.dcendents:android-maven-plugin:1.0'
        }
    }
    
    apply plugin: 'android-library'
    apply plugin: 'android-maven'
    

    然后您应该能够使用 install 任务将 aar 安装到您的本地 maven 存储库中。

    希望对您有所帮助,如果您发现插件有问题,请在 github 上告诉我,我会修复它。

    【讨论】:

    • 如何使用这个插件配置groupid、artifactid和版本?
    • 这是一个非常好的解决方案。我现在在我的图书馆项目中使用它!我唯一担心的是,您将如何应对 Gradle Maven 插件的新更新,您是否要让这些更新保持同步?
    • 好吧,到目前为止我不需要这样做,但是如果你(或其他人)注意到它在使用新的 gradle 版本时会中断,那么请在 github 项目上打开一个问题,我会发布一个更新版本。
    • 效果很好,并在此处添加步骤 1 中的几个部分:virag.si/2015/01/publishing-gradle-android-library-to-jcenter,使其也可以推送源代码和 javadoc。
    • 我在使用此解决方案时收到org.gradle.api.internal.project.ProjectInternal.getPluginManager()Lorg/gradle/api/internal/plugins/PluginManagerInternal;
    【解决方案2】:

    详细说明 CyclingSir 的回答,我建议添加一个单独的“installArchives”任务。这也应该注意拾取您的自定义工件(例如源)。

    apply plugin: 'maven'
    
    task installArchives(type: Upload) {
      description "Installs the artifacts to the local Maven repository."
      configuration = configurations['archives']
      repositories {
        mavenDeployer {
          repository url: repositories.mavenLocal().url
        }
      }
    }
    

    请注意,使用 Gradle Android 插件 v0.5.5,gradle install 仍会尝试在设备上安装某些内容。

    【讨论】:

    • 效果很好!您可以使用repositories.mavenLocal().url 而不是对其进行硬编码。
    【解决方案3】:

    如果您不想使用自定义插件,有一个更简单的解决方案。相反,只需使用不同的名称重新创建安装任务。我称它为installArchives。将以下代码添加到您的build.gradle

    task installArchives(type: Upload) {
        description "Installs the artifacts to the local Maven repository."
        repositories.mavenInstaller {
            configuration = configurations.default
            pom.groupId = 'my.group'
            pom.artifactId = 'my-artifact'
            pom.version = '1.0.0'
        }
    }
    

    您现在可以运行 gradle installArchives 在本地安装您的 aar。

    【讨论】:

    • 您能否修改您的任务以同时为源代码和 JavaDoc 生成 jars?
    • @JJD 此任务只会从默认配置上传 aar(无 jars)。我不相信 Android gradle 插件支持开箱即用地生成 JavaDocs 或源 aars。弄清楚如何做到这一点可能会引起自己的问题。
    • 当aar部署到maven repo时,生成的pom不包含库项目的依赖。
    • 我遇到了和 Moritz 一样的问题,但是使用 viphe 的方法,一切都按原样上传了。
    【解决方案4】:

    2014 年 11 月 26 日更新

    在撰写本文时,以下答案是有意义的,当时 Android 构建工具的版本为 0.5.5。它现在很可能已经过时并且可能不再起作用了。

    我已亲自将我的项目切换为使用android-maven-plugin,如上述答案中所述,该插件也适用于最新版本的 Android Build Tools。

    2014 年 2 月的原始答案

    作为 AAR 发布

    如果您不介意使用旧版本的com.android.tools.build:gradle(即0.5.4),您可以使用this blogpost 中描述的方法。并不是说根据the discussion in adt-dev mailing-list,这在 0.5.5 中不起作用。

    将以下行添加到您的build.gradle

    apply plugin: 'maven-publish'
    
    // load bundleRelease task
    // this will not load the task in 0.5.5
    android.libraryVariants
    
    publishing {
        publications {
            maven(MavenPublication) {
                artifact bundleRelease
            }
        }
    }
    

    要发布到本地 maven 仓库,请调用以下命令:

    gradle publishToMavenLocal
    

    以 JAR 形式发布

    如果您的 Android 库没有自定义资源并且可以作为 JAR 发布,那么您可以使用以下 build.gradle,即使在 0.5.5 中也可以使用。

    // build JAR file
    task androidReleaseJar(type: Jar, dependsOn: assembleRelease) {
        from "$buildDir/classes/release/"
    }
    
    apply plugin: 'maven-publish'
    
    publishing {
        publications {
            maven(MavenPublication) {
                artifact androidReleaseJar
            }
        }
    }
    

    要发布到本地 maven 仓库,请调用以下命令:

    gradle publishToMavenLocal
    

    【讨论】:

    • Android 0.5.5 和andoroid.libraryVariants 仍然没有解决方法。
    • 我个人已切换到 android-maven-plugin(请参阅已接受的答案),并且从未回头查看我的答案中描述的解决方法。
    • 对我不起作用。我收到一条错误消息,提示“找不到属性 'bundleRelease'”
    • 我的回答中提出的解决方案现在可能已经过时了。我们在我们的项目中使用android-maven-plugin,它对我们来说很好。
    【解决方案5】:

    我刚刚通过定义上传存档解决了这个问题,如下所述: Gradle documentation 52.6.2. Deploying to a Maven repository

    uploadArchives {
      repositories {
        mavenDeployer {
          repository(url: "file://${System.env.HOME}/.m2/repository/")
        }
      }
    }
    

    打电话

    gradle uploadArchives
    

    将工件部署到(在我的情况下是本地的)Maven 存储库。 我还没有找到一种简单且更灵活的方法来指定本地 repo 的 url,例如mavenLocal() 但以上满足我的需要。

    【讨论】:

    • 谢谢,但我要求 gradle install 而不是 gradle uploadArchives,在我的情况下,它用于将工件实际上传到 Maven 中心。我希望能够在将其发布到 Maven Central 之前安装在本地 maven 存储库中。
    • 这其实不是个好习惯——请参考Peter's answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2014-06-22
    • 2018-05-16
    • 2018-06-23
    • 1970-01-01
    • 2015-11-30
    • 2021-04-26
    相关资源
    最近更新 更多