没有使用maven将处理(生成)源编译到apk中

时间:2012-07-09 06:49:19

标签: android maven annotations annotation-processing android-annotations

我尝试使用Maven和androidannotations构建我的apk,但独立于任何IDE(我实际上使用的是IntelliJ IDEA而不是Eclipse,但我希望它完全独立于IDE,所以它也适用于任何构建服务器。)

注释似乎得到了正确处理,但是它们没有被编译到apk 中,这是我目前所处的位置。

我尝试使用<includes>中的maven-compiler-plugin部分,路径应该是正确的 - 它存在并且还包含已处理,生成的注释,java类,这是Android主要活动但是带下划线(_)后缀。

有一个wiki页面描述如何使用Maven + Eclipse,但它与Eclipse IDE绑定得太多了。 https://github.com/excilys/androidannotations/wiki/Building-Project-Maven-Eclipse 所以它无助于我解决问题。

这是我的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甚至向我显示了具有正确路径的-s编译器选项:

[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_

mvn install的完整记录在这里:http://pastebin.com/6dQkcNXD

但仍在运行apk失败:

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

并且处理的注释HelloAndroidActivity_不在apk / classes.dex中。

2 个答案:

答案 0 :(得分:9)

发现问题:<extensions>true</extensions>下缺少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>              

答案 1 :(得分:1)

经过进一步研究,我确定我的原始答案不正确。

问题实际上在于生成的.classpath文件。请参阅:https://gist.github.com/cwc/5224145

在该示例中,broken_cp.xml会导致处理后的注释在用作.classpath时不会包含在APK中。将其内容更改为working_cp.xml并刷新项目(提示构建)会立即修复APK问题,我可以成功从Eclipse启动我的Android应用程序。

每次使用 Maven&gt;时,都会重新生成类路径文件的损坏形式更新项目... 项目菜单选项以重新加载POM文件。我正在解决这个问题,密切关注.classpath文件的更新,并确保我没有提交损坏的版本。