Gradle Maven plugin “install” task does not work w

2019-01-16 09:31发布

问题:

I want to install android library project to local maven repository. Here is 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.

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

回答1:

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.

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

Configure your android library projects to use it:

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.

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



回答2:

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).

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 install still tries to install something on a device.



回答3:

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:

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 installArchives to install your aar locally.



回答4:

UPDATE 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.

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

THE ORIGINAL ANSWER FROM FEBRUARY 2014

Publishing as 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.

Add the following lines to your 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:

gradle publishToMavenLocal

Publishing as JAR

If your Android Library does not have custom resources and can be published as JAR, then you can use the following build.gradle that works even with 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:

gradle publishToMavenLocal


回答5:

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

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.