Converting to Maven project in Eclipse results in a ClassNotFoundException

I am using the m2eclipse plugin in Eclipse Juno, with JDK 1.7 (switches from JRE 1.6 based on some searches in Stack Overflow).

The bare bone Android app works great when created. However, when I convert it to a Maven project, I start to get a ClassNotFoundException whenever I try to start the application. I tried updating the project, restarting Eclipse, checking all the libraries in the build path, cleaning up and rebuilding the project, restarting the emulator and reducing my pom.xml to a minimum.

What is the reason for this error? Is this some simple wrong configuration for Maven?

My logcat:

07-09 23:07:18.027: D/AndroidRuntime(958): Shutting down VM 07-09 23:07:18.027: W/dalvikvm(958): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 07-09 23:07:18.137: E/AndroidRuntime(958): FATAL EXCEPTION: main 07-09 23:07:18.137: E/AndroidRuntime(958): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myproject/com.example.myproject.MainActivity}: java.lang.ClassNotFoundException: com.example.myproject.MainActivity 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.access$600(ActivityThread.java:123) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.os.Handler.dispatchMessage(Handler.java:99) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.os.Looper.loop(Looper.java:137) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.reflect.Method.invokeNative(Native Method) 07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.reflect.Method.invoke(Method.java:511) 07-09 23:07:18.137: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-09 23:07:18.137: E/AndroidRuntime(958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-09 23:07:18.137: E/AndroidRuntime(958): at dalvik.system.NativeStart.main(Native Method) 07-09 23:07:18.137: E/AndroidRuntime(958): Caused by: java.lang.ClassNotFoundException: com.example.myactivity.MainActivity 07-09 23:07:18.137: E/AndroidRuntime(958): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 07-09 23:07:18.137: E/AndroidRuntime(958): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 07-09 23:07:18.137: E/AndroidRuntime(958): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 07-09 23:07:18.137: E/AndroidRuntime(958): ... 11 more 

My pom.xml:

 <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.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <version>4.1.1.4</version> <scope>provided</scope> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project> 

My MainActivity.java:

 package com.example.myproject; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

Update: I found that trying to create Maven Clean and Maven will generate sources in pom.xml. However, installing Maven gives me an error:

 [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] MainActivity.java:[12,33] package R does not exist [ERROR] MainActivity.java:[18,44] package R does not exist 

I also added a few things (dependency and packaging), as well as a modified (1.6-> 1.7, 3.1-> 3.0) pom.xml file in this last board.

+4
source share
2 answers

In my opinion, to avoid such situations, you should not create a new project in Eclipse and then convert it to maven, but create an android project with maven from scratch. In addition, always create your own projects also in the terminal (just in case), because m2eclipse creates errors from time to time, especially in complex projects.

Try this (in terminal):

  • Create a folder, for example. myMvnProjects.
  • Go to this folder.
  • Type: mvn archetype:generate
  • Select de.akquinet.android.archetypes:android-quickstart (Creates a skeleton for an Android application) (or something else - it depends on your needs).
  • Provide the necessary parameters (platform, groupId, artifactId, version, etc.).
  • Press "Enter" (or return to Mac), and that he - Maven creates a simple project for you.
  • Type: mvn clean install and wait for it to build.

By the way, I'm curious why you have <packaging>jar</packaging> in your main Android project. You must have <packaging>apk</packaging> . Moreover, Maven works without problems with JDK 7.

+1
source

You cannot just create a jar in maven and deploy it on your device, expecting that android will work, android uses a special compiler and jar compression. something like this might help: http://books.sonatype.com/mvnref-book/reference/android-dev.html

0
source

Source: https://habr.com/ru/post/1490601/


All Articles