33

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

4

5 回答 5

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'

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

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

于 2013-10-21T21:53:11.327 回答
18

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

于 2014-03-17T21:11:16.203 回答
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。

于 2014-02-14T23:49:39.190 回答
6

更新 2014-11-26

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

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

2014 年 2 月的原始答案

作为 AAR 发布

如果您不介意使用旧版本com.android.tools.build:gradle(即 0.5.4),您可以使用本博文中描述的方法。并不是根据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
于 2013-10-14T15:36:28.433 回答
0

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

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

打电话

gradle uploadArchives

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

于 2013-10-12T09:23:55.690 回答