Android Schematic content provider library configuration?

帅比萌擦擦* 提交于 2019-12-03 07:50:40

问题


Jake Wharton mentioned this library in a recent talk and it looks like a great way to avoid a load of boilerplate so I gave it a go. But without any success. https://github.com/SimonVT/schematic

Below is the definition of the content provider with the annotation attached and the manifest provider element. The issue is that Android Studio does not like the provider definition because the content provider class does not extend ContentProvider.

Caused by: java.lang.ClassCastException: com.myapp.SchematicContentProvider
cannot be cast to android.content.ContentProvider

What am I missing? It could be related to android-apt which I am not using (Schematic recommends it but does not seem to require it) - when I try using android-apt I get a VerifyError so had to remove it from the build.

AndroidManifest.xml

    <provider
        android:name="com.myapp.SchematicContentProvider"
        android:authorities="com.myapp.provider"
        android:exported="false" />

And the class definition:

import net.simonvt.schematic.annotation.ContentProvider;
import net.simonvt.schematic.annotation.ContentUri;
import net.simonvt.schematic.annotation.TableEndpoint;

@ContentProvider(authority = SchematicContentProvider.AUTHORITY, database = SchematicDatabase.class)
public class SchematicContentProvider {

    public static final String AUTHORITY = "com.myapp.provider";

    interface Path {
        String ROUTES = "routes";
    }

    @TableEndpoint(table = SchematicDatabase.ROUTES) public static class Routes {

        @ContentUri(path = Path.ROUTES, type = "vnd.android.cursor.dir/list", defaultSort = SchematicRouteColumns.TITLE + " ASC")
        public static final Uri ROUTES = Uri.parse("content://" + AUTHORITY + "/" + Path.ROUTES );
    }

}

I've looked through the Schematic sample app (the code snippets in the readme are partial) but I can't see what I've missed. I'm not sure how to confirm that the code generation is working, how do I check? I looked under build but I only see BuildConfig under the Schematic package name.

It's a shame it's not working for me, it has great potential.


回答1:


You aren't declaring the right ContentProvider. You have to declare the generated one in the Manifest. I should like this :

<provider
    android:name=".yourOptionalPackage.generated.SchematicContentProvider"
    android:authorities="com.myapp.provider"
    android:exported="false" />

If your IDE (Android Studio/IntelliJ) shows red warning, just click on the Make Project button to generate the code.

If it's still not working, include apt-libs in your project (worth it), and to save even more time, use this awesome library also based on apt-libs ;)

Let me know in the comments if I solved your problem or not, and if you need help configuring your gradle file.




回答2:


You receive this error because com.myapp.SchematicContentProvider is your class with annotations and isn't a generated ContentProvider (which will have the same name).

Louis Cognault provided a correct answer, but it worth to mention that Schematic has a special parameter packageName for @ContentProvider and @Database annotations. packageName defines where generated classes will be placed. It let's you to clarify creation of AndroidManifest.xml.

Provider definition class:

@ContentProvider(
    authority = SchematicContentProvider.AUTHORITY, 
    database = SchematicDatabase.class,
    packageName = "com.myapp.providerpackage")
public class SchematicContentProvider {
    ...
}

Database definition class:

@Database(
        version = SchematicDatabase.VERSION,
        packageName = "com.myapp.providerpackage"
)
public class SchematicDatabase{
    public static final int VERSION = 1;
...
}

AndroidManifest.xml:

<provider
    android:name="com.myapp.providerpackage.SchematicContentProvider"
    android:authorities="com.myapp.provider"
    android:exported="false" />


来源:https://stackoverflow.com/questions/26120190/android-schematic-content-provider-library-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!