简体   繁体   中英

ClassNotFoundException when trying to put Android component into a library

I have two Android projects: an app and a library. The library uses StreamProvider and it is included as an AAR module in the app project. Here are important (changed by me) bits in both projects:

Library project:

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

provider/StreamProvider.java:

package au.com.a1ict.mylib.provider;

public class StreamProvider extends com.commonsware.cwac.provider.StreamProvider {
    public StreamProvider() {
    }
}

App project:

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/build.gradle: (name chosen by Android Studio when importing the lib):

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

settings.gradle:

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

Compilation & linking phase ends up well, just when the app is about to start I see in the logs:

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

It must be related to the fact the component (StreamProvider) is located in the AAR file because when used in one project, it works flawlessly (same namespace, packages, ... -- I literally just copy library project's files into app-debug ). Related SO questions mostly struggle with Eclipse. I'm really out of ideas here.

OK, this took one hell of an investigation but I have finally figured it out. It was actually a combination of several problems:

  1. AAR doesn't contain information about its dependencies; so I either have to include them manually or setup a maven repository.
  2. First I used maven-publish plugin for that only to realise it doesn't fill out AAR dependencies automatically.
  3. Then I used Gradle Android Maven plugin , much better but:
  4. Latest Gradle Android Maven plugin (1.5) requires Gradle 3.0+ while AS offers/recommends/sets 2.14.1 so I had to downgrade the plugin to 1.4.1
  5. Then I realised that actually I still have to include https://s3.amazonaws.com/repo.commonsware.com dependency url into the app project because POM files do not contain information about repositories, only packages.

After that I finally don't see any crashes. Btw, I didn't have to use

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

form (as suggested all over SO), specifying my dependency as:

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

was enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM