Issue
Before migrating to androidX, the release build is working fine with every HTTP request.
After migrating to androidX, every GET request is working, but all of the POST request is not working. It can't send arguments to the server-side. All the content of the POST request is empty.
The only change I've done is migrate this project to androidX.
I'm not sure where I can look for more insight... :(
[update]: add interface and POJO by recommending from kushan comments :)
interface
@POST("/api/preference")
Call<RoasterPreference> updatePreference(@Body RoasterPreference roasterPreference);
POJO object
import com.google.gson.annotations.SerializedName;
public class RoasterPreference {
@SerializedName("type")
private String type;
@SerializedName("value")
private String value;
public RoasterPreference(String _type,String _value){
type = _type;
value = _value;
}
}
OkHttp log from debugging build:
D/OkHttp: --> POST http://192.168.0.1:9999/api/preference
Content-Type: application/json; charset=UTF-8
Content-Length: 29
{"type":"t_unit","value":"0"}
--> END POST (29-byte body)
OkHttp log from release build:
D/OkHttp: --> POST http://192.168.0.1:9999/api/preference
Content-Type: application/json; charset=UTF-8
Content-Length: 2
{}
--> END POST (2-byte body)
the debugger in debug mode
the debugger in release mode
diff in build.gradle when migrating to androidX
dependencies
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.exifinterface:exifinterface:1.0.0'
implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.fragment:fragment:1.1.0'
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
implementation 'androidx.core:core:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.github.HotBitmapGG:RingProgressBar:V1.2.3'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'org.apache.commons:commons-lang3:3.3.2'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.2.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.19.0'
// google services
implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Solution
when upgrading to Android Studio 3.4 later, R8 is defaulted on. So Gson library needs to add a new proguard rule.
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
ref:https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
Answered By - andyang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.