Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Schematic content provider library configuration?

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.

like image 564
Ollie C Avatar asked Sep 30 '14 11:09

Ollie C


1 Answers

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.

like image 106
Louis CAD Avatar answered Nov 15 '22 00:11

Louis CAD