Extract jar from aar for cordova plugin, Class not found error at runtime

1.1k Views Asked by At

I have an "aar" file that was generated using gradle with

apply plugin: 'com.android.library'

Now I want to use this aar library to create a cordova plugin. I unzip the aar file, get a "classes.jar" and put the jar into cordova's "libs" directory. My cordova project compiles without any problem, I can import and use classes in the jar file. But it crushed at run time with the following error

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/pkg/R$raw;

Package "com.example.pkg" is in my "classes.jar", but there is no class call "R$raw". What did I miss?

2

There are 2 best solutions below

1
On BEST ANSWER

AAR files bundle not only source but also the libraries self contained Android resources. Take a look at the contents of an AAR bundle.

It appears the original AAR file you had contained some raw resources which are not included in the jar. You cannot package Android resources in a .jar file so your application is crashing when it needs to access the ID's generated for resources.

You can check this by opening up the original AAR file (it is simply a zip archive). Take a look under the res folder for any resources that are needed by the library.

0
On

Yes, extracting classes.jar is not going to work and it will throw runtimeexceptions...

What i did to solve the problem is:

After cordova project creation, adding platform, adding plugin is done.

Lets say the aar file are copied in libs folder. ( assume file name is cards.aar )

then in app build.gradle specify following and click sync project with Gradle files.

repositories { flatDir { dirs 'libs' } }

dependencies { compile(name:'cards', ext:'aar') }

It works for me!! but one drawback of this solution is .. you need to open the project from platform folder on Android Studio and add the above lines to your build.gradle