Android 来自图书馆的外部服务(AAR,不是共享服务)

Android external Service from a library (AAR, not Shared Service)

我正在尝试创建一个包含简单服务的 Android 库。例如:

public class BasicService extends Service {
    public BasicService() {
        Log.d("I", "work");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

我按以下方式启动服务:

startService(new Intent(this, BasicService.class));

该服务被编译成一个 AAR 文件,将其添加到应用程序后我得到一个 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]]

我不需要绑定服务,因为应用程序不必与服务通信。我也不需要 aidl 界面,因为我不想与其他应用程序共享该服务。我知道我需要在应用程序端(而不是库)声明服务,所以我按照以下方式进行了:

<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>

这是我的图书馆清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.company.example.library" >
    <application/>
</manifest>

我在 Whosebug/Google 做了一些研究,但我找不到问题的答案。

Android service in library

也尝试了不同的启动服务的方式(在操作中等),但目前没有什么用。

欢迎所有帮助,如果我提出了一个邪恶的重复问题,我深表歉意。

BaseService 正在实现来自另一个库的接口,该库未与 aar 文件打包在一起。这导致应用程序在设备上启动后立即崩溃,因为它无法解析依赖关系。

public class BaseService implements OtherLibraryInterface {
    ...
}

不会为 OtherLibraryInterface 抛出 ClassNotFoundException,Android 会为 BaseService 抛出该异常。这非常令人困惑,让我们相信服务有问题。

将 OtherLibrary 依赖项显式添加为主应用程序的依赖项解决了这个问题。