androidandroid-paginggoogle-codelab

Android Paging 3 Codelabs failed on Room Paging


I am trying to follow this Paging 3 Codelab:https://developer.android.com/codelabs/android-paging, but when trying to compile at the end I am getting this error:

RepoDao.java:17: error: To use PagingSource, you must add `room-paging` artifact from Room as a dependency. androidx.room:room-paging:<version>
public abstract androidx.paging.PagingSource<java.lang.Integer, com.example.android.codelabs.paging.model.Repo> reposByName(@org.jetbrains.annotations.NotNull()

And also the end project has the same error. I can't see any difference between the code and mine.

RepoDao

@Dao
interface RepoDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun  insertAll(repos: List<Repo>)

    @Query("SELECT * FROM repos WHERE " +
            "name LIKE :queryString OR description LIKE :queryString " +
            "ORDER BY stars DESC, name ASC")
    fun reposByName(queryString: String): PagingSource<Int, Repo>

    @Query("DELETE FROM repos")
    suspend fun clearRepos()

}

GithubRepository

class GithubRepository(
    private val service: GithubService,
    private val database: RepoDatabase
) {

    fun getSearchResultStream(query: String): Flow<PagingData<Repo>> {
        Log.d("GithubRepository", "New query: $query")
        // appending '%' so we can allow other characters to be before and after the query string
        val dbQuery = "%${query.replace(' ', '%')}%"
        val pagingSourceFactory = { database.reposDao().reposByName(dbQuery) }


        @OptIn(ExperimentalPagingApi::class)
        return Pager(
            config = PagingConfig(
                pageSize = NETWORK_PAGE_SIZE,
                enablePlaceholders = false
            ),
            remoteMediator = GithubRemoteMediator(
                query,
                service,
                database
            ),
            pagingSourceFactory = pagingSourceFactory
        ).flow

    }

    companion object {
        const val NETWORK_PAGE_SIZE = 50
    }
}

build.gradle

roomVersion = '2.5.0'
pagingVersion = '3.1.1'


implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
implementation "androidx.paging:paging-runtime-ktx:$pagingVersion"
kapt "androidx.room:room-compiler:$roomVersion"

Also haven't seen any issues open about it. I tried changing versions of room and paging, restarting, invalidating cache, what I am doing wrong?


Solution

  • You are missing the room-paging library. Add it to your project by adding the following line to your app module's build.gradle file:

    implementation "androidx.room:room-paging:$roomVersion"