1

I followed the official instructions to Add an AAR as a dependency. The build.gradle of the app has:

implementation files('libs/androidLibrary-release.aar')

The app builds and starts fine. However, it crashes when it uses a method of the library with the following in the logcat:

        java.lang.NoClassDefFoundError: Failed resolution of: Lorg/joda/time/DateTime;
            at net.mydomain.android.utility.getCurrentTime(SourceFile:1)
...
    Caused by: java.lang.ClassNotFoundException: Didn't find class "org.joda.time.DateTime" on path: DexPathList[[zip file "/data/app/com.mydomain.foo-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    ...
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Method getCurrentTime() uses joda-time. The library has the following in its build.gradle:

implementation 'joda-time:joda-time:2.10.5'

I also tried api as follows to no avail:

api 'joda-time:joda-time:2.10.5'

Could anyone offer a tip to help find the culprit?

I used to import aar libraries as modules and they worked flawlessly. I have just updated Android Studio to Arctic Fox and the option to import an aar library is no longer available.

Hong
  • 17,643
  • 21
  • 81
  • 142
  • 1
    1. Drag your apk to AS and search the `org.joda.time.DateTime` in the .dex files. 2. Assuming it not exists, download `joda-time` aar file from your maven repo and do step 1 again on the aar. – Shlomi Katriel Aug 04 '21 at 17:23
  • 1
    3. Keep in mind that even though you chose 2.10.5, it can be replaced by gradle in case of a conflict. Run `gradle :app:dependencies` command to verify the actual version – Shlomi Katriel Aug 04 '21 at 17:27
  • @ShlomiKatriel Thank you for the tip. I tried to open app-debug.apk with AS and it is still loading after 10 minutes. Did I do it incorrectly. I can us AS to open the library aar file. The class.jar of the aar does not seem to have org.joda... – Hong Aug 04 '21 at 17:49
  • I can open the apk now, but am unable to open classes.dex. When I select classes.dex, AS shows "Load Proguard mappings..." at the bottom and nothing else happens. – Hong Aug 04 '21 at 18:03
  • The error is gone after I added "implementation 'joda-time:joda-time:2.10.5'" to the build.gradle of the app. This cannot be the right way to do it because an aar library should be self-contained. – Hong Aug 04 '21 at 18:10
  • Well, I know when you add a dependencies, inner dependencies are automatically fetched. But not sure if it works the same with local AAR. – Shlomi Katriel Aug 04 '21 at 19:10
  • 1
    I realized something and asked another question: https://stackoverflow.com/questions/68656587/how-to-import-an-android-library-aar-as-a-module-with-android-studio-arctic-fox – Hong Aug 04 '21 at 19:15
  • Anyway, when someone publishes a library, the best practice is to embed the dependencies directly into the library to prevent issues like this and prevent version conflicts. My guess is that `org.joda.time.DateTime` is not embedded in the AAR file, therefore, the crash. Please verify that this class is indeed not exist in the AAR. – Shlomi Katriel Aug 04 '21 at 19:15
  • I used AS to open the library aar file. The class.jar of the aar does not seem to have org.joda... – Hong Aug 04 '21 at 19:19
  • @ShlomiKatriel Could you turn your first comment about opening apk with AS into an answer for me to accept? I think that is the right way to find the culprit. – Hong Aug 06 '21 at 14:46

0 Answers0