Java.lang.NoClassDefFoundError : org/apache/lucene/codecs/codec error

Java.lang.NoClassDefFoundError : org/apache/lucene/codecs/codec error … here is a solution to the problem.

Java.lang.NoClassDefFoundError : org/apache/lucene/codecs/codec error

I have a project that takes Lucene (4.3.0) and adds the following dependencies:
lucene-core,lucene-analyzers-common,lucene-queries,lucene-queryparser.

Also, after adding the lucene-codecs dependency, the error is also reported.

However, lucene-core jars contain codecs class

—– start to be abnormal—–

I/TestRunner(2443): java.lang.NoClassDefFoundError: org/apache/lucene/codecs/Codec
I/TestRunner(2443):     at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:118)
I/TestRunner(2443):     at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:144)
I/TestRunner(2443):     at com.my.search.SearchIndexManager.newWriter(SearchIndexManager.java:301)
I/TestRunner(2443):     at com.my.search.SearchIndexManager.addIndexState(SearchIndexManager.java:95)
I/TestRunner(2443):     at com.my.SearchOperation.addIndexer(SearchOperation.java:68)
I/TestRunner(2443):     at com.my.test.SearchOperationTest.testSearchWithFilter(SearchOperationTest.java:208)
I/TestRunner(2443):     at java.lang.reflect.Method.invokeNative(Native Method)
I/TestRunner(2443):     at java.lang.reflect.Method.invoke(Method.java:511)
I/TestRunner(2443):     at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
I/TestRunner(2443):     at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
I/TestRunner(2443):     at junit.framework.TestCase.runBare(TestCase.java:134)
I/TestRunner(2443):     at junit.framework.TestResult$1.protect(TestResult.java:115)
I/TestRunner(2443):     at junit.framework.TestResult.runProtected(TestResult.java:133)
I/TestRunner(2443):     at junit.framework.TestResult.run(TestResult.java:118)
I/TestRunner(2443):     at junit.framework.TestCase.run(TestCase.java:124)
I/TestRunner(2443):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
I/TestRunner(2443):     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
I/TestRunner(2443):     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
I/TestRunner(2443):     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
I/TestRunner(2736): Caused by: java.lang.IllegalArgumentException: A SPI class of type org.apache.lucene.codecs.Codec with name 'Lucene42' does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath.The current classpath supports the following names: []
I/TestRunner(2736):     at org.apache.lucene.util.NamedSPILoader.lookup(NamedSPILoader.java:109)
I/TestRunner(2736):     at org.apache.lucene.codecs.Codec.forName(Codec.java:95)
I/TestRunner(2736):     at org.apache.lucene.codecs.Codec.<clinit>(Codec.java:122)
I/TestRunner(2736):     ... 19 more

—–End Exception—–

Solution

A Google search yielded some interesting results.

https://issues.apache.org/jira/browse/LUCENE-4204

Based on the above issue, the Android APK builder removed some files in the META-INF/services directory required by Lucene. They come up with workarounds such as adding the necessary files directly to the APK from Lucene jar through some ant tasks done after the APK builder is completed. Be careful because some lucene jars use the same files in the META-INF/services directory, and you may overwrite them unnecessarily.

I actually recommend that you use the android maven plugin because they already have the fix applied for this (see https://code.google.com/p/maven-android-plugin/issues/detail?id=97). Just use the latest version of the plugin (or anything above 3.2.1).

Related Problems and Solutions