应用程序可以在调试模式下工作,但不能在发布模式下工作

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

我有一个应用程序,现在我要发布它。 我创建了一个 apk 文件并安装了它,但是当我向服务器发送登录请求时,它返回 null..

奇怪的是,当我使用调试版本启动应用程序时,它工作正常。 该问题仅发生在发布模式下。所以我在阅读这篇文章

后认为问题来自于proguard

这是我到目前为止所做的。

  1. 添加了所有需要的 proguard 规则。 (改造、Okhttp3、Glide)- 未工作。
  2. 在“构建”菜单中使用[构建 apk(s)] 创建一个 apk 并测试它 - 有效,但正在调试。

如有任何帮助,我们将不胜感激...

这是我的 build.gradle 文件。

defaultConfig {
    multiDexEnabled true       
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:design:$rootProject.supportLibraryVersion"
    implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:gridlayout-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:animated-vector-drawable:$rootProject.supportLibraryVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'       
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'     
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    implementation 'com.squareup:otto:1.3.8'
    implementation 'com.github.nisrulz:recyclerviewhelper:26.0.0'
    implementation 'jp.wasabeef:recyclerview-animators:2.2.7'
    implementation 'com.github.ittianyu:BottomNavigationViewEx:1.2.4'
    implementation 'com.blankj:utilcode:1.9.6'
    implementation 'com.github.bumptech.glide:glide:4.3.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
    implementation 'com.j256.ormlite:ormlite-core:5.0'
    implementation 'com.j256.ormlite:ormlite-android:5.0'
    implementation 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:1.1.3'
    implementation 'com.akexorcist:RoundCornerProgressBar:2.0.3'
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'
    implementation 'com.daimajia.easing:library:2.0@aar'
    implementation 'com.daimajia.androidanimations:library:2.3@aar'
    implementation files('libs/opencsv-3.8.jar')
    implementation('tk.zielony:carbon:0.15.0.1') {
        exclude group: 'com.android.support', module: 'animated-vector-drawable'
        exclude group: 'com.android.support', module: 'gridlayout-v7'
    }       
    implementation 'com.github.QuadFlask:colorpicker:0.0.13'
    implementation 'com.android.support:multidex:1.0.2'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.7.3'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
    implementation 'com.liulishuo.filedownloader:library:1.6.9'
    implementation 'org.jsoup:jsoup:1.11.2'
    implementation project(':typekit')
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    compile('com.crashlytics.sdk.android:crashlytics:2.9.0@aar') {
        transitive = true;
    }
}

这是我的 proguard-rules.pro 文件。

-dontwarn com.google.android.gms.common.GooglePlayServicesUtil

-ignorewarnings
-keep class * {
    public private *;
}    
# Retrofit   
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain service method parameters.
-keepclassmembernames,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement    
# OkHttp 3    
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.    

# Glide

-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
android proguard android-proguard
5个回答
3
投票

我犯了一个愚蠢的错误。

我错过了一条 proguard 规则。这是奥托巴士的。

-keepattributes *Annotation*
-keepclassmembers class ** {
    @com.squareup.otto.Subscribe public *;
    @com.squareup.otto.Produce public *;
}

我只搜索了github。没有关于 proguard 的部分。我在 Square 的博客上找到的。

如果您的代码似乎受到 proguard 的影响,请检查两次并进行更多搜索!


0
投票

我在 gradle.properties 中添加了以下行,之后工作顺利。

android.enableR8 = false

0
投票

对我有用的解决方案是, 只需在开发者设置中禁用和启用调试选项按钮即可。


0
投票

我有一个 API,在从启动屏幕导航到其他屏幕之前,其数据是必要的先决条件。这个 API 端点类似于

https://fakesite.com/pseudo-api/
。解决方案是删除最后的
/
。由于某种原因,
https://fakesite.com/pseudo-api/
无法从发布版本访问,但
https://fakesite.com/pseudo-api
可以。


0
投票

禁用 R8 是一个糟糕的解决方案,它会阻止应用程序代码优化、代码缩减和混淆

要解决这个问题,你必须添加 ProGuard 规则来保留一些类

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