我正在使用AndroidFastNetworking库为我的一个项目消费restful Web服务。下面的代码是我用来消费api的代码。
private void getCampaigns(){
Util.showDialog(this);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.addInterceptor(new GzipRequestInterceptor())
.build();
AndroidNetworking.get(AppConstants.BASE_URL+"GetCampaignsInfo")
.setPriority(Priority.MEDIUM)
.setOkHttpClient(okHttpClient)
.build()
.getAsObject(Campaign.class, new ParsedRequestListener<Campaign>() {
@Override
public void onResponse(Campaign response) {
Log.d("TAG", "size:"+response.result.length);
// Log.d("TAG", "size:"+response.toString());
getViewModel().campaignsList.addAll(Arrays.asList(response.result));
Util.hideDialog();
}
@Override
public void onError(ANError error) {
error.printStackTrace();
if(error.getMessage().contains("SocketTimeoutException")){
Snackbar.make(getBinding().toolbar, error.getMessage(), Snackbar.LENGTH_LONG).show();
}
Util.hideDialog();
}
});
}
下面的代码在我的proguard-rules.pro 中
-dontwarn okio.**
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *; }
-keep interface com.squareup.okhttp3.* { *; }
-dontwarn javax.annotation.Nullable
-dontwarn javax.annotation.ParametersAreNonnullByDefault
-keep class com.fasterxml.jackson.databind.ObjectMapper {
public <methods>;
protected <methods>;
}
-keep class com.fasterxml.jackson.databind.ObjectWriter {
public ** writeValueAsString(**);
}
-keepnames class com.fasterxml.jackson.** { *; }
-dontwarn com.fasterxml.jackson.databind.**
-dontwarn okhttp3.internal.platform.*
-dontwarn retrofit2.Platform$Java8
-dontwarn javax.annotation.**
-keep class com.androidnetworking.** { *; }
#-keep class com.yourapp.objects.** { *; }
-keepattributes Signature, *Annotation*
-keepattributes *Annotation*,SourceFile,LineNumberTable
-keep class in.kdms.crm1100.campaigns.Campaign.** { *; }
我的成绩档案如下。
----------------
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
signingConfig signingConfigs.config
}
}
-------------------
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.github.stfalcon:androidmvvmhelper:0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.amitshekhar.android:jackson-android-networking:1.0.2'
// implementation 'com.afollestad.material-dialogs:core:0.9.4.7'
configurations.all {
exclude group: 'com.android.support', module: 'support-v13'
}
implementation 'com.afollestad.material-dialogs:core:0.9.4.7'
implementation(name: "truesdk-v0.7-releasePartner", ext: "aar")
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
}
这段代码在调试构建中非常有效。但当我在proguard应用后用发布版apk进行签名和测试时,logcat显示了以下崩溃。
2-04 16:15:57.374 22418-22418/in.kdms.crm1100 E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.kdms.crm1100, PID: 22418
java.lang.NullPointerException: Attempt to get length of null array
at in.kdms.crm1100.campaigns.CampaignsListActivity$1.a(CampaignsListActivity.java:73)
at in.kdms.crm1100.campaigns.CampaignsListActivity$1.onResponse(CampaignsListActivity.java:70)
at com.androidnetworking.common.ANRequest.deliverSuccessResponse(ANRequest.java:735)
at com.androidnetworking.common.ANRequest.access$6500(ANRequest.java:80)
at com.androidnetworking.common.ANRequest$6.run(ANRequest.java:709)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Campaign.java
public class Campaign {
public Result[] result;
public Subcampaigns[] subcampaigns;
public static class Result extends BaseObservable{
public String SubCampaign;
public String Status;
public String C1100SmeName;
public String CampaignName;
public String RtgSmeName;
public String Id;
public String Dialed;
public String RtgSlName;
public String C1100SlName;
public String DepartmentName;
@Bindable
public String getCampaignName() {
return CampaignName;
}
public void setCampaignName(String campaignName) {
CampaignName = campaignName;
// notifyPropertyChanged(BR.CampaignName);
}
public String getSubCampaign() {
return SubCampaign;
}
public void setSubCampaign(String subCampaign) {
SubCampaign = subCampaign;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getC1100SmeName() {
return C1100SmeName;
}
public void setC1100SmeName(String c1100SmeName) {
C1100SmeName = c1100SmeName;
}
public String getRtgSmeName() {
return RtgSmeName;
}
public void setRtgSmeName(String rtgSmeName) {
RtgSmeName = rtgSmeName;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getDialed() {
return Dialed;
}
public void setDialed(String dialed) {
Dialed = dialed;
}
public String getRtgSlName() {
return RtgSlName;
}
public void setRtgSlName(String rtgSlName) {
RtgSlName = rtgSlName;
}
public String getC1100SlName() {
return C1100SlName;
}
public void setC1100SlName(String c1100SlName) {
C1100SlName = c1100SlName;
}
@Bindable
public String getDepartmentName() {
return DepartmentName;
}
public void setDepartmentName(String departmentName) {
DepartmentName = departmentName;
// notifyPropertyChanged(BR.DepartmentName);
}
}
public static class Subcampaigns {
private String Id;
private String DisplayName;
}
}
所以基本上崩溃是getCampaigns()中onResponse()的第一行。我没有得到我应该在proguard文件中添加的内容来克服这个问题。请调查一下我的问题,让我知道我在流程中错过了什么。提前谢谢。
在proguard文件中添加以下两行代码后,错误得到解决。这可能会对一些正在为android开发proguard的人有所帮助。
-keep class in.kdms.crm1100.campaigns.Campaign**
-keepclassmembers class in.kdms.crm1100.campaigns.Campaign** {
*;
}