Java Gradle Maven 插件“安装”任务不适用于 Android 库项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18559932/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:02:59  来源:igfitidea点击:

Gradle Maven plugin "install" task does not work with Android library project

javaandroidmaveninstallgradle

提问by Nimrod Dayan

I want to install android library project to local maven repository. Here is build.gradle:

我想将 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
    }
}

When I run:

当我运行时:

gradle install -i

it gets stuck here:

它卡在这里:

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

So first thing I noticed is that it's trying for some odd reason to deploy it on a device as APK.

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

Am I doing something wrong or is it just android-library plugin not compatible with maven plugin?

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

采纳答案by dcendents

Edit:Please refer to the github page (https://github.com/dcendents/android-maven-gradle-plugin) for the latest instructions and find the correct version to use. The original instructions are not suitable anymore with the latest gradle release.

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

Original Post:

原帖:

I've modified the maven plugin to be compatible with android library projects. See the project on github: https://github.com/dcendents/android-maven-gradle-plugin

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

Configure your android library projects to use it:

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

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.github.dcendents:android-maven-plugin:1.0'
    }
}

apply plugin: 'android-library'
apply plugin: 'android-maven'

Then you should be able to install aar into your local maven repository using the install task.

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

Hope this helps, if you find issues with the plugin please let me know on github and I'll fix it.

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

回答by CyclingSir

I just solved the issue by defining an upload archive as described here: Gradle documentation 52.6.2. Deploying to a Maven repository

我刚刚通过定义上传档案解决了这个问题,如下所述: Gradle 文档 52.6.2。部署到 Maven 存储库

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

calling

打电话

gradle uploadArchives

deploys the artefact to the (in my case local) Maven repo. I havn't found a simple and more flexible way to specify the local repo's url with e.g. mavenLocal() yet but the above suits my needs.

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

回答by Miroslav Bajto?

UPDATE 2014-11-26

更新 2014-11-26

The answer below made sense at the time of writing, when Android Build Tools were at version 0.5.5. It is most likely outdated now and probably does not work anymore.

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

I have personally switched my projects to use android-maven-pluginas described in the answer above, the plugin works fine with the recent versions of Android Build Tools too.

我个人已经android-maven-plugin按照上面的答案中的描述将我的项目切换为使用,该插件也适用于最新版本的 Android Build Tools。

THE ORIGINAL ANSWER FROM FEBRUARY 2014

2014 年 2 月的原始答案

Publishing as AAR

作为 AAR 发布

If you don't mind using an older version of com.android.tools.build:gradle(i.e. 0.5.4), you can use the approach described in this blogpost. Not that according to the discussion in adt-dev mailing-list, this does not work in 0.5.5.

如果你不介意使用的旧版本com.android.tools.build:gradle(即0.5.4),您可以使用描述的方法我这篇文章。并不是说根据adt-dev mailing-list 中的讨论,这在 0.5.5 中不起作用。

Add the following lines to your build.gradle:

将以下行添加到您的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
        }
    }
}

To publish to your local maven repo, call this command:

要发布到您的本地 Maven 存储库,请调用以下命令:

gradle publishToMavenLocal

Publishing as JAR

发布为 JAR

If your Android Library does not have custom resources and can be published as JAR, then you can use the following build.gradlethat works even with 0.5.5.

如果您的 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
        }
    }
}

To publish to your local maven repo, call this command:

要发布到您的本地 Maven 存储库,请调用以下命令:

gradle publishToMavenLocal

回答by Greg

There's an easier solution if you don't want to use a custom plugin. Instead, just recreate the install task with a different name. I called it installArchives. Add the following code to your build.gradle:

如果您不想使用自定义插件,有一个更简单的解决方案。相反,只需使用不同的名称重新创建安装任务。我叫它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'
    }
}

You can now run gradle installArchivesto install your aar locally.

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

回答by viphe

Elaborating on CyclingSir's answer, I propose to add a separate "installArchives" task. This should also take care of picking up your custom artifacts (e.g. sources).

详细阐述 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
    }
  }
}

Note that with Gradle Android plugin v0.5.5, gradle installstill tries to install something on a device.

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