Mittwoch, 2. Juli 2014

Android daily pain : class R does not contain the correct constants

Yesterday I started preparing  the release of BARACUS 0.8. Therefore, I started to update all tutorials. By the new Android versions I surely updated my SDK in the meantime. In mind, just to do a version upgrade and use a new plugin version, I started migration.

So what I did was using a newer pom, update plugin and sdk version :

public class ApplicationContext extends BaracusApplicationContext {
 <properties>
        <android.sdk.path>C:/adt-bundle-windows-x86-20130219/sdk</android.sdk.path>
        <android.plugin.version>3.8.2</android.plugin.version>
        <android.sdk.version>18</android.sdk.version>
  <platform.version>4.0.1.2</platform.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <baracus.version>0.8</baracus.version>
 </properties>
... and ..,
public class ApplicationContext extends BaracusApplicationContext {
 <build>
  <finalName>${project.artifactId}</finalName>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>com.jayway.maven.plugins.android.generation2</groupId>
     <artifactId>android-maven-plugin</artifactId>
     <version>${android.plugin.version}</version>
     <extensions>true</extensions>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>${android.plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <sdk>
                        <platform>${android.sdk.version}</platform>
                    </sdk>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>


So, basically, I chose the setting mostly equals to the one in the framework. Also, I switched to use the new ModelBase class, since the AbstractModelBase becomes unavailable in this release.

Problem : The android project did not compile any more. But it did with the old plugin!?! WTF?!!

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/marcus/tutorial-fix/baracus-tutorial/src/main/java/net/mantucon/baracus/HelloAndroidActivity.java:[57,25] cannot find symbol
  symbol:   variable layout
  location: class net.mantucon.baracus.R
[ERROR] /home/marcus/tutorial-fix/baracus-tutorial/src/main/java/net/mantucon/baracus/HelloAndroidActivity.java:[89,57] cannot find symbol
  symbol:   variable menu
  location: class net.mantucon.baracus.R
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.913 s
[INFO] Finished at: 2014-07-02T15:28:54+01:00
[INFO] Final Memory: 27M/336M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project baracus-tutorial: Compilation failure: Compilation failure:
[ERROR] /home/marcus/tutorial-fix/baracus-tutorial/src/main/java/net/mantucon/baracus/HelloAndroidActivity.java:[57,25] cannot find symbol
[ERROR] symbol:   variable layout
[ERROR] location: class net.mantucon.baracus.R
[ERROR] /home/marcus/tutorial-fix/baracus-tutorial/src/main/java/net/mantucon/baracus/HelloAndroidActivity.java:[89,57] cannot find symbol
[ERROR] symbol:   variable menu
[ERROR] location: class net.mantucon.baracus.R
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Process finished with exit code 1

After searching a little bit, I found out that the class R exists twice. I imported the BARACUS framework and it shares the package namespace with the tutorial.

Reason: The latest Android SDK and plugin do not allow the Mixup of R's within the same namespace any more.

Solution : I did a refactoring, moving all tutorial classes to the baracustutorial namespace. This fixed the issue. Now the error disappeard. Thanks to IDEA Intellij, this was less than 10mins per project. Annoying was the fact, that every R-access had to be removed by Hand.





Keine Kommentare:

Kommentar veröffentlichen