vepzfe
vepzfe

Reputation: 4537

Why is not possible to declare an Intent Service as package private?

I'm working on a android library, so I wish to keep all the library code package private apart from a few classes which the library user needs to access.

Among these classes is an IntentService. However, the app crashes with this error :

java.lang.RuntimeException: Unable to instantiate service com.library.sdk.SaveDataIntentService: java.lang.IllegalAccessException: java.lang.Class<com.library.sdk.SaveDataIntentService> is not accessible from java.lang.Class<android.app.ActivityThread>
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3304)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6349)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

Caused by: java.lang.IllegalAccessException: java.lang.Class<com.library.sdk.SaveDataIntentService> is not accessible from java.lang.Class<android.app.ActivityThread>
    at java.lang.Class.newInstance(Native Method)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3301)
    at android.app.ActivityThread.-wrap5(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6349) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 

Even the manifest shows a warning because the intent service is not declared as a public class.

What exactly is causing this and why does the intent service need to be public?

Upvotes: 3

Views: 899

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007524

What exactly is causing this

A Java class outside of your package needs to create an instance of your IntentService.

why does the intent service need to be public?

Because a Java class outside of your package cannot create an instance of a package-private class, nor can it invoke a package-private constructor.

Upvotes: 4

Related Questions