简体   繁体   中英

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

I am trying to set up a MAVEN project with Android application. I have this pom file

<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)

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

R class is build by your IDE during compilation. MAVEN cannot find the R class because by default the class can be found under build folder. 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.

so now I have

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

After a quick look, I believe you're missing the build element. Maven is building the project with current sources, R and other gen classes have not been generated yet. At least you need something like (after dependencies tag):

<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 :

<packaging>apk</packaging>

I would strongly recommend to start reading the Android Maven documentation along with samples .

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 :

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. 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.

Modify packaging to apklib, like this:

<packaging>apklib</packaging>

And add build goal at end of pom.xml like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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