Search code examples
mavenandroid-gradle-pluginandroid-buildgradle-pluginmaven-dependency

How to import classes from `com.android.build.api.transform` package in Maven


I am trying to transfer a gradle-based build plugin for android to a maven build system. Up to now I was successful except with the android part.

It seems that I am missing classes from the package com.android.build.api.transform. Although I used this code

    <dependency>
        <groupId>com.android.tools.build</groupId>
        <artifactId>gradle</artifactId>
        <version>2.2.0</version>
        <type>jar</type>
    </dependency>

which is supposed to provide (rather indirectly) the required classes/packages, it seems that this is not working.

I also added

<repositories>
    <repository>
        <id>android</id>
        <name>android</name>
        <url>https://plugins.gradle.org/m2/</url>
    </repository>
</repositories>

just in case, with no luck. Still the project is not able to compile due to missing classes.

Any idea what I am missing? I am very new to the gradle/maven scene, and I still feel out of my waters with these tools.

For reference here is a link to the javadoc of this artifact.

Thanks for your help


Solution

  • The 'gradle' artifact does not contain the missing package. As you mention, it depends transitively on at least one artifact that contains that package, but only in scope 'runtime'. Hence, it will not be available to your project on the compile classpath.

    Anyway, adding the dependency explicitly in your pom.xml is the right thing to do. A search for the missing package on search.maven.org shows that you have the choice between com.android.tools.build:transform-api and com.android.tools.build:gradle-api. Locking at the transform-api artifact reveals that it's deprecated and encourages to use gradle-api instead. (The latest version is '2.0.0-deprecated-use-gradle-api'). I downloaded the gradle-api jar file, which indeed has the missing package. So please try adding that artifact to your pom.xml instead:

    <dependency>
        <groupId>com.android.tools.build</groupId>
        <artifactId>gradle-api</artifactId>
        <version>2.3.0</version>
    </dependency>
    

    I didn't check version 2.2.0, but if you for some reason want to use the older version, it will probably work too.