小编Viv*_*odi的帖子

我们建议使用较新的 Android Gradle 插件来使用compileSdk = 33

我怎样才能更新到最新版本?我找不到最新的gradle-plugin

build.gradle(示例应用程序)

buildscript {
    ext {
        kotlin_version = '1.6.10'
        kotlin_serializtion_version = '1.5.21'
        kotlin_coroutines_version = "1.6.0"
        moshiVersion = "1.13.0"
        retrofit2_version = "2.9.0"
        okhttp3_version = "4.9.0"
        mockk_version = "1.12.2"
        picassoVersion = "2.71828"
        lifecycle_version = "2.5.0"
        koin_version = "3.2.0"
        barcode_scanner_version = "16.1.1"
        camerax_version = "1.0.0"
        camera_view_version = "1.0.0-alpha22"
        stripe_version = "19.1.0"
        jacoco_version = "0.8.7"
        room_version = "2.4.1"
        glide_version = "4.12.0"
        json_version = "20180813"
        kotlin_reflect_version = "1.6.10"
        compose_version = '1.2.0-rc03'
        espresso_version = '3.4.0'
        core_testing_version = '2.1.0'
        pdf_view_version = '3.2.0-beta.1'
        appboy_version = '21.0.0' …
Run Code Online (Sandbox Code Playgroud)

android gradle kotlin android-gradle-plugin kotlin-gradle-plugin

36
推荐指数
3
解决办法
12万
查看次数

Android Jetpack Compose 带索引的惰性列项目?

如何在 Jetpack Compose 中访问 LazyColumn 当前项目的索引。

LazyColumn {
  items(viewModel.list) { item ->
      // Here I want to get the index of the item
      Timber.d("item - $item")
  }
}
Run Code Online (Sandbox Code Playgroud)

android android-jetpack-compose android-jetpack-compose-list android-jetpack-compose-lazy-column

32
推荐指数
1
解决办法
2万
查看次数

检查 AAR 元数据值时发现的一个或多个问题:

嘿,我正在尝试运行我的应用程序,但收到此错误

构建.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 30

    configurations.all {
        resolutionStrategy { force 'androidx.core:core-ktx:1.7.0-alpha01' }
    }

    defaultConfig {

        applicationId "com.example.listadapter"
        minSdk 21
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+' …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-studio build.gradle android-gradle-plugin

30
推荐指数
2
解决办法
8万
查看次数

垂直可滚动组件的测量具有无限大的最大高度限制,这是不允许的

我在 recyclerview 项目布局中使用 ComposeView 来与 jetpack compose 一起使用。当我打开屏幕时遇到奇怪的问题

错误

java.lang.IllegalStateException: Vertically scrollable component was measured with an infinity maximum height constraints, which is disallowed. One of the common reasons is nesting layouts like LazyColumn and Column(Modifier.verticalScroll()). If you want to add a header before the list of items please add a header as a separate item() before the main items() inside the LazyColumn scope. There are could be other reasons for this to happen: your ComposeView was added into a …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-jetpack android-jetpack-compose lazycolumn

25
推荐指数
1
解决办法
3万
查看次数

Android kotlin 中 SharedFlow 的使用

嘿,我正在学习 kotlin 中的流程。我正在学习MutableStateFlowMutableSharedFlow。我尝试在现实世界的例子中学习MutableStateFlow。但我无法获得MutableSharedFlow示例,它更适合。我尝试了一些MutableStateFlow

例如,当我们从 api 获取数据时,我们可以使用 seal 类来进行相应的填充。

LoggedState.kt

sealed class LoggedState {
    data class OnSuccess(val data: List<XYZ>) : LoggedState()
    object OnEmpty : LoggedState()
    data class IsLoading(val isLoading: Boolean = true) : LoggedState()
    data class OnError(val message: String) : LoggedState()
} 
Run Code Online (Sandbox Code Playgroud)

设置ViewModel.kt

class SettingsViewModel : ViewModel() {

 var loggedMutableStateFlow = MutableStateFlow<LoggedState>(LoggedState.OnEmpty)

 fun fetchData(){
   val result = dataRepository.getLogged()
                result.handleResult(
                    onSuccess = { response ->
                        val data = response?.items
                        if (!data.isNullOrEmpty()) {
                            loggedMutableStateFlow.value …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-flow kotlin-stateflow kotlin-sharedflow

19
推荐指数
2
解决办法
1万
查看次数

无法在 kotlin 的惰性列中使用列表

我在项目中使用 LazyColumn。当我传递列表时,它给了我错误。有人可以指导我什么错误吗?

结果屏幕.kt

@Composable
fun ResultScreen(nearestResultList: List<NearestResult>?) {
    LazyColumn(
        Modifier
            .fillMaxSize()
            .background(getBackgroundColor())
    ) {
        items(nearestResultList) { nearestResult ->
            Text(text = "$nearestResult")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

Type mismatch.
Required:
Int
Found:
List<NearestResult>?
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

更新

在此输入图像描述

android kotlin android-jetpack android-jetpack-compose lazycolumn

13
推荐指数
2
解决办法
2942
查看次数

线程“测试工作人员”java.lang.IllegalStateException 中出现异常:带有主调度程序的模块未能初始化

嘿,我遇到了这种奇怪的问题。我不明白为什么这会在我的单元测试中引起。有人可以指导我我身边缺少什么吗?

Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:118)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:96)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:319)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14) .....
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试文件。我不明白为什么这会导致问题。

单元测试.kt

class XYZViewModelTest {

    @get:Rule
    val koinTestRule = KoinTestRule.create {
        modules(utilsModule)
    }

    @get:Rule
    val testInstantTaskExecutorRule: TestRule …
Run Code Online (Sandbox Code Playgroud)

android unit-testing kotlin mockk kotlin-coroutines

11
推荐指数
1
解决办法
8695
查看次数

仅限制 jetpack compose 中 TextField 中的数字

我只想在文本字段中输入数字。我尝试使用此stackoverflow逻辑来限制字母和特殊字符,但是当我按键盘上的点时,它会崩溃。

错误

2022-08-18 09:47:13.966 8050-8050/com.abc.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.abc.app.dev, PID: 8050
    java.lang.NumberFormatException: For input string: "."
        at java.lang.Integer.parseInt(Integer.java:733)
        at java.lang.Integer.parseInt(Integer.java:865)
        at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:187)
        at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:186)
        at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:266)
        at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:264)
        at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:241)
        at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:236)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion.onEditCommand(TextFieldDelegate.kt:198)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion.access$onEditCommand(TextFieldDelegate.kt:90)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:246)
        at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:243)
        at androidx.compose.ui.text.input.TextInputServiceAndroid$createInputConnection$1.onEditCommands(TextInputServiceAndroid.android.kt:111)
        at androidx.compose.ui.text.input.RecordingInputConnection.endBatchEditInternal(RecordingInputConnection.android.kt:162)
        at androidx.compose.ui.text.input.RecordingInputConnection.addEditCommandWithBatch(RecordingInputConnection.android.kt:136)
        at androidx.compose.ui.text.input.RecordingInputConnection.commitText(RecordingInputConnection.android.kt:181)
        at com.android.internal.inputmethod.RemoteInputConnectionImpl.lambda$commitText$16$com-android-internal-inputmethod-RemoteInputConnectionImpl(RemoteInputConnectionImpl.java:569)
        at com.android.internal.inputmethod.RemoteInputConnectionImpl$$ExternalSyntheticLambda34.run(Unknown Source:8)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Run Code Online (Sandbox Code Playgroud)

代码

appendTextFieldValue: (TextFieldValue) -> Unit,
Run Code Online (Sandbox Code Playgroud)

这是传递函数,下面是我的文本字段 …

android kotlin android-jetpack android-jetpack-compose android-compose-textfield

11
推荐指数
2
解决办法
1万
查看次数

java.lang.NoClassDefFoundError:com/android/tools/lint/client/api/Vendor

嘿,我正在将 kotlinx 协程更新到 Android 中的最新版本。我在 lint 检查中遇到奇怪的问题。我在我的项目中将版本更新为1.5.1to 1.6.0

构建.gradle(应用程序)

ext {
        kotlin_version = '1.5.21'
        kotlin_serializtion_version = '1.5.21'
        kotlin_coroutines_version = "1.6.0"
        retrofit_version = '2.5.0'
        moshiVersion = "1.12.0"
        retrofit2_version = "2.5.0"
        okhttp3_version = "4.9.0"
        mockk_version = "1.10.4"
        picassoVersion = "2.71828"
        lifecycle_version = "2.4.0"
        koin_version = "3.1.5"
        barcode_scanner_version = "16.1.1"
        camerax_version = "1.0.0"
        camera_view_version = "1.0.0-alpha22"
        stripe_version = "19.0.0"
        jacoco_version = "0.8.7"
        room_version = "2.3.0"
    }


dependencies {
        classpath 'com.android.tools.build:gradle:4.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1'
        classpath 'com.google.gms:google-services:4.3.3'
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_serializtion_version"
        classpath 'com.google.firebase:firebase-appdistribution-gradle:2.2.0' …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-coroutines

10
推荐指数
0
解决办法
2849
查看次数

垂直滚动影响喷气背包组合中的修改器重量

我希望我的按钮位于屏幕底部,使用Column. 当我尝试使用较小的设备时,我的内容不可滚动。所以我在堆栈溢出中搜索并找到了这个答案。当我添加时

.verticalScroll(rememberScrollState())
Run Code Online (Sandbox Code Playgroud)

在我的修改器中,它打破了间隔重量的东西并将我的按钮放在顶部。

输入

@可组合

fun Input() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .padding(10.dp)
    ) {
        Item()
    }
}
Run Code Online (Sandbox Code Playgroud)

物品

@Composable
fun Item {
    Image()
    Text() // more item here
    Pressure()
}
Run Code Online (Sandbox Code Playgroud)

压力

@Composable
fun Pressure() {
      var value by rememberSaveable(stateSaver = TextFieldValue.Saver) {
         mutableStateOf(TextFieldValue())
      }
    Column {
        Text(value)
        Image()
        // more item here
        Spacer(modifier = Modifier.weight(1f))
        OnSubmit(value)
    }
}
Run Code Online (Sandbox Code Playgroud)

实际产量

在此输入图像描述

预期输出

在此输入图像描述

我希望我的按钮在较小的设备中位于底部并垂直滚动。

更新

@SemicolonSpace 之后,我尝试了代码,但我的按钮位于屏幕后面

fun Input() {
        Column(
            modifier = Modifier.fillMaxHeight(),
            verticalArrangement …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-jetpack android-jetpack-compose

9
推荐指数
1
解决办法
3959
查看次数