Maven compilation error for android Project "Error: R package does not exist"

I am trying to create a MAVEN project with an 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 compilation of MAVEN I get this error (about 100 times, i.e. as many times as 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 

Are there any ideas about this error? On the Internet, I either find unanswered questions about this error output. I have no experience at MAVEN, so I believe that something is missing here.

+5
java android maven
source share
4 answers

An R class is created by your IDE at compile time. MAVEN cannot find class R, because by default the class can be found in the assembly folder. You need to add something like this

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

telling MAVEN that you have resource files in the build folder and you want to make them available for compilation in order to add them to the target folder that will be under your project.

so now i

 build |----res |----src src |----main |----java |----res target 
+3
source share

After a quick look, I find that you are missing the build element. Maven is building a project with current sources, R and other gen classes have not yet been created. At least you need something like (after the 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 should be apk :

 <packaging>apk</packaging> 

I would highly recommend starting reading the Android Maven documentation along with samples .

0
source share

This error may occur if you change the package name generated by archetype: generate , for me I use android-quickstart to create 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 recommends that I use "com.yy.android.gameLibs" as the name of the package, and I accepted, I compiled this module and worked. After that, I change the name of the package as "com.yy.android.sample", also change the attribute of the package Androidmenifest.xml, so the module reports an error, I am responsible for the generation command and choosing the name of the package to solve this problem.

0
source share

Change the packaging to apklib, for example:

 <packaging>apklib</packaging> 

And add the build target at the end of pom.xml as follows:

 <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> 
0
source share

All Articles