gpt4 book ai didi

android - 如何在 AS 4.0 中使用 Retrofit 的挂起功能并启用组合?

转载 作者:行者123 更新时间:2023-11-29 22:38:40 25 4
gpt4 key购买 nike

我正在做一个实验项目,我在 Android Studio 4.0 中使用 jetpack compose 和 Retrofit 的协程支持。

这是我的顶级 gradle.build:

buildscript {
ext.kotlin_version = "1.3.61"
ext.compose_version = '0.1.0-dev03'

repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0-alpha06'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

}
}

allprojects {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
}

task clean (type: Delete) {
delete rootProject.buildDir
}

这是应用程序模块 build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
applicationId "com.example.test"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}

}


dependencies {
def room_version = "2.2.2"
def lifecycle_version = "2.2.0-rc03"

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'

implementation "androidx.compose:compose-runtime:$compose_version"
implementation "androidx.ui:ui-framework:$compose_version"
implementation "androidx.ui:ui-layout:$compose_version"
implementation "androidx.ui:ui-material:$compose_version"
implementation "androidx.ui:ui-foundation:$compose_version"
implementation "androidx.ui:ui-animation:$compose_version"
implementation "androidx.ui:ui-tooling:$compose_version"

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.fragment:fragment-ktx:1.2.0-rc03"

implementation "com.squareup.retrofit2:retrofit:2.6.2"
implementation "com.squareup.retrofit2:converter-moshi:2.4.0"
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
compileOnly 'org.glassfish:javax.annotation:10.0-b28'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

最后是我有问题的改造 api 服务接口(interface):

import com.example.test.data.response.SnapshotResponse
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Header

interface ApiService {
@GET("/login")
suspend fun login(@Header("Authorization") credentials: String): SnapshotResponse
}

每当我尝试调用此登录方法时,我都会得到:

java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.) 

但是当我尝试将我的登录方法更改为像这样的非暂停传统版本时:

@GET("/login")
fun login(@Header("Authorization") credentials: String): Call<SnapshotResponse>

然后一切正常。

这似乎与AS4.0的IR编译器有关。有人为此找到了一些解决方法吗?

编辑:我可能在 Retrofit 的跟踪器上发现了一个相关问题 https://github.com/square/retrofit/issues/3233和谷歌的追踪器 https://issuetracker.google.com/issues/143468771

最佳答案

您可以在放置所有 Retrofit 服务的地方创建一个不启用 compose 的模块,App 模块(取决于您的“服务”模块是否启用了 Compose 并不重要,只要您的“服务”模块没有启用)没有启用它)

此解决方法在 Android Studio 4.0 Canary 7 中对我来说工作正常。

关于android - 如何在 AS 4.0 中使用 Retrofit 的挂起功能并启用组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59277052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com