Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError at Runtime on class inside .AAR in Android Studio

I've been researching for many hours about a possible solution to this but whatever I try it just fails. Here's the explained situation:

I have this library project on Android Studio which generates the app-debug.aar file. Then I add this new module in the implementing project, following the wizard to import .JAR/.AARs I pick the .aar and update the dependency for :app in the Project Structure.

Once the project is cleaned (even using ./gradlew clean--Mac OS X) I can make references of this class and its methods in any of the activities.

Now at runtime, once built and installed on the real devices the app will crash throwing the aforementioned Runtime Exception.

Note: I'm importing the .aar because I would need to protect the code and it has resources so a .jar is out of the question. And if I import the library module (with source and all instead of an.aar) then the app has no issues at runtime.

I've tried everything and if anyone can throw some light on what this could be, it'll be highly appreciated.

The class extends ViewGroup and is instantiated at runtime in case that implies anything. The code was given to me so major changes might not be possible unless it's absolutely necessary.

Thanks beforehand to all!

Armando

like image 463
Armando Avatar asked Oct 19 '22 17:10

Armando


2 Answers

For those, who are still looking for the solution, following two options worked for me to solve the exact same problem as OP has mentioned.

  • Include the (problematic) library dependency in the target module as well, i-e the in my case i was including the protobuf-lite as a dependency to my library module but getting noclassdefFound error when lib imported as .aar in app module. As a workaround, i added the protobuf-lite dependency to my app module too and it worked like charm.

  • Second option that worked for me that instead of adding the gradle dependency `compile 'com.google.protobuf:protobuf-lite:3.0.1', i downloaded the protobuf-jar from maven and added manually to the libs of my library module, and the problem got solved.

Don't know what's wrong with the gradle plugin, but hope it helps someone else looking for the same problem.

like image 180
Qasim Avatar answered Oct 22 '22 09:10

Qasim


The NoClassDefFound error has actually happening to a backward-compatibility library being used by this class I mentioned on the question ("Class A"). No details were given other than Class A couldn't be found and later on found out that another class had a similar issue BUT was pointing at that compat lib with the same exception and I noticed that Class A was also calling its methods and implementing its callbacks. By bumping up the min version (to use the native API) I could overcome that issue. It was indeed a problem of including that backward-comp library in the .aar or so it seems. Should I understand this issue a lot better I'll update this "answer."

like image 40
Armando Avatar answered Oct 22 '22 07:10

Armando