当尝试将Android组件放入库时,ClassNotFoundException异常



我有两个Android项目:一个应用程序和一个库。该库使用StreamProvider,并作为AAR模块包含在应用程序项目中。以下是两个项目中重要的部分(由我修改):

图书馆项目:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="au.com.a1ict.mylib">
...
<provider
    android:name=".provider.StreamProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="com.commonsware.cwac.provider.STREAM_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

build.gradle:

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.commonsware.cwac:provider:0.4.3'
}
提供者/StreamProvider.java:

package au.com.a1ict.mylib.provider;
public class StreamProvider extends com.commonsware.cwac.provider.StreamProvider {
    public StreamProvider() {
    }
}

应用项目:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="au.com.a1ict.myapplication">

app/build.gradle:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile project(':app-debug')
}

app-debug/构建。gradle:(Android Studio在导入lib时选择的名称):

configurations.maybeCreate("default")
artifacts.add("default", file('app-debug.aar'))

settings.gradle:

include ':app', ':app-debug'

编译,链接阶段结束得很好,就在应用程序即将启动时,我在日志中看到:

08-31 13:16:30.386 6656-6656/au.com.a1ict.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: au.com.a1ict.myapplication, PID: 6656
java.lang.RuntimeException: Unable to get provider au.com.a1ict.mylib.provider.StreamProvider: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[zip file "/data/app/au.com.a1ict.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at android.app.ActivityThread.installProvider(ActivityThread.java:5002)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4594)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4534)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[zip file "/data/app/au.com.a1ict.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.ActivityThread.installProvider(ActivityThread.java:4987)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4594) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4534) 
at android.app.ActivityThread.access$1500(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Suppressed: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-support-annotations-24.2.0_789c261ae08b7fa204bc7699aff21c5cfe4ba218-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_e6a045dc4d6d599e73e736c6e39e0dcf8f4490fd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_c96bb2006cae6009a151f1c7452e4a28bb4585dd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_a0d684a27f515a3bfd5243a957fad78a92e4fbcd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_369ab9945f1de9068baf3d6bc7fa4d034f59c368-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_2f9cbd5b7b19dc2598fd196e294d333d451c7a47-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.2.0_ec6f782152c4bb5e3b0b6840a634e809e68482eb-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-com.android.support-support-v4-24.2.0_3e07c7a20cb62485a70
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: exClass=java.lang.ClassNotFoundException
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: exMsg=Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[zip file "/data/app/au.com.a1ict.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: file=BaseDexClassLoader.java
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: class=dalvik.system.BaseDexClassLoader
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: method=findClass line=56
08-31 13:16:30.426 6656-6656/au.com.a1ict.myapplication D/Error: ERR: stack=java.lang.RuntimeException: Unable to get provider au.com.a1ict.mylib.provider.StreamProvider: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[zip file "/data/app/au.com.a1ict.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at android.app.ActivityThread.installProvider(ActivityThread.java:5002)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4594)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4534)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[zip file "/data/app/au.com.a1ict.myapplication-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.ActivityThread.installProvider(ActivityThread.java:4987)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4594) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4534) 
at android.app.ActivityThread.access$1500(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Suppressed: java.lang.ClassNotFoundException: Didn't find class "au.com.a1ict.mylib.provider.StreamProvider" on path: DexPathList[[dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-support-annotations-24.2.0_789c261ae08b7fa204bc7699aff21c5cfe4ba218-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_e6a045dc4d6d599e73e736c6e39e0dcf8f4490fd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_c96bb2006cae6009a151f1c7452e4a28bb4585dd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_a0d684a27f515a3bfd5243a957fad78a92e4fbcd-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_369ab9945f1de9068baf3d6bc7fa4d034f59c368-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-internal_impl-24.2.0_2f9cbd5b7b19dc2598fd196e294d333d451c7a47-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.2.0_ec6f782152c4bb5e3b0b6840a634e809e68482eb-classes.dex", dex file "/data/data/au.com.a1ict.myapplication/files/instant-run/dex/slice-com.android.support-support-v4-24.2.0_3e07c7a20cb62485a70a27026d0f6ed7d58d351e-classes.dex", dex file "/data/data/au.com.a1i

它必须与组件(StreamProvider)位于AAR文件中的事实有关,因为当在一个项目中使用时,它可以完美地工作(相同的命名空间,包,…)—我实际上只是复制库项目的文件到app-debug)。相关的SO问题大多与Eclipse有关。

好吧,这花了我一番功夫,但我终于弄明白了。它实际上是几个问题的组合:

  1. AAR不包含有关其依赖项的信息;所以我要么手动包含它们,要么设置一个maven仓库。
  2. 首先我使用maven-publish插件,只是为了意识到它不会自动填写AAR依赖项。然后我用Gradle Android Maven插件,好多了,但是:
  3. 最新的Gradle Android Maven插件(1.5)需要Gradle 3.0+,而AS提供/推荐/设置2.14.1,所以我不得不将插件降级到1.4.1
  4. 然后我意识到,实际上我仍然必须包括https://s3.amazonaws.com/repo.commonsware.com依赖url到应用程序项目,因为POM文件不包含有关存储库的信息,只有包。

在那之后我终于没有看到任何崩溃。顺便说一句,我没有必要使用

compile ('com.group:artifact:0.0.1@aar'){transitive=true}

形式(如上所述),指定我的依赖项为:

compile ('com.group:artifact:0.0.1')

就足够了。

相关内容

  • 没有找到相关文章

最新更新