【问题标题】:Failed to find byte code for Volley when using it in an Android library AAR在 Android 库 AAR 中使用 Volley 时找不到字节码
【发布时间】:2018-09-19 18:58:22
【问题描述】:

我在一个已构建到 AAR 文件中的库项目中使用 Volley。

我将 AAR 文件添加到主项目中。在构建主项目时,出现以下错误:

Failed to find byte code for com/android/volley/Response$Listener

我猜它与 Proguard 配置有关,但是即使我在创建 AAR 时构建了调试变体,我也会收到错误。

这是我的库项目的 Proguard 文件:

# Volley
-dontwarn com.android.volley.**
-dontwarn com.android.volley.error.**
-keep class com.android.volley.** { *; }
-keep class com.android.volley.toolbox.** { *; }
-keep class com.android.volley.Response$* { *; }
-keep class com.android.volley.Request$* { *; }
-keep class com.android.volley.RequestQueue$* { *; }
-keep class com.android.volley.toolbox.HurlStack$* { *; }
-keep class com.android.volley.toolbox.ImageLoader$* { *; }
-keep interface com.android.volley.** { *; }
-keep class org.apache.commons.logging.*

关于可能导致此问题的任何提示?

更新:关于consumerProguardFiles Proguard 文件,我已经尝试过https://stackoverflow.com/a/27052696/1020311,但在构建主项目时仍然遇到同样的错误。

我还尝试了默认的 proguard-library.pro 文件以及我的 Volley 行:

#
# This ProGuard configuration file illustrates how to process a program
# library, such that it remains usable as a library.
# Usage:
#     java -jar proguard.jar @library.pro
#

# Specify the input jars, output jars, and library jars.
# In this case, the input jar is the program library that we want to process.

# -injars  in.jar
# -outjars out.jar

# -libraryjars  <java.home>/lib/rt.jar

# Save the obfuscation mapping to a file, so we can de-obfuscate any stack
# traces later on. Keep a fixed source file attribute and all line number
# tables to get line numbers in the stack traces.
# You can comment this out if you're not interested in stack traces.

#-printmapping out.map
-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,EnclosingMethod

# Preserve all annotations.

-keepattributes *Annotation*

# Preserve all public classes, and their public and protected fields and
# methods.

-keep public class * {
    public protected *;
}

# Preserve all .class method names.

-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames class * {
    native <methods>;
}

# Preserve the special static methods that are required in all enumeration
# classes.

-keepclassmembers class * extends java.lang.Enum {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your library doesn't use serialization.
# If your code contains serializable classes that have to be backward
# compatible, please refer to the manual.

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    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();
}

# Your library may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName:

# -keep public class mypackage.MyClass
# -keep public interface mypackage.MyInterface
# -keep public class * implements mypackage.MyInterface

# Volley
-dontwarn com.android.volley.**
-dontwarn com.android.volley.error.**
-keep class com.android.volley.** { *; }
-keep class com.android.volley.toolbox.** { *; }
-keep class com.android.volley.Response$* { *; }
-keep class com.android.volley.Request$* { *; }
-keep class com.android.volley.RequestQueue$* { *; }
-keep class com.android.volley.toolbox.HurlStack$* { *; }
-keep class com.android.volley.toolbox.ImageLoader$* { *; }
-keep interface com.android.volley.** { *; }
-keep class org.apache.commons.logging.*

此外,我尝试在 buildTypes 中注释掉 Proguard 行,因为该模块将是开源的,我不需要混淆,但仍然没有运气。

这是 build.gradle 文件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'proguard-library.pro'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'proguard-library.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.volley:volley:1.1.1'
}

我会不会弄错 AAR 文件?

谢谢!

【问题讨论】:

  • 你能发布你的 gradle 文件吗?
  • @FcoP。已添加,谢谢
  • 这是proguard文件,我是说build.gradle文件
  • @FcoP。对不起,误读了您的评论。它在那里。谢谢
  • 奇怪,我用你的文件做了一个测试项目,它仍然可以编译,我将发布和调试都设置为使用 proguard。你可以检查吗? github.com/fcopardo/stackOverflowQuestion1

标签: android android-volley proguard android-proguard


【解决方案1】:

未能找到 com/android/volley/Response$Listener

的字节码

我关心的事情是这样的:

consumerProguardFiles 'proguard-library.pro'

您在 releasedefaultConfig 块中都使用了这行代码,我相信这会导致问题。

Check the comment:

consumerProguardFiles 应该在 defaultConfig 中指定 而不是 buildTypes/release 以便它在消费应用程序的情况下工作 调试和发布模式下的 proguards(例如,避免 65k dex 方法限制)

PS: Progaurd 规则似乎不是问题。至少,您可以尝试更新 Appcompat 然后测试项目,以及我创建了一个测试项目,它可以与您当前的代码一起使用。

【讨论】:

【解决方案2】:

在测试规则时最好将-dontwarn 注释掉并添加-verbose(在顶部)。

...所需的规则可能是:

-keep,includedescriptorclasses class com.android.volley.** { *; }

或带有两个**(同样适用于所有其他条目,带有一个*):

-keep class com.android.volley.Response$** { *; }

它甚至可以明确地拼写出来,类似:

-keep class com.android.volley.Response$Listener { *; }

大概是这样的:

-keepattributes *Annotation*

-keep class com.android.volley.** { *; }
-keep class com.android.volley.toolbox.** { *; }
-keep class com.android.volley.Response$** { *; }
-keep class com.android.volley.Request$** { *; }
-keep class com.android.volley.RequestQueue$** { *; }
-keep class com.android.volley.toolbox.HurlStack$** { *; }
-keep class com.android.volley.toolbox.ImageLoader$** { *; }
#-dontwarn com.android.volley.error.**
#-dontwarn com.android.volley.**

# not sure if this one is required
#-keep interface com.android.volley.** { *; }

-keep class org.apache.commons.logging.**
#-dontwarn org.apache.**

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多