删除 table 时出现受限 API lint 错误。房间持久性

Restricted API lint error when deleting table. Room Persistence

所以我试图删除我的 Room Persistence table 中的所有项目,这显然导致我这样做:

@Dao 
interface MyDao {
    @Query("DELETE FROM myTableName")
    public void nukeTable();
}

然而,当 运行 ./gradlew lint 时,我遇到了如下所示的受限 API 故障。

Dao_Impl.java

1.My代码

@Dao
public interface AlertDao {

    @Insert
    void insert(List<Alert> alert);

    @Query("SELECT * FROM alert")
    List<Alert> queryAll();

    @Query("DELETE FROM alert")
    void deleteTable();
}
  1. 我的 lint 错误

Restricted API ../generated/source/apt/debug/com/app/test/storage/dao/AlertDao_Impl.java:>84: SharedSQLiteStatement.acquire can only be called from within the same >library group (groupId=android.arch.persistence.room) 81 82 @Override 83 public void deleteTable() { 84 final SupportSQLiteStatement _stmt = >__preparedStmtOfDeleteTable.acquire();
85 __db.beginTransaction(); 86 try { 87 _stmt.executeUpdateDelete();

../generated/source/apt/debug/com/app/test/storage/dao/AlertDao_Impl.java:91: SharedSQLiteStatement.release can only be called from within the same library group (groupId=android.arch.persistence.room) 88 __db.setTransactionSuccessful(); 89 } finally { 90 __db.endTransaction(); 91 __preparedStmtOfDeleteTable.release(_stmt);
92 } 93 }

../generated/source/apt/debug/com/app/test/storage/dao/AlertDao_Impl.java:98: RoomSQLiteQuery can only be called from within the same library group (groupId=android.arch.persistence.room) 95 @Override 96 public List queryAll() { 97 final String _sql = "SELECT * FROM alert"; 98 final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
99 final Cursor _cursor = __db.query(_statement); 100 try { 101 final int _cursorIndexOfAutoGeneratedKey = _cursor.getColumnIndexOrThrow("autoGeneratedKey");

../generated/source/apt/debug/com/app/test/storage/dao/AlertDao_Impl.java:134: RoomSQLiteQuery.release can only be called from within the same library group (groupId=android.arch.persistence.room) 131 return _result; 132 } finally { 133 _cursor.close(); 134 _statement.release();
135 } 136 } 137 }

如有任何帮助,我们将不胜感激。

此致

我在这里发现了一些类似的问题 Android Architecture Component Issue Tracker。根据问题,他们可能 ignorelinting 中生成代码。 Android 架构组件仍处于 alpha 所以他们正在努力修复它。

我在我的应用程序中使用了 deleteTable(),我的 DAO 看起来像

@Query("DELETE FROM tv_show")
void deleteTable();

它对我有用,没有给出任何 lint 错误。

D/TVShowViewModel: deleteTVShowTable: Delete

确保您使用的是架构组件gradle[=36=的最新版本 ] 以避免任何已修复的错误。在此处查找最新版本 Android Architecture Components Releases

您可以在 Android Studio 项目 gradle 文件

中暂时禁用 RestrictedApi 作为解决方法
android {
  lintOptions {
    disable 'RestrictedApi'
  }
}