5

我认为 IntelliJ 在生成 R.java 文件时存在一些问题,这会导致 AndroidAnnotations 出现问题,因为它无法解析我的布局的资源 ID。

这是我的活动:

package com.airlocksoftware.canvastest;

import android.app.Activity;
import android.os.Bundle;
import com.googlecode.androidannotations.annotations.EActivity;

@EActivity(R.layout.main)
public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

奇怪的是,即使我注释掉注释,并且 do setContentView(R.layout.main),即使 R.java 从未导入,它也能正常工作。

顺便说一句,布局只是一个简单的“Hello World”。那里没有什么奇怪的事情发生。

无论我做什么,我都无法让 AndroidAnnotations 生成CanvasActivity_.java. 我的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.airlocksoftware.canvastest</groupId>
    <artifactId>CanvasTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <android.version>4.2.2_r2</android.version>
        <android.platform>17</android.platform>
        <androidannotations.version>2.7.1</androidannotations.version>
        <java.version>1.6</java.version>
    </properties>

    <repositories>
        <repository>
            <id>local-repo</id>
            <url>file:///${env.HOME}/.m2/repository</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>android</groupId>
            <artifactId>android</artifactId>
            <version>${android.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.androidannotations</groupId>
            <artifactId>androidannotations</artifactId>
            <version>${androidannotations.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.androidannotations</groupId>
            <artifactId>androidannotations-api</artifactId>
            <version>${androidannotations.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <sdk>
                        <platform>${android.platform}</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

注释处理设置如此屏幕截图所示。

注释处理

基本上,我将处理器路径设置为指向本地 maven 存储库中的 jar 文件,如此处所述。

项目源的设置如此屏幕截图所示。

项目来源

无论我如何制作/重建项目,gen/aa 中都没有出现任何内容(因此我无法引用 CanvasActivity_.java)我从项目根目录运行 mvn clean install,但此堆栈跟踪失败。

INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.363s
[INFO] Finished at: Sat May 18 15:53:06 MDT 2013
[INFO] Final Memory: 11M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project CanvasProto: Compilation failure: Compilation failure:
[ERROR] /Users/matthewbbishop/Clients/proto/CanvasProto/src/main/java/com/airlocksoftware/CanvasProto/CanvasActivity.java:[6,38] cannot find symbol
[ERROR] symbol  : class R
[ERROR] location: package com.airlocksoftware.CanvasProto
[ERROR] /Users/matthewbbishop/Clients//proto/CanvasProto/src/main/java/com/airlocksoftware/CanvasProto/CanvasActivity.java:[11,12] package R does not exist
[ERROR] /Users/matthewbbishop/Clients/proto/CanvasProto/src/main/java/com/airlocksoftware/CanvasProto/CanvasActivity.java:[14,15] package R does not exist
[ERROR] error: Unexpected error. Please report an issue on AndroidAnnotations, with the following content: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[ERROR] at com.googlecode.androidannotations.helper.AnnotationHelper.extractAnnotationParameter(AnnotationHelper.java:293)
[ERROR] at com.googlecode.androidannotations.helper.AnnotationHelper.extractAnnotationResIdValueParameter(AnnotationHelper.java:269)
[ERROR] at com.googlecode.androidannotations.helper.IdValidatorHelper.resIdsExist(IdValidatorHelper.java:45)
[ERROR] at com.googlecode.androidannotations.validation.EActivityValidator.validate(EActivityValidator.java:55)
[ERROR] at com.googlecode.androidannotations.validation.ModelValidator.validate(ModelValidator.java:56)
[ERROR] at com.googlecode.androidannotations.AndroidAnnotationProcessor.validateAnnotations(AndroidAnnotationProcessor.java:414)
[ERROR] at com.googlecode.androidannotations.AndroidAnnotationProcessor.processThrowing(AndroidAnnotationProcessor.java:364)
[ERROR] at com.googlecode.androidannotations.AndroidAnnotationProcessor.process(AndroidAnnotationProcessor.java:341)
[ERROR] at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:627)
[ERROR] at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:556)
[ERROR] at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:701)
[ERROR] at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:987)
[ERROR] at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
[ERROR] at com.sun.tools.javac.main.Main.compile(Main.java:353)
[ERROR] at com.sun.tools.javac.main.Main.compile(Main.java:279)
[ERROR] at com.sun.tools.javac.main.Main.compile(Main.java:270)
[ERROR] at com.sun.tools.javac.Main.compile(Main.java:87)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:597)
[ERROR] at org.codehaus.plexus.compiler.javac.JavacCompiler.compileInProcess0(JavacCompiler.java:551)
[ERROR] at org.codehaus.plexus.compiler.javac.JavacCompiler.compileInProcess(JavacCompiler.java:526)
[ERROR] at org.codehaus.plexus.compiler.javac.JavacCompiler.compile(JavacCompiler.java:167)
[ERROR] at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:678)
[ERROR] at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:128)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:597)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[ERROR] Caused by: java.lang.reflect.InvocationTargetException
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:597)
[ERROR] at com.googlecode.androidannotations.helper.AnnotationHelper.extractAnnotationParameter(AnnotationHelper.java:287)
[ERROR] ... 46 more
[ERROR] Caused by: java.lang.annotation.AnnotationTypeMismatchException: Incorrectly typed data found for annotation element public abstract int com.googlecode.androidannotations.annotations.EActivity.value() (Found data of type int)
[ERROR] at com.sun.tools.javac.model.AnnotationProxyMaker$ValueVisitor$1.generateException(AnnotationProxyMaker.java:243)
[ERROR] at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:56)
[ERROR] at $Proxy16.value(Unknown Source)
[ERROR] ... 51 more

老实说,我现在很茫然。如果有人知道该怎么做,那将是惊人的。谢谢!

编辑:这是 IntelliJ 生成的存根 R.java。

/*___Generated_by_IDEA___*/

package com.airlocksoftware.canvastest;

/* This stub is for using by IDE only. It is NOT the R class actually packed into APK */
public final class R {
}
4

2 回答 2

1

这是因为您pom.xml缺少以下内容:

<packaging>apk</packaging>

没有这个R.java文件将不会生成。

将其添加到您的顶部,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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.airlocksoftware.canvastest</groupId>
    <artifactId>CanvasTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>apk</packaging>

    /* ... */
    /* rest are fine */
    /* ... */
于 2013-05-28T11:13:40.233 回答
0

这很奇怪。你的 Maven 配置看起来很棒。至少,它应该可以通过 Maven 正常编译...我不认为是这样,因为错误应该不同,但是您是否更新到最后一个 Android SDK 22 ?

于 2013-05-19T17:14:46.193 回答