Android Schematic content provider library configuration?
Asked Answered
M

2

7

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.

Mazurek answered 30/9, 2014 at 11:53 Comment(0)
R
6

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.

Residentiary answered 28/5, 2015 at 7:27 Comment(0)
R
4

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" />
Rattlesnake answered 4/6, 2015 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.