在我的 Android 应用程序中激活 ProGuard 时 API 无法工作

问题描述 投票:0回答:2

启用后

minifyEnabled=true
API 未向服务器发送数据。当我检查服务器时,我发现所有输入均为空。但是,如果我在“proguard-rules.pro”文件中添加
-dontobfuscate
,那么该应用程序就可以正常工作。我正在使用改造2。我尝试了几乎所有关于改造的 ProGuard 规则。没有任何效果!.

我的

build.gradle
文件:

def lifecycleExtensionVersion = '2.2.0'
def butterknifeVersion = '10.1.0'
def supportVersion = '29.0.0'
def retrofitVersion = '2.3.0'
def glideVersion = '4.9.0'
def rxJavaVersion = '2.1.1'
def daggerVersion = '2.14.1'
def mockitoVersion = '2.11.0'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'com.google.firebase:firebase-crashlytics:17.2.1'
    implementation 'com.google.firebase:firebase-messaging:20.2.4'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    implementation 'com.google.firebase:firebase-auth:19.3.0'

    implementation "com.android.support:design:$supportVersion"
    implementation "android.arch.lifecycle:extensions:$lifecycleExtensionVersion"

    implementation "com.jakewharton:butterknife:$butterknifeVersion"
    implementation 'androidx.work:work-runtime:2.2.0'
    annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"

    implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxJavaVersion"

    implementation "com.github.bumptech.glide:glide:$glideVersion"

    implementation "com.google.dagger:dagger:$daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$daggerVersion"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
    annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"

    implementation 'com.airbnb:deeplinkdispatch:4.1.0'
    annotationProcessor 'com.airbnb:deeplinkdispatch-processor:4.1.0'

    testImplementation "org.mockito:mockito-inline:$mockitoVersion"
    testImplementation "android.arch.core:core-testing:1.1.1"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

此外,当

minifyEnabled=true
时,当我调试时,以下“getLoginResult”函数未到达。但是当我添加
-dontobfuscate
时就达到了。

LoginViewModel.Java

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import io.reactivex.disposables.CompositeDisposable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginViewModel extends ViewModel {

    public MutableLiveData<LoginUser> loginUser = new MutableLiveData<>();
    public MutableLiveData<Boolean> loginUserLoadError = new MutableLiveData<>();
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();

    private CompositeDisposable disposable = new CompositeDisposable();

    public LiveData<ResponseBody> getLoginResult(String getEmail, String getPassword, String device_ip){
        loading.setValue(true);

        final MutableLiveData<ResponseBody> outputReponse = new MutableLiveData<>();

        LoginUser loginUser = new LoginUser(getEmail, getPassword, device_ip);

        Call<ResponseBody> call = LoginService
                .getInstance()
                .getLoginAPI()
                .loginUser(loginUser);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                outputReponse.setValue(response.body());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("Login Failed", t.getLocalizedMessage());
            }
        });

        return outputReponse;
    }

我在这里遗漏了什么或做错了什么?我怎样才能让它工作。

android retrofit2 proguard obfuscation
2个回答
3
投票

有两种方法可以解决这个问题:

  1. 为所有响应和请求 DTO 类插入注释

    Keep

  2. 修复以将您需要的所有课程保留在

    proguard-rules.pro
    文件中,如下所示:

-ignorewarnings
-keep class * {
    public private *;
}

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

-dontwarn org.xmlpull.v1.**
-dontnote org.xmlpull.v1.**
-keep class org.xmlpull.** { *; }

#retRofit
# 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
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
# Orm
-keep class * extends com.raizlabs.android.dbflow.config.DatabaseHolder { *; }

# Fresco

-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip
-keep @com.facebook.common.internal.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.common.internal.DoNotStrip *;
}
-keepclassmembers class * {
    native <methods>;
}
-dontwarn okio.**
-dontwarn com.squareup.okhttp.**
-dontwarn okhttp3.**
-dontwarn javax.annotation.**
-dontwarn com.android.volley.toolbox.**

# com.github.siyamed:android-shape-imageview
-dontwarn android.support.v7.**
-keepattributes *Annotation,Signature
-dontwarn com.github.siyamed.**
-keep class com.github.siyamed.shapeimageview.**{ *; }
-dontwarn org.xmlpull.v1.**
-dontwarn uk.co.senab.photoview.**

#Image Cropper
-keep class androidx.appcompat.widget.** { *; }



# Keep source file names, line numbers, and Parse class/method names for easier debugging
 -keepattributes SourceFile,LineNumberTable
 -keepnames class com.parse.** { *; }


 # Required for Parse
 -keepattributes *Annotation*
 -keepattributes Signature
 -dontwarn com.squareup.**
 -dontwarn okio.**


-keepattributes SourceFile,LineNumberTable
-keep class com.parse.*{ *; }
-dontwarn com.parse.**
-dontwarn com.squareup.picasso.**
-keepclasseswithmembernames class * {
    native <methods>;
}
```

0
投票

为了使 API 在使用 proguard 时正常工作,您可以将所有与 API 相关的类保留在一个单独的模块(例如“网络”)中,然后使用以下步骤:

  1. 在app uild.gradle中添加以下代码
    release {
        debuggable false
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  1. 将 API 相关文件保留在网络模块中,例如:

    ApiClient、ApiService、AuthInterceptor、ApiModels 等

  2. 在 proguard-rules.pro 中添加以下行:

-保留类 com.example.network.** { *; }

这不会混淆网络文件,因此可以保持模型和 API 通信应有的状态,并混淆所有其他文件。

© www.soinside.com 2019 - 2024. All rights reserved.