Android Schematic内容提供程序库配置?

Oll*_*e C 6 android android-contentprovider

杰克沃顿在最近的一次演讲中提到了这个图书馆,它看起来是一个很好的方法来避免一大堆样板,所以我试了一下.但没有任何成功. https://github.com/SimonVT/schematic

下面是附加了注释的内容提供程序的定义以及清单提供程序元素.问题是Android Studio不喜欢提供程序定义,因为内容提供程序类不扩展ContentProvider.

Caused by: java.lang.ClassCastException: com.myapp.SchematicContentProvider
cannot be cast to android.content.ContentProvider
Run Code Online (Sandbox Code Playgroud)

我错过了什么?它可能与我没有使用的android-apt有关(Schematic推荐它但似乎并不需要它) - 当我尝试使用android-apt时,我得到一个VerifyError,所以不得不从构建中删除它.

AndroidManifest.xml中

    <provider
        android:name="com.myapp.SchematicContentProvider"
        android:authorities="com.myapp.provider"
        android:exported="false" />
Run Code Online (Sandbox Code Playgroud)

和类定义:

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 );
    }

}
Run Code Online (Sandbox Code Playgroud)

我查看了原理图示例应用程序(自述文件中的代码片段是部分的),但我看不出我错过了什么.我不确定如何确认代码生成是否正常,我该如何检查?我在构建下查看,但我只在Schematic包名下看到BuildConfig.

遗憾的是它不适合我,它有很大的潜力.

Lou*_*CAD 5

您没有声明正确的 ContentProvider。您必须在 Manifest 中声明生成的一个。我应该喜欢这个:

<provider
    android:name=".yourOptionalPackage.generated.SchematicContentProvider"
    android:authorities="com.myapp.provider"
    android:exported="false" />
Run Code Online (Sandbox Code Playgroud)

如果您的 IDE (Android Studio/IntelliJ) 显示红色警告,只需单击 Make Project 按钮即可生成代码。

如果它仍然不起作用,请在您的项目中包含 apt-libs(值得),为了节省更多时间,请使用这个同样基于 apt-libs 的很棒的库;)

如果我解决了您的问题,以及您是否需要配置 gradle 文件的帮助,请在评论中告诉我。