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

时间:2013-09-01 15:30:01

标签: java android maven install 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
    }
}

当我跑步时:

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插件不兼容?

5 个答案:

答案 0 :(得分:32)

编辑:请参阅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'

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

希望这有帮助,如果你发现插件有问题,请告诉我github,我会解决它。

答案 1 :(得分:17)

详细说明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仍尝试在设备上安装某些内容。

答案 2 :(得分:11)

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

答案 3 :(得分:6)

更新2014-11-26

在撰写本文时,Android Build Tools的版本为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

答案 4 :(得分:0)

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

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

致电

gradle uploadArchives

将人工制品部署到(在我的情况下是本地的)Maven仓库。 我没有找到一种简单而灵活的方式来指定本地回购的网址,例如mavenLocal()但是上面的内容适合我的需要。