Rom*_*ain 20 android proguard retrofit retrofit2
我尝试使用Retrofit 2登录我的用户.(基本上是使用基本标题对登录URL进行GET)它运行良好但是一旦我ProGuard它,标题授权就不再发送了.(见日志输出)
示例代码:
用户模型:
public interface UserService {
@GET(GET_LOGIN)
Observable<User> login(@Header("Authorization") String basic);
}
Run Code Online (Sandbox Code Playgroud)
登录活动:
public void onClick(View v) {
mRetrofit.create(UserService.class)
.login(Credentials.basic(email, password))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(user -> {
UserHelper.save(LoginActivity.this, user);
}, throwable -> Dog.d);
}
Run Code Online (Sandbox Code Playgroud)
Proguard文件:
# Retrofit
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepclasseswithmembers class * {
@retrofit.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)
日志(proguard):
D/OkHttp: --> GET http://passport-supercairos.rhcloud.com/users/login HTTP/1.1
D/OkHttp: User-Agent: VirtualPassport-Client {Android-23} {Aquaris_E5}
D/OkHttp: Cache-Control: max-stale=10800
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP/1.1 401 Unauthorized (258ms)
D/OkHttp: Date: Fri, 19 Feb 2016 12:57:19 GMT
D/OkHttp: X-Powered-By: Express
D/OkHttp: WWW-Authenticate: Basic realm="Users"
D/OkHttp: Keep-Alive: timeout=15, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Content-Type: text/plain
D/OkHttp: OkHttp-Sent-Millis: 1455886639681
D/OkHttp: OkHttp-Received-Millis: 1455886639787
D/OkHttp: Unauthorized
D/OkHttp: <-- END HTTP (12-byte body)
Run Code Online (Sandbox Code Playgroud)
日志(非proguard):
D/OkHttp: --> GET http://passport-supercairos.rhcloud.com/users/login HTTP/1.1
D/OkHttp: User-Agent: VirtualPassport-Client {Android-23} {Aquaris_E5}
D/OkHttp: Cache-Control: max-stale=10800
D/OkHttp: Authorization: Basic ZG9yb2ZyanVAZ21haWwuY29tOmN2dnZ2dnY=
D/OkHttp: --> END GET
D/OkHttp: <-- HTTP/1.1 401 Unauthorized (258ms)
D/OkHttp: Date: Fri, 19 Feb 2016 12:57:19 GMT
D/OkHttp: X-Powered-By: Express
D/OkHttp: WWW-Authenticate: Basic realm="Users"
D/OkHttp: Keep-Alive: timeout=15, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Content-Type: text/plain
D/OkHttp: OkHttp-Sent-Millis: 1455886639681
D/OkHttp: OkHttp-Received-Millis: 1455886639787
D/OkHttp: Unauthorized
D/OkHttp: <-- END HTTP (12-byte body)
Run Code Online (Sandbox Code Playgroud)
完整代码可以在这里找到:https://github.com/supercairos/virtual-passport
Rom*_*ain 38
我终于成功了.这是关于Retrofit 2的proguard配置
# Retrofit
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
@retrofit2.* <methods>;
}
-keepclasseswithmembers interface * {
@retrofit2.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)
谢谢@ xudshen
UPDATE
主要问题:我使用了proguard-android-optimize所以我应该补充:
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)
我还切换回square提供的常规Retrofit 2 proguard配置:
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
Run Code Online (Sandbox Code Playgroud)
sil*_*udo 11
添加到@ Romain的答案你需要添加到proguard文件
-keepclasseswithmembers class * {
@retrofit2.http.* <methods>;
}
Run Code Online (Sandbox Code Playgroud)
如果您正在使用@Header,@ Query ...
mhK*_*ami 10
我终于找到了。如果您使用Gson,请尝试此操作,将其添加到改造 pro-guard 中:
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
Run Code Online (Sandbox Code Playgroud)
然后@SerializedName("name")在您的模型中使用。kotlin例子:
class PaymentRequestModel (
@SerializedName("name")
@Expose
var name : String = "",
}
Run Code Online (Sandbox Code Playgroud)
另一个简单的解决方案是使用支持注释中的@keep https://developer.android.com/reference/android/support/annotation/Keep.html
@Keep
interface APIService
{
@GET("/user/auth")
fun auth(@Header(Constants.AUTHORIZATION) authorization: String): Call<User>
}
Run Code Online (Sandbox Code Playgroud)
小智 5
对我来说使用注释@SerializedName
public class YourJsonClass{
@SerializedName("name") String username;
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11997 次 |
| 最近记录: |