简体   繁体   English

Android项目的Maven编译错误“错误:包R不存在”

[英]Maven compilation error for android Project “error: package R does not exist ”

I am trying to set up a MAVEN project with Android application. 我正在尝试使用Android应用程序设置MAVEN项目。 I have this pom file 我有这个pom文件

<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.myproject</groupId>
  <artifactId>userprofile</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>userprofile</name>
  <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
</project>

and during MAVEN compile I get this error (about 100 times, ie as many times as it is used in my methods) 在MAVEN编译期间,我收到此错误(大约100次,即我的方法中使用的次数)

    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR : 
    [INFO] -------------------------------------------------------------
src\main\java\com\myproject\userprofile\BaseActivity.java:[52,43] error: package R does not exist
    Process finished with exit code 1

Any idea about this error? 有关此错误的任何想法? On the web I either find unanswered questions about similar error output. 在网络上,我要么找到关于类似错误输出的未解答的问题。 I have no experience on MAVEN, so I believe I am missing something here. 我没有MAVEN的经验,所以我相信我在这里遗漏了一些东西。

R class is build by your IDE during compilation. R类在编译期间由IDE构建。 MAVEN cannot find the R class because by default the class can be found under build folder. MAVEN找不到R类,因为默认情况下可以在build文件夹下找到该类。 You need to add something like this 你需要添加这样的东西

<sourceDirectory>build</sourceDirectory>
<outputDirectory>target</outputDirectory>

telling MAVEN that you have some resource files under build folder and you want to make them available to compile so add them under the target folder which will be under your project. 告诉MAVEN您在构建文件夹下有一些资源文件,并且您希望使它们可用于编译,因此将它们添加到将在您的项目下的目标文件夹下。

so now I have 所以现在我有

build
  |----res
  |----src
src
  |----main
       |----java
       |----res 
target

After a quick look, I believe you're missing the build element. 快速浏览后,我相信你错过了build元素。 Maven is building the project with current sources, R and other gen classes have not been generated yet. Maven正在使用当前来源构建项目, R和其他gen类尚未生成。 At least you need something like (after dependencies tag): 至少你需要类似的东西(在dependencies标签之后):

<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>src</sourceDirectory>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.7.0</version>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <configuration>
                <sdk>
                    <!-- platform or api level (api level 16 = platform 4.1)-->
                    <platform>16</platform>
                </sdk>
            </configuration>
        </plugin>
    </plugins>
</build>

Also the packaging must be apk : 包装必须是apk

<packaging>apk</packaging>

I would strongly recommend to start reading the Android Maven documentation along with samples . 我强烈建议您开始阅读Android Maven文档以及示例

This error might happen if you had modify the package name that generate by archetype:generate , for me, I use android-quickstart to generate the module structure : 如果您修改了由archetype生成的包名称,则可能会发生此错误对于我,我使用android-quickstart生成模块结构:

mvn archetype:generate \
    -DarchetypeArtifactId=android-quickstart \
    -DarchetypeGroupId=de.akquinet.android.archetypes \
    -DarchetypeVersion=1.0.11 \
    -DgroupId=com.yy.android.gameLibs \
    -DartifactId=sample

akquient recommend me to use "com.yy.android.gameLibs" as package name and I accepted, I compile this module successful and worked. akquient建议我使用“com.yy.android.gameLibs”作为包名,我接受了,我编译这个模块成功并且工作了。 After that, I change the package name as "com.yy.android.sample" also change the Androidmenifest.xml package attribute, therefore the module report that error, I follow back the generate command and choice package name myself to solve this. 之后,我将包名更改为“com.yy.android.sample”也更改了Androidmenifest.xml包属性,因此模块报告该错误,我自己跟回生成命令和选择包名来解决此问题。

Modify packaging to apklib, like this: 将包装修改为apklib,如下所示:

<packaging>apklib</packaging>

And add build goal at end of pom.xml like this: 并在pom.xml的末尾添加构建目标,如下所示:

<dependencies>
    <!--Android deps -->
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.0.1.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>        
    <plugins>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <sdk>
                    <platform>17</platform>
                </sdk>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
</build>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM