添加 kotlin 协程和房间后无法进行发布构建,proguard 警告

Posted

技术标签:

【中文标题】添加 kotlin 协程和房间后无法进行发布构建,proguard 警告【英文标题】:Cannot make release build, proguard warnings after adding kotlin coroutines and room 【发布时间】:2019-09-24 16:18:33 【问题描述】:

在我添加了 kotlin 协程和房间后,我无法进行发布构建,请帮助我。

app/prguard-rules.pro

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the javascript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview 
#   public *;
#

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# Glide
-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.ImageHeaderParser$** 
  **[] $VALUES;
  public *;



 -keep class com.google.gson.**  *; 
  -dontwarn com.squareup.picasso.**
  -dontwarn com.squareup.okhttp.**
  -keep public class org.jsoup.**  public *; 


#dagger
  -dontwarn com.google.errorprone.annotations.**


  #facebook


  -keepclassmembers class * implements java.io.Serializable 
      private static final java.io.ObjectStreamField[] serialPersistentFields;
      private void writeObject(java.io.ObjectOutputStream);
      private void readObject(java.io.ObjectInputStream);
      java.lang.Object writeReplace();
      java.lang.Object readResolve();
  

  -keepnames class com.facebook.FacebookActivity
  -keepnames class com.facebook.CustomTabActivity

  -keep class com.facebook.login.Login


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

# 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



-dontwarn okio.**
# lifecycle

-keepattributes *Annotation*

-keepclassmembers enum android.arch.lifecycle.Lifecycle$Event 
    <fields>;


-keep class * implements android.arch.lifecycle.LifecycleObserver 


-keep class * implements android.arch.lifecycle.GeneratedAdapter 
    <init>(...);


-keepclassmembers class ** 
    @android.arch.lifecycle.OnLifecycleEvent *;




#crashlytics

-keep class com.crashlytics.**  *; 
-dontwarn com.crashlytics.**


## Google Play Services 4.3.23 specific rules ##
## https://developer.android.com/google/play-services/setup.html#Proguard ##

-keep class * extends java.util.ListResourceBundle 
    protected Object[][] getContents();


-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable 
    public static final *** NULL;


-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * 
    @com.google.android.gms.common.annotation.KeepName *;


-keepnames class * implements android.os.Parcelable 
    public static final ** CREATOR;


# Parceler library
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class *  *; 
-keep class **$$Parcelable  *; 


-keepclassmembers class fqcn.of.javascript.interface.for.webview 
   public *;


# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile


-keep class com.firebase.**  *; 
-keep class org.apache.**  *; 
-keepnames class com.fasterxml.jackson.**  *; 
-keepnames class javax.servlet.**  *; 
-keepnames class org.ietf.jgss.**  *; 
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**

# Only necessary if you downloaded the SDK jar directly instead of from maven.
-keep class com.shaded.fasterxml.jackson.**  *; 


-dontwarn kotlin.**

-keep public class com.google.android.gms.*  public *; 
-dontwarn com.google.android.gms.**

-keep class * extends java.util.ListResourceBundle 
    protected Object[][] getContents();


-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable 
    public static final *** NULL;


-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * 
    @com.google.android.gms.common.annotation.KeepName *;


-keepnames class * implements android.os.Parcelable 
    public static final ** CREATOR;


-dontwarn org.conscrypt.**

-dontwarn com.google.gson.Gson$6


#Appsee
-keep class com.appsee.**  *; 
-dontwarn com.appsee.**
-keep class android.support.**  *; 
-keep interface android.support.**  *; 
-keep class androidx.**  *; 
-keep interface androidx.**  *; 


#UXCam

-keep class com.uxcam.**  *; 
-dontwarn com.uxcam.**


# For CleverTap SDK
-dontwarn com.clevertap.android.sdk.**


# ServiceLoader support
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory 
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler 
-keepnames class kotlinx.coroutines.android.AndroidExceptionPreHandler 
-keepnames class kotlinx.coroutines.android.AndroidDispatcherFactory 

# Most of volatile fields are updated with AFU and should not be mangled
-keepclassmembernames class kotlinx.** 
    volatile <fields>;


-keep class com.google.android.gms.internal.**  *; 
-dontwarn com.google.android.gms.internal.zzhu


-dontwarn kotlin.**
-dontwarn kotlin.reflect.jvm.internal.**
-keep class kotlin.reflect.jvm.internal.**  *; 

-keep class kotlin.Metadata  *; 
-keepclassmembers public class com.mypackage.** 
    public synthetic <methods>;

-keepclassmembers class kotlin.Metadata 
    public <methods>;


-keepclassmembers class **$WhenMappings 
    <fields>;


-dontwarn android.arch.util.paging.CountedDataSource
-dontwarn android.arch.persistence.room.paging.LimitOffsetDataSource

-keep class com.facebook.stetho.**  *; 
-dontwarn com.facebook.stetho.**

我的 build.gradle 文件(app 模块)

dependencies 



    // if you are not using support library, include this instead
    implementation "com.google.dagger:dagger-android:$daggerVersion"

    kapt "com.google.dagger:dagger-compiler:$daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
    // Lifecyles, LiveData and ViewModel
    implementation 'android.arch.lifecycle:runtime:1.1.1'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    kapt 'android.arch.lifecycle:extensions:1.1.1'

    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

    // ReactiveX
    implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'

    // Retrofit2  converter
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

    // Retrofit2 and RxJava2
    implementation 'io.reactivex.rxjava2:rxjava:2.1.9'

    //    RxAndroid adds android specific bindings for RxJava,

    // Specifically AndroidSchedulers.mainThread() which provides a Scheduler

    // that schedules on main thread and can be used to switch between threads in Android.
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

    //    Retrofit 2 works seamlessly with RxJava 2 using the

    // RxJava 2 adapter for Retrofit 2, add the following dependency

    // to enable the RxJava 2 compatibility
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.5@aar') 
        transitive = true;
    


    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
    implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin+Coroutines

// Room components
    implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
    kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
    androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"

// Lifecycle components
    kapt "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"

// Coroutines
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

    implementation 'com.facebook.stetho:stetho:1.5.1'


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

kotlin 
    experimental 
        coroutines "enable"
    

与之前的版本相比,我更改了 firebase 版本,添加了 kotlin 协程并添加了空间:与之前的版本相比,我在代码中添加了这些:

apply plugin: 'kotlin-kapt'
 kapt "com.google.dagger:dagger-compiler:$daggerVersion"
 kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
 kapt 'android.arch.lifecycle:extensions:1.1.1'
 implementation 'com.google.firebase:firebase-core:16.0.8'(previous it was 
 16.0.5)
  implementation 'com.google.firebase:firebase-messaging:17.6.0'
 implementation 'com.google.android.gms:play-services-gcm:16.1.0'
    implementation 'com.google.firebase:firebase-dynamic-links:16.1.3'          implementation 'com.google.firebase:firebase-dynamic-links:16.1.8'
    implementation "com.google.firebase:firebase-invites:16.0.5"            implementation "com.google.firebase:firebase-invites:16.1.1"
    implementation 'com.google.firebase:firebase-perf:16.2.0'           implementation 'com.google.firebase:firebase-perf:16.2.5'

/ Room components
    implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
    kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
    androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
// Lifecycle components
    kapt "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
// Coroutines
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
    implementation 'com.facebook.stetho:stetho:1.5.1'

kotlin 
    experimental 
        coroutines "enable"
    

我在发布版本时收到以下警告:

Warning: there were 11 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes, you can suppress
         the warnings with '-dontwarn' options.

these are warnings in red in Message view :
  Warning: kotlinx.coroutines.flow.FlowKt__MergeKt: can't find referenced class kotlinx.atomicfu.AtomicBoolean

这些是其中的 11 个,我猜 proguard 只是在谈论 htese 警告,请帮忙

【问题讨论】:

你找到解决办法了吗? 【参考方案1】:

这是最常见的错误,因为“许多预编译的第三方库引用了其他未实际使用的库,因此不存在。这在调试版本中工作正常,但在发布版本中,ProGuard 需要所有库" ,因此它可以执行适当的静态分析。

所以,你可以忽略它或在 proguard-rules.pro 中写 -dontwarn

-dontwarn kotlinx.atomicfu.**

【讨论】:

谢谢,但同样的警告,现在也无法构建 我因此无法发布版本,请帮助【参考方案2】:

这对我有用,不知道错误的原因是什么,为什么 kotlin 团队会留下这样的错误:

-dontwarn kotlinx.**

【讨论】:

【参考方案3】:

写在pro-guard规则中

-忽略警告

【讨论】:

但它可能会产生更多问题,它会忽略所有可能有用的警告 我通过在 pro-guard 规则中添加这个解决了我的错误。现在我的应用程序已在 Play 商店中发布,到目前为止没有任何问题。 我建议你使用api而不是实现 ingore 警告不会解决错误但会抑制错误

以上是关于添加 kotlin 协程和房间后无法进行发布构建,proguard 警告的主要内容,如果未能解决你的问题,请参考以下文章

使用协程和 Flow 简化 API 设计

房间 allowMainThreadQueries 与 Kotlin 协程

使用协程和 Flow 简化 API 设计

python协程和异步IO——IO多路复用

kotlin协程

如何确保清除 ktor websocket 客户端创建的所有 Kotlin 协程?