Question

I try to use Maven and androidannotations to build my apk, but independently of any IDE (I'm actually using IntelliJ IDEA and not Eclipse, but I would like to have it entirely IDE-independent, so that it would also work perfectly on any build server).

The annotations seem to get processed properly, but they're not being compiled into the apk, which is where I'm currently stuck.

I try to use the <includes> section in the maven-compiler-plugin, and the path should be correct - it exists and also contains the annotation processed, generated, java class, which is the Android main activity but with a underscore (_) suffix.

There is a wiki page description how to use Maven+Eclipse, but it's too much bound to the Eclipse IDE. https://github.com/excilys/androidannotations/wiki/Building-Project-Maven-Eclipse so it doesn't help me solve the problem.

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>de-mycompany-myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>com-mycompany-base-myproject</name>

    <prerequisites>
        <maven>2.2.1</maven>
    </prerequisites>

    <properties>
        <platform.version>2.3.3</platform.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.androidannotations</groupId>
            <artifactId>androidannotations</artifactId>
            <version>2.6</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.androidannotations</groupId>
            <artifactId>androidannotations</artifactId>
            <classifier>api</classifier>
            <version>2.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>10</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <includes>
                        <include>${project.basedir}/target/generated-sources/apt/**</include>
                        <!--<include>target/generated-sources/apt/**</include>-->
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>versions-maven-plugin</artifactId>
                <groupId>org.codehaus.mojo</groupId>
                <version>1.3.1</version>
            </plugin>


            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.5</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>com.googlecode.androidannotations.AndroidAnnotationProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
                <dependencies/>
            </plugin>

        </plugins>
    </build>

</project>

mvn install even shows me the -s compiler option with the correct path:

[INFO] javac option: -proc:only
[INFO] javac option: -processor
[INFO] javac option: com.googlecode.androidannotations.AndroidAnnotationProcessor
[INFO] javac option: -d
[INFO] javac option: /Users/myuser/path/to/com.mycompany.myproject/target/classes
[INFO] javac option: -s
[INFO] javac option: /Users/myuser/path/to/com.mycompany.myproject/target/generated-sources/apt
[INFO] diagnostic Note: Starting AndroidAnnotations annotation processing
[INFO] diagnostic warning: Supported source version 'RELEASE_6' from annotation processor 'com.googlecode.androidannotations.AndroidAnnotationProcessor' less than -source '1.7'
[INFO] diagnostic Note: Dummy source file: file:///Users/path/to/com.mycompany.myproject/target/generated-sources/apt/dummy1341816057285.java
[INFO] diagnostic Note: AndroidManifest.xml file found: /Users/myuser/path/to/com.mycompany.myproject/AndroidManifest.xml
[INFO] diagnostic Note: Number of files generated by AndroidAnnotations: 1
[INFO] diagnostic Note: Generating source file: com.mycompany.myproject.activity.HelloAndroidActivity_

(The complete log of mvn install is here: http://pastebin.com/6dQkcNXD)

But still running the apk fails with:

E/AndroidRuntime( 6942): Caused by: java.lang.ClassNotFoundException: com.mycompany.myproject.activity.HelloAndroidActivity_ in loader dalvik.system.PathClassLoader

and the annotation processed HelloAndroidActivity_ is not inside the apk / classes.dex.

Was it helpful?

Solution

Found the problem: <extensions>true</extensions> was missing under maven-compiler-plugin:

   <plugin>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.2</version>
       <configuration>
           <source>1.6</source>
           <target>1.6</target>
           <includes>
               <include>${project.basedir}/target/generated-sources/apt/**</include>
           </includes>
       </configuration>
   <extensions>true</extensions>
   </plugin>              

OTHER TIPS

After further research, I've determined that my original answer was incorrect.

The problem actually lies in the generated .classpath file. See: https://gist.github.com/cwc/5224145

In that example, broken_cp.xml causes the processed annotations to not be included in the APK when used as .classpath. Changing its contents to that of working_cp.xml and refreshing the project (prompting a build) immediately fixes the APK issue, and I can launch my Android app from Eclipse successfully.

The broken form of the classpath file is regenerated every time I use the Maven > Update Project... project menu option to reload POM files. I'm working around the issue by paying close attention to updates to the .classpath file, and making sure I don't commit a broken version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top