改造网址未正确翻译

时间:2017-01-23 04:53:37

标签: android retrofit retrofit2 okhttp okhttp3

我正在使用Retrofit来调用API。我添加了一个拦截器来查看所请求的url。在调试版本中,url被正确转换,但签名版本导致url不能被翻译和解析。一些相关的代码如下:

改造服务:

@GET("/1.1/launch/next/{number}")
Observable<LaunchResponse> getNextXLaunches(@Path("number") int numberOfNextLaunches);

调用此方法时,我只需传入一个整数来确定要请求的启动次数。在签名apks中,上面产生以下URL:

..../1.1/launch/next/%7BlaunchNum%7D

而不是

..../1.1/launch/next/10

正如您所看到的,传递到路径中的值未正确转换,而{number}正在按字面解析。

在遇到类似问题后,我尝试了不同的版本:

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

我发现奇怪的是这个问题仅在运行签名版本时出现。可能导致此问题的原因是什么?有什么问题?

编辑:根据要求,以下是一些相关的预备规则:

# Retrofit 2.X
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

# OkHttp
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**

# OkHttp3
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

编辑:我已将minifyEnabled设置为false的版本构建为禁用ProGuard,并且它可以正常运行。因此,问题必须在于我的ProGuard配置,但我不确定它可能是什么。

1 个答案:

答案 0 :(得分:1)

我相信我已经解决了这个问题。更新相关的依赖项并将其缩小到我的ProGuard配置问题后,我将以下规则添加到proguard-rules.pro

# Retrofit 2.X
...
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

# Note: had already been added when updating to OkHttp 3.
# OkHttp3
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

此外,还没有保留几个本地文件。明确地包括规则中的那些或确保使用通配符来保留所有本地文件应该可以解决问题:

#local
-keep class example.** { *; }