web-dev-qa-db-ja.com

Proguardを使用してリソースの縮小と縮小を有効にすると、Retrofit bodyリクエストがRetrofitで空白になる

minifyEnabled & shrinkResourcestrueに設定すると、送信されるRetrofit本文JSONリクエストは空白になり、falseに設定すると、完全に正常に動作します。

私はgradleファイル全体を投稿しましたので、私が間違っていることを教えてくれるでしょう。

minifyEnabled & shrinkResourcestrueの場合、Retrofitリクエストのjson本文は次のようになります。

{}

minifyEnabled & shrinkResourcesfalseの場合、正常に機能します。

{"Data":"demoToken","Key":"demokey","Token":"2a9a8677-ac79-49d6-9947-d797b3e4d8e5"}

私のグラドルは次のようになります:

   apply {
    plugin 'com.Android.application'
    plugin 'kotlin-Android'
    plugin 'kotlin-Android-extensions'
    plugin 'kotlin-kapt'
    plugin 'io.fabric'
    plugin 'com.google.firebase.firebase-perf'
}
Android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.something.theapp"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 43 
        versionName "0.6.7"
        testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
        flavorDimensions "server"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }
    }
    signingConfigs {
        kaira {
            storeFile file('key_tts.jks')
            storePassword 'Android'
            keyAlias 'tts_key'
            keyPassword 'Android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.kaira
            minifyEnabled true
            shrinkResources true
            buildConfigField "boolean", "ALLOW_DATABASE", "false"
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
        release {
            signingConfig signingConfigs.kaira
            minifyEnabled true
            shrinkResources true
            buildConfigField "boolean", "ALLOW_DATABASE", "false"
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {
        local {
            //buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.136:6264/siteapp/api/\""
//            buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.21:5678/api/\""
            //   buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.233:5677/api/\""
            //  buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.26:5678/api/\""
            //buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.25:5678/api/\""
//            buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.37:5678/api/\""
//            buildConfigField "String", "WebServiceUrl", "\"https://api.forsell.in/api/\""
            //  buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.23:5678/api/\""
            buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.29:5678/api/\""
            buildConfigField "String", "WebUrl", "\"https://someurl.in/\""
            buildConfigField "String", "ServerName", "\"-Local\""
            dimension "server"
            copy {
                from "src/local"
                include "google-services.json"
                into "."
            }
        }
        temp {
            buildConfigField "String", "WebServiceUrl", "\"http://api.lezza.in/api/\""
            buildConfigField "String", "WebUrl", "\"https://someurl.in/\""
            buildConfigField "String", "ServerName", "\"-Local\""
            dimension "server"
            copy {
                from "src/local"
                include "google-services.json"
                into "."
            }
        }
        live {
            buildConfigField "String", "WebServiceUrl", "\"https://api.someurl.in/api/\""
            buildConfigField "String", "WebUrl", "\"https://someurl.in/\""
            buildConfigField "String", "ExtClientNameNew", "\"ExtClientName:TTS\""
            dimension "server"
            copy {
                from "src/livegcm"
                include "google-services.json"
                into "."
            }
        }
        staging {
            //buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.233:85/api/\""//sunil
            //     buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.233:5677/api/\""//devang
            //   buildConfigField "String", "WebServiceUrl", "\"http://192.168.1.233:85/api/\""
            buildConfigField "String", "WebServiceUrl", "\"https://stageapi.someurl.in/api/\""
            buildConfigField "String", "WebUrl", "\"https://stageweb.someurl.in/\""
            buildConfigField "String", "ServerName", "\"-Staging\""
            dimension "server"
            copy {
                from "src/local"
                include "google-services.json"
                into "."
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main.Java.srcDirs += 'src/main/kotlin'
    }
    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g" // 2g should be also OK
    }
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    lintOptions {
        abortOnError false
        disable 'MissingTranslation'
    }
}
repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.Android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'
            }
        }
    }
}
dependencies {
    def lifecycle_version = "2.0.0"
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation "Android.Arch.lifecycle:extensions:$lifecycle_version"
    annotationProcessor "Android.Arch.lifecycle:compiler:$lifecycle_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.0-alpha1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation('com.Android.support.test.espresso:espresso-core:3.0.2') {
        exclude group: 'com.google.code.findbugs'
    }
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.okhttp3:okhttp:3.12.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    implementation 'com.google.Android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.github.bumptech.glide:glide:4.5.0'
    implementation 'com.karumi:dexter:5.0.0'
    implementation 'com.google.Android.gms:play-services-maps:16.1.0'
    implementation 'com.google.Android.gms:play-services-location:16.0.0'
    implementation 'com.google.Android.gms:play-services-auth:16.0.1'
    implementation 'org.jetbrains.anko:anko-common:0.9'
    implementation 'com.google.Android.gms:play-services-ads:17.2.0'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation project(path: ':imagepicker')
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-inappmessaging-display:17.2.0'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
    implementation 'com.google.firebase:firebase-config:17.0.0'
    implementation 'com.google.firebase:firebase-perf:17.0.2'
    implementation 'com.github.chrisbanes:PhotoView:2.3.0'
    implementation 'com.google.Android.gms:play-services-places:16.1.0'
    implementation 'com.google.Android.libraries.places:places:1.1.0'
    api 'com.theartofdev.edmodo:Android-image-cropper:2.8.+'
    implementation 'com.github.faruktoptas:RetrofitRssConverterFactory:0.1.0'
    implementation 'com.wang.avi:library:2.1.3'
    implementation 'com.facebook.Android:facebook-Android-sdk:5.4.0'
    implementation('com.crashlytics.sdk.Android:crashlytics:2.9.8@aar') {
        transitive = true
    }
    implementation 'com.haozhang.libary:Android-slanted-textview:1.2'
    implementation 'com.facebook.shimmer:shimmer:0.4.0'
    implementation 'com.github.freshdesk:freshchat-Android:1.5.3'
    implementation 'commons-io:commons-io:2.4'
    implementation 'com.getkeepsafe.taptargetview:taptargetview:1.12.0'
    implementation 'com.razorpay:checkout:1.5.6'
    implementation 'com.google.Android.gms:play-services-analytics:17.0.0'

}
apply plugin: 'com.google.gms.google-services'

以下は私のメインモデルクラスです

class ServiceRequest {

    var Key: String? = "SiteAdminAppkey"
    var Token: String? = ""
    //    var Slug: String? = "admin"
    var Data: Any? = null
}

@SerializeNameアノテーションを追加することでこれを機能させることができますが、すべてのモデルクラスで行う必要があります。私の他のプロジェクトの1つでは、Proguardとすべてがシリアル化なしで機能します。

6
Salman Shaikh

Gsonを使用してJsonをモデルに変換しているので、これらのクラスでリフレクションが適切に機能していることを確認する必要があります(これらのカスタムアダプターを作成していないと思います)。

クラス名とフィールド名を保持する最も簡単な方法は、モデルに@KeepAndroidX Annotations から注釈を付けるだけです。 =:

ビルド時にコードが縮小されるときに、注釈付き要素を削除してはならないことを示します。これは通常、リフレクションを介してのみアクセスされるメソッドとクラスで使用されるため、コンパイラーはコードが未使用であると考える場合があります。

1
tynn

以下の行をproguardルールに追加します

-keepclassmembers class <yourpackagename>.** { <fields>; }

yourpackagenameを、私の場合のように、すべてのモデルクラスがあるディレクトリパスに置き換えます。

-keepclassmembers class com.demo.app.model.** { <fields>; }
0
Kailash Chouhan
  • 最新バージョンのレトロフィットlibを使用:com.squareup.retrofit2:retrofit:2.7.1 R8を使用している場合は、圧縮ルールと難読化ルールが自動的に含まれます。 最新の改造lib

  • また、Gson実装の変更 'com.google.code.gson:gson:2.8.6' 最新のGSON lib

  • R8の互換性に関するよくある質問 を確認してくださいG8のr8-互換性に関するよくある質問

  • OkHttp3を使用している場合は、最新バージョンokhttp3LibVersion = '4.3.1に変更してください。

0
YAGNESH SHINDE