首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Retrofit的挂起函数的AS 4.0和组合启用?

如何使用Retrofit的挂起函数的AS 4.0和组合启用?
EN

Stack Overflow用户
提问于 2019-12-10 23:56:43
回答 1查看 1.1K关注 0票数 5

我正在做一个实验项目,在这个项目中,我使用了AndroidStudio4.0中的jetpack和Retrofit的协同支持。

这是我的顶级gradle.build

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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服务接口:

代码语言:javascript
运行
复制
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
}

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

代码语言:javascript
运行
复制
java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., @GET, @POST, etc.) 

当我试图将登录方法更改为不挂起以下传统版本时,但是

代码语言:javascript
运行
复制
@GET("/login")
fun login(@Header("Authorization") credentials: String): Call<SnapshotResponse>

那一切都很好。

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

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

EN

回答 1

Stack Overflow用户

发布于 2019-12-27 10:30:56

您可以在不启用撰写功能的情况下创建一个模块,如果您将所有Retrofit服务放置在其中,那么App模块(依赖于您的“服务”模块已经启用了撰写功能,只要您的“服务”模块没有启用)并不重要)。

在AndroidStudio4.0金丝雀7中,这个解决方案对我来说很好。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59277052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档