android - 在 Android 库 AAR 中使用时无法找到 Volley 的字节码

标签 android android-volley proguard android-proguard

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

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

无法找到 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.*

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

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

我还尝试了默认的 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 文件弄错了?

谢谢!

最佳答案

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

我关心的是:

consumerProguardFiles 'proguard-library.pro'

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

Check the comment:

consumerProguardFiles should be specified in defaultConfig rather than buildTypes/release so that it works if the consuming application proguards in both debug and release mode (e.g. to avoid the 65k dex method limit)

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

关于android - 在 Android 库 AAR 中使用时无法找到 Volley 的字节码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52412570/

相关文章:

android - 如何将Volley包含在Unity插件中?

android - Proguard - 找不到引用的方法

java - 如何从我的 Android 设备获取未读短信/未接来电?

android - ListView 中的微调器

android - 用点提示密码 EditText? [安卓]

php - android 从 mysql 中检索数据并将其呈现在 recycleview 中并添加事件

Android Studio 布局 : Trying to make button 50% width and 50% height,,中间有按钮

php - 使用volley在android中检索Json对象

android - 使用 minifyEnabled true 启动应用程序时 FirebaseInitProvider.onCreate 中的 java.lang.IncompatibleClassChangeError

android - Proguard 不再适用于 Retrofit