生成签名的 apk 时构建失败

时间:2021-01-25 20:48:52

标签: android android-studio gradle

如果我为调试版本启用 r8/proguard,我的应用程序构建时不会出现任何错误。但是,如果我尝试生成签名的 apk,则构建失败,尽管我从 okhttp 和改造的 github 存储库中添加了 proguard/r8 的规则。

使用 --info 运行时的 Gradle 错误日志

R8 is a new Android code shrinker. If you experience any issues, please file a bug at
https://issuetracker.google.com, using 'Shrinker (R8)' as component name. You can
disable R8 by updating gradle.properties with 'android.enableR8=false'.
Current version is: 2.1.75 (build 8142868c18707f172934d5343ba33dc36cff56ad from go/r8bot (luci-r8-custom-ci-xenial-1-nxk4)).

C:\Users\acer\Documents\AndroidStudioProjects\ClassManager-Kotlin\app\build\intermediates\proguard-files\proguard-android-optimize.txt-4.1.2:15:1-15: R8: Ignoring option: -optimizations
C:\Users\acer\Documents\AndroidStudioProjects\ClassManager-Kotlin\app\build\intermediates\proguard-files\proguard-android-optimize.txt-4.1.2:16:1-22: R8: Ignoring option: -optimizationpasses

C:\Users\acer\.gradle\caches\transforms-2\files-2.1\f594407e73853045cddd2f89553be864\jetified-okhttp-4.7.2.jar: R8: Type `org.conscrypt.Conscrypt` was not found, it is required for default or static interface methods desugaring of `Lokhttp3/internal/platform/ConscryptPlatform$Companion;atLeastVersion(III)Z`

> Task :app:minifyReleaseWithR8
C:\Users\acer\.gradle\caches\transforms-2\files-2.1\f594407e73853045cddd2f89553be864\jetified-okhttp-4.7.2.jar: R8: Type `org.conscrypt.ConscryptHostnameVerifier` was not found, it is required for default or static interface methods desugaring of `okhttp3.internal.platform.ConscryptPlatform$platformTrustManager$2`
AGPBI: {"kind":"error","text":"com.android.tools.r8.errors.b: Undefined value encountered during compilation. This is typically caused by invalid dex input that uses a register that is not defined on all control-flow paths leading to the use.","sources":[{}],"tool":"R8"}
Task :app:minifyReleaseWithR8 in app Finished

> Task :app:minifyReleaseWithR8 FAILED
:app:minifyReleaseWithR8 (Thread[Daemon worker Thread 6,5,main]) completed. Took 23.724 secs.
AAPT2 aapt2-4.1.2-6503028-windows Daemon #0: shutdown

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:minifyReleaseWithR8'.
> com.android.tools.r8.CompilationFailedException: Compilation failed to complete

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 33s
29 actionable tasks: 5 executed, 24 up-to-date

我的 proguard-rules.pro 文件:

-dontobfuscate
-keep public class bd.edu.daffodilvarsity.classmanager.common.models.* { *; }

# Rules for OkHttp library

# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn org.conscrypt.**
-dontwarn okhttp3.internal.platform.**



# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod

# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions

# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>

build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.4.21'
    repositories {
        google()
        jcenter()
        maven {
            url "http://storage.googleapis.com/r8-releases/raw/master"
        }
    }
    dependencies {
        classpath 'com.android.tools:r8:2.1.75'
        classpath 'com.android.tools.build:gradle:4.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.4'
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://www.jitpack.io" }

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle-wrapper.properties

#Mon Jan 25 03:03:02 BDT 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

1 个答案:

答案 0 :(得分:2)

这是 R8 中的一个错误 - 与 https://issuetracker.google.com/176381203 密切相关。它应该在 R8 编译器的 2.2.53 版中修复。修复程序正在进入工作室,应该不迟于 Studio 4.2 RC1 出现。可以通过在顶级 build.gradle 文件中添加以下内容来使用 2.2.53 版本:

buildscript {

    repositories {
        maven {
            url 'https://storage.googleapis.com/r8-releases/raw'
        }
    }

    dependencies {
        classpath 'com.android.tools:r8:2.2.53'          // Must be before the Gradle Plugin for Android.
        classpath 'com.android.tools.build:gradle:X.Y.Z' // Your current AGP version.
     }
}