I am trying to create an Android Library containing a simple service. For example:
public class BasicService extends Service {
public BasicService() {
Log.d("I", "work");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
I start the service the following way:
startService(new Intent(this, BasicService.class));
The service is compiled into an AAR file and after adding that to an application I get a ClassNotFoundException.
Caused by: java.lang.ClassNotFoundException: Didn't find class "nl.company.example.library.Services.BasicService" on path: DexPathList[[zip file "/data/app/nl.example.aartest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
I don't need to bind a service as the application does not have to communicate with the service. I also don't need an aidl interface as I don't want to share the service with other apps. I know I need to declare the service at the application side (not library) so I did that the following way:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.company.example.exampleApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<application
android:settings... >
<service
android:name="nl.company.example.library.Services.BasicService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
This is the manifest of my library:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.company.example.library" >
<application/>
</manifest>
I've done some research at Stackoverflow/Google and I wasn't able to find an answer to my problem.
Android service in library
Also tried different ways of starting the service (on action etc.) but no good so far.
All help is welcome and my apologies if I made an evil duplicate question.
The BaseService was implementing an interface from another library which was not packaged with the aar
file. This caused the application to crash right after starting on a device, because it couldn't resolve the dependency.
public class BaseService implements OtherLibraryInterface {
...
}
Instead of throwing a ClassNotFoundException
for the OtherLibraryInterface
, Android would throw exactly that exception for the BaseService
. Which is very confusing and lead us to believe that there is something wrong with the Service.
Adding the OtherLibrary dependency explicitly as a dependency to the main application solved this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With