Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create cache in Android #102

Closed
bhohler opened this issue Feb 13, 2019 · 7 comments

Comments

@bhohler
Copy link

commented Feb 13, 2019

It seems like issue #4 has cropped up again.

I found this recent Stack Overflow post that matches the exact issue I'm seeing as well: https://stackoverflow.com/questions/53583759/java-lang-noclassdeffounderror-when-using-cache2k-on-android

As I'm sure you're aware, the javax.naming package isn't available in Android which is the source of the NoClassDefFoundError. The catch block in JndiDefaultNameProvider.java doesn't actually catch this since Error and its subclasses aren't actually instances of Exception.

I've checked out the master branch and updated this class to also catch and ignore the NoClassDefFoundError but that's just revealed a similar issue later on in the initialization process when cache2k tries to use classes from the java.lang.management package:

2019-02-12 22:58:07.442 13788-13788/? I/Cache2kCoreProviderImpl: cache2k starting. version=1.4-SNAPSHOT
2019-02-12 22:58:07.464 13788-13788/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bhohler.test, PID: 13788
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
        at org.cache2k.impl.serverSide.JmxSupport.managerCreated(JmxSupport.java:80)
        at org.cache2k.core.CacheManagerImpl.<init>(CacheManagerImpl.java:115)
        at org.cache2k.core.Cache2kCoreProviderImpl.getManager(Cache2kCoreProviderImpl.java:158)
        at org.cache2k.core.Cache2kCoreProviderImpl.getManager(Cache2kCoreProviderImpl.java:133)
        at org.cache2k.CacheManager.getInstance(CacheManager.java:89)
        at org.cache2k.Cache2kBuilder.config(Cache2kBuilder.java:161)
        at org.cache2k.Cache2kBuilder.name(Cache2kBuilder.java:321)
        at com.bhohler.test.services.retrofit.SpotifyService.<clinit>(SpotifyService.java:38)
        ...

My cache instance is super basic:

public class SpotifyService {
  ...

  private static final Cache<String, Track> trackCache =
      new Cache2kBuilder<String, Track>() {}.name("trackCache")
          .eternal(true)
          .entryCapacity(100)
          .enableJmx(false)
          .build();

  ...

}

It looks like it's trying to initialize parts of JMX support even though the docs say it's supposed to be disabled by default? I'm not sure where to go from here.

For what it's worth, I'm testing on Android 9 (Pie) with a OnePlus 6T.

@cruftex

This comment has been minimized.

Copy link
Member

commented Feb 13, 2019

Many thanks for pointing me to the stackoverflow question.

The cache2k jar is pretty small and I therefore decided to give up to make lots of more tiny jars. That means the JMX support is included in the jar. That's documented here: #80

To make it work on Android again I provided proguard rules, that strip out the JMX support. See it in the documentation here:
https://cache2k.org/docs/latest/user-guide.html#android

Let me know whether that helps you and just shout if there are any further questions.

@bhohler

This comment has been minimized.

Copy link
Author

commented Feb 14, 2019

Thanks for the quick response!

I had proguard disabled when I initially created this issue but it's still not working even after enabling it and adding those rules. To rule out any 3rd party libraries causing issues I've created a minimal reproduction of the issue here:
https://github.com/bhohler/cache2k-android-test

Am I missing something obvious?

@bhohler

This comment has been minimized.

Copy link
Author

commented Feb 16, 2019

I got it working by adding these additional lines to my ProGuard rules:
https://github.com/bhohler/cache2k-android-test/blob/master/app/proguard-rules.pro#L26-L29

To actually use the ProGuard'd APK you have to either install it manually or change your run configuration in Android Studio from "Default APK" to "APK from app bundle". Just running it using "Default APK" wasn't actually applying the ProGuard rules for me.

And while this does actually work now, the build time for even that super basic project I linked in my last comment is 40+ seconds on my machine since it has to reapply the ProGuard rules every time. I'd love to use this library but that extra compile doesn't seem worth it.

@cruftex

This comment has been minimized.

Copy link
Member

commented Mar 8, 2019

@bhohler thanks for the feedback and providing the test code. I will look into that more. Maybe its a good idea to release a android version with proguard rules already applied.

@cruftex cruftex self-assigned this Aug 8, 2019

@cruftex cruftex added the bug label Aug 8, 2019

@cruftex cruftex added this to the v1.4 milestone Aug 8, 2019

@cruftex

This comment has been minimized.

Copy link
Member

commented Aug 8, 2019

Sorry to keep this dangling for a while. Since I am not an Android user its always a bit a hassle to double check what is going on in an Android environment.

Actually I am running an integration test with API level 16 on every commit. This test works without problem. The reason is that my build tools (subproject test-android) don't support service providers correctly and provide the contents of META-INF/services. Newer build tools seem to do that. That's why the troublesome code is not executed in my continuous Android test environment.

I added some code that catches the unavailable management and JNDI classes and then everything works fine on Android. Both things are optional and not needed for the core functionality.

Since this is only a few lines of code it doesn't make sense to provide a separate jar for Android.

@cruftex

This comment has been minimized.

Copy link
Member

commented Aug 8, 2019

Fix just released in v1.2.3.Final

@cruftex cruftex closed this Aug 8, 2019

@cruftex

This comment has been minimized.

Copy link
Member

commented Aug 8, 2019

Thanks @bhohler for the test project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.