5

我正在做一个实验项目,我在 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 服务接口:

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上发现了一个相关问题

4

1 回答 1

5

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

这个解决方法在 Android Studio 4.0 canary 7 中对我来说很好用。

于 2019-12-27T10:30:56.910 回答