Search code examples
javaandroidjarexternalnoclassdeffounderror

NoClassDefFoundError when adding external jar to a pure java module, which is a dependency of the app module in Android Studio


First of all I am rather new to Android development, so excuse me in advance if I do not use the correct semantics. Please feel free to correct me on this.

So I have an Android Studio project which includes:

  • Android app module
  • Pure Java module

The pure Java module is a dependency of the Android app module. Now, I want to add an external.jar to the Pure java module. I have done this by adding the jar to pure_java_module\lib\ and everything compiles fine.

The problem is on runtime, the first time the app is trying to access one of the classes in external.jar, it throws a NoClassDefFoundError.

I have searched long and hard on this website and in Google in general, and all the solutions I have tried failed so far. To mention what I have tried:

  • Using clean project/"gradlew.bat clean" and trying again
  • Adding external.jar to the \libs\ under the Android app module and then pressing on it right click->Add as Library... and selecting just the Pure Java module/both modules
  • Adding the external.jar as a module and then adding it as a dependency of the Pure Java module: File->New->New Module...->import .JAR/.AAR package

I am seriously at a loss here. Any help would be very much appreciated.


Solution

  • Well, eventually I solved it only it's not really a direct solution, it's more of a workaround. I happened to have access to the source code of the external.jar I wanted to use, so I just added the classes and it works fine.

    But if someone sees this question and can explain how to add external.jar to a Pure Java module that is a dependency for an Android app module it would be the better solution, as of course you can't always have access to the source code of a library.