Sem*_*cer 5 android android-studio retrofit retrofit2 android-jetpack-compose
我正在做一个实验项目,我在 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
}
Run Code Online (Sandbox Code Playgroud)
这是应用程序模块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'
}
Run Code Online (Sandbox Code Playgroud)
最后是我有问题的改造 api 服务接口:
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
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试调用此登录方法时,我都会得到:
java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将登录方法更改为非暂停传统版本时:
@GET("/login")
fun login(@Header("Authorization") credentials: String): Call<SnapshotResponse>
Run Code Online (Sandbox Code Playgroud)
然后一切正常。
这似乎与AS4.0的IR编译器有关。有人为此找到了一些解决方法吗?
编辑:我可能在 Retrofit 的跟踪器https://github.com/square/retrofit/issues/3233 和谷歌的跟踪器 https://issuetracker.google.com/issues/143468771上发现了相关问题
您可以在不启用 compose 的情况下创建一个模块,在其中放置所有 Retrofit 服务,App 模块(取决于您的“服务”模块是否启用了 Compose,只要您的“服务”模块没有它)就没有关系启用)
这个解决方法在 Android Studio 4.0 canary 7 中对我来说很好用。