Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

android - Build fails while generating signed apk

If I enable r8/proguard for debug version my application builds without any error. But if I was try to generate a signed apk then the build fails although I have added rules for proguard/r8 from okhttp and retrofit's github repo.

Gradle error log when I run it with --info

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:UsersacerDocumentsAndroidStudioProjectsClassManager-Kotlinappuildintermediatesproguard-filesproguard-android-optimize.txt-4.1.2:15:1-15: R8: Ignoring option: -optimizations
C:UsersacerDocumentsAndroidStudioProjectsClassManager-Kotlinappuildintermediatesproguard-filesproguard-android-optimize.txt-4.1.2:16:1-22: R8: Ignoring option: -optimizationpasses

C:Usersacer.gradlecachesransforms-2files-2.1f594407e73853045cddd2f89553be864jetified-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:Usersacer.gradlecachesransforms-2files-2.1f594407e73853045cddd2f89553be864jetified-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

My proguard-rules.pro file:

-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 file:

// 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
question from:https://stackoverflow.com/questions/65892228/build-fails-while-generating-signed-apk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This was an error in R8 - closely related to https://issuetracker.google.com/176381203. It should be fixed in version 2.2.53 of the R8 compiler. The fix is being rolled into studio and should appear no later than Studio 4.2 RC1. One can use version 2.2.53 by adding the following to the top-level build.gradle file:

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.
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...