我试图为新的 Paging 3 库模仿 Google 的 codelab,当我尝试让 Room DAO 方法返回 a 时遇到以下错误PagingSource:
D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:38: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByUserName(@org.jetbrains.annotations.NotNull()
^D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:43: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByNote(@org.jetbrains.annotations.NotNull()
Run Code Online (Sandbox Code Playgroud)
这是我的UsersDao.kt:
@Dao
interface UsersDao {
@Insert
fun insert(user: GithubUser): Completable
@Insert
fun insert(userList: List<GithubUser>): Completable
@Query("DELETE FROM userDb")
fun clearDb(): Completable
@Query("SELECT * FROM …Run Code Online (Sandbox Code Playgroud) android rx-java2 android-room android-paging android-paging-library
这可能是一个奇怪的问题,但是spJetpack Compose 中的定义与spXML 中的定义不同吗?我们使用的 XML 样式TextView定义如下:
<style name="Body">
<item name="android:textSize">12sp</item>
<item name="android:lineSpacingExtra">8dp</item>
<item name="android:letterSpacing">-0.03125</item>
</style>
Run Code Online (Sandbox Code Playgroud)
android:fontFamily我们还有一个在 XML 中定义的字体系列,并通过和应用于主题fontFamily:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/some_font_regular"
app:fontStyle="normal"
app:fontWeight="500" />
<font
app:font="@font/some_font_italic"
app:fontStyle="italic"
app:fontWeight="500" />
<font
app:font="@font/some_font_bold"
app:fontStyle="normal"
app:fontWeight="700" />
<font
app:font="@font/cera_pro_bold_italic"
app:fontStyle="italic"
app:fontWeight="700" />
</font-family>
Run Code Online (Sandbox Code Playgroud)
转到 Jetpack Compose,我们有一个数据类,其中包含TextStyles字体系列的定义,我们尝试在其中模仿 XML 中的定义:
<style name="Body">
<item name="android:textSize">12sp</item>
<item name="android:lineSpacingExtra">8dp</item>
<item name="android:letterSpacing">-0.03125</item>
</style>
Run Code Online (Sandbox Code Playgroud)
该数据类又在该对象中使用,该对象提供对版式、颜色等的访问:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:font="@font/some_font_regular"
app:fontStyle="normal"
app:fontWeight="500" /> …Run Code Online (Sandbox Code Playgroud) 我的应用程序面向 API 31/Android 12,根据Google 的说法,需要 WorkManager 2.7.0 版本,因此为了做到这一点,我已添加setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)到我的OneTimeWorkRequestBuilderAndroidManifest 中并添加了必要的更改(请参阅此链接了解详情)。然而,当我运行我的应用程序时,我遇到了这个错误:
java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Expedited WorkRequests require a ListenableWorker to provide an implementation for `getForegroundInfoAsync()`
Run Code Online (Sandbox Code Playgroud)
Google 没有提供关于如何为 RxWorker 执行此操作的示例或文档,但我在 Stackoverflow 中找到了这个答案,但它是针对协程的。
getForegroundInfoAsync我的问题是你如何实现何时必须返回的RxWorker说法——阅读文档似乎我必须将 Guava 添加到我的应用程序中才能做到这一点?由于文档说getForegroundInfoAsyncListenableFuture<ForegroundInfo>ListenableFutureAvoid implementing ListenableFuture from scratch. If you can't get by with the standard implementations, prefer to derive a new Future instance with the methods in Futures or, if necessary, to extend AbstractFuture.
因此,我们正要提交我们的应用程序进行审核,但我们被 Google 对针对 Android 13 及更高版本的应用程序的新要求阻止了,该要求基本上告诉开发人员AD_ID在其应用程序中披露权限的目的。问题是,我们的应用程序没有显式声明AD_ID权限,但事实证明某些 play-services 库或 Firebase 使用了它,因此它已合并到我们应用程序的清单中。
因此,我找到了一些解决方案,以便库声明的权限不会合并到应用程序的清单中,但我担心我们的应用程序依赖于 Firebase 的功能可能会停止工作。有人遇到过这种情况吗?
google-play firebase android-permissions android-tiramisu android-13
我一直在使用 StateFlow + 密封接口来表示 Android 应用程序中的各种 UI 状态。在我的 ViewModel 中,我有一个密封接口UiState来执行此操作,并且各种状态公开为StateFlow:
sealed interface UiState {
class LocationFound(val location: CurrentLocation) : UiState
object Loading : UiState
// ...
class Error(val message: String?) : UiState
}
@HiltViewModel
class MyViewModel @Inject constructor(private val getLocationUseCase: GetLocationUseCase): ViewModel() {
private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState
// ...
}
Run Code Online (Sandbox Code Playgroud)
然后在可组合项中,我以这种方式观察事件:
@Composable
fun MyScreen(
viewModel: HomeScreenViewModel,
onLocationFound: (CurrentLocation) -> Unit,
onSnackbarButtonClick: () -> Unit
) {
// ...
LaunchedEffect(true) { …Run Code Online (Sandbox Code Playgroud) android android-jetpack-navigation android-jetpack-compose kotlin-stateflow
我正在尝试将我的测试从 JUnit 4 迁移ViewModel到 JUnit 5,并结合使用 MockK。在 JUnit4 中,我在一个测试中使用了规则,即 RxJava2、LiveData 和 Coroutines 的规则,并且效果很好。我是这样使用它们的:
class CollectionListViewModelTest {
@get:Rule
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@get:Rule
val taskExecutorRule = InstantTaskExecutorRule()
@get:Rule
val rxSchedulerRule = RxSchedulerRule()
@ExperimentalCoroutinesApi
@get:Rule
val coroutineRule = MainCoroutineRule()
@Mock
lateinit var getAllCollectionsUseCase: GetAllCollectionsUseCase
private lateinit var SUT: CollectionListViewModel
@Before
fun setUp() {
SUT = CollectionListViewModel(getAllCollectionsUseCase)
}
...
}
Run Code Online (Sandbox Code Playgroud)
在尝试迁移到 JUnit5 时,我了解到规则现在是扩展,在搜索后我拼凑出了我之前使用的规则的替代品,并用 MockK 替换了 Mockito,我尝试以这种方式用扩展替换规则:
@ExperimentalCoroutinesApi
@Extensions(
ExtendWith(InstantExecutorExtension::class),
ExtendWith(MainCoroutineExtension::class),
ExtendWith(RxSchedulerExtension::class)
)
class CollectionListViewModelTest {
@MockK
lateinit var getAllCollectionsUseCase: GetAllCollectionsUseCase …Run Code Online (Sandbox Code Playgroud) android ×5
android-13 ×1
android-room ×1
android-xml ×1
firebase ×1
google-play ×1
junit4 ×1
junit5 ×1
kotlin ×1
mockk ×1
rx-java2 ×1