8

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.

Community
  • 1
  • 1
jobbert
  • 3,297
  • 27
  • 43
  • 1
    There is a mismatch between your classname `BasicService` and the service you actually start `SimpleService`. Might that be the issue? Just tried this in a test repo and seems to be working fine. I don't declare the service in the manifest of my library, but I do declare it in the manifest of my application. I start the service in exactly the same way as you do and include the library via a local aar. – Jeroen Mols Aug 13 '15 at 06:39
  • Hello jmols, sorry this was a mistake with making the issue anonymous. Also added the libraries manifest. Hope you are able to help:) – jobbert Aug 13 '15 at 07:46
  • Your code seems correct to me. Autocomplete is working right? (That way we're sure the dependency is included correctly.) What version of the Gradle plugin are you using? I'm using `com.android.tools.build:gradle:1.3.0`. – Jeroen Mols Aug 13 '15 at 07:53
  • Autocomplete also working right that's why it's driving me nuts. And yes also using gradle version 1.3.0. – jobbert Aug 13 '15 at 07:55
  • How do you include the `aar` file in your project? In what folder/repository is it stored? – Jeroen Mols Aug 13 '15 at 07:59
  • I use IDE android studio. File->Project structure->New module(alt-insert)->import JAR/AAR package-> select correct AAR. Then I press module app and add the AAR to the dependencies. It will be added in the gradle of the app in the dependencies like this: compile project(':mybeamlibrary-debug'). Then it is placed at the main folder of the project. – jobbert Aug 13 '15 at 08:09
  • Tried to do the same here and everything is working like it should... If you upload a full project I can take a look? – Jeroen Mols Aug 13 '15 at 08:36
  • https://www.dropbox.com/s/j0yfc53dnu8z4e2/TestOfAar.7z?dl=0 – jobbert Aug 13 '15 at 08:43
  • When I include your aar file into my project I get the same error, so I'm guessing its a library issue. Can you try adding a complete element to your manifest? (containing theme and label) Also your `R.txt` seems to contain many elements of the support library (which are not included in your app), can you clean your project and build a new `aar`? – Jeroen Mols Aug 13 '15 at 09:13
  • If I add theme I get duplicate theme exceptions: Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : Attribute application@theme value=(@style/AppTheme) from AndroidManifest.xml:9:9-40 is also present at [TestOfAar:mybeamlibrary-debug:unspecified] AndroidManifest.xml:13:9-47 value=(@style/Theme.AppCompat) Suggestion: add 'tools:replace="android:theme"' to element at AndroidManifest.xml:5:5-22:19 to override – jobbert Aug 13 '15 at 09:28
  • Both done but doesn't work unfortunately:( – jobbert Aug 13 '15 at 09:30
  • What is in the R.txt file if you uncompress the new `aar` file? Are there still references to the support lib? – Jeroen Mols Aug 13 '15 at 09:34
  • Yes there still are, do I need to remove support lib from my lib? – jobbert Aug 13 '15 at 09:40
  • The reason why I think this might be causing problems is because the file defines resources which are not packaged with the app. Maybe this causes a compilation issue of some sort? (my library doesn't include the support lib) – Jeroen Mols Aug 13 '15 at 09:44
  • Even moving all classes to new library project results in the same.. – jobbert Aug 13 '15 at 09:44
  • Moving to the main app works, right? And if you include the module iso the `aar`? – Jeroen Mols Aug 13 '15 at 09:48
  • What do you mean with "Moving to the main app works, right? And if you include the module iso the aar?". I succesfully removed the support lib but doesn't work either. – jobbert Aug 13 '15 at 09:59
  • I can share the library with you if you want? – jobbert Aug 13 '15 at 10:01
  • I mean merging your library with your main app, just to verify that the code actually works. What I would recommend is that you create a completely new project, with a library and an app. And then create the simplest prototype possible, just to get a similar setup like me working. Then you can re-add all your code from there. makes sense? – Jeroen Mols Aug 13 '15 at 10:02
  • I am sorry for late response. Base seems to work I'll let you know more in secs.. – jobbert Aug 13 '15 at 11:30
  • Really curious as to what the issue actually is :) – Jeroen Mols Aug 13 '15 at 11:45
  • First I thought it would be because I am missing empty constructor, still working on it. More info asap. – jobbert Aug 13 '15 at 12:02
  • The problem is solved! The problem is using an interface from a sub-library dependency. Added that dependency to the application and it is working now. If I use a interface from a sub library it will just show class not found of the service while it is actually the interface that's not found. If I used an external class of the sub library the message suddenly was clear. Thanks for all your help! Please post this as answer and I will be happy to accept;) – jobbert Aug 13 '15 at 12:43
  • Great, glad it is working for you now! Also very useful to know that `ClassNotFoundExceptions` work the way they do. (and can mask other interfaces) – Jeroen Mols Aug 13 '15 at 13:19

1 Answers1

8

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.

Jeroen Mols
  • 3,436
  • 17
  • 24