Failed to find byte code for Volley when using it in an Android library AAR
Asked Answered
B

2

9

I'm using Volley in a library project that I've built into a AAR file.

I add the AAR file into the main project. When building the main project, I get the following error:

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

I'm guessing it has something to do with the Proguard config, however I get the error even if I build a debug variant when creating the AAR.

This is my Proguard file for the library project:

# 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.*

Any tips on what might be causing this?

Update: I've tried this https://mcmap.net/q/213051/-proguard-ignores-config-file-of-library regarding the consumerProguardFiles Proguard file, but I still get the same error when building the main project.

I also tried the default proguard-library.pro file along with my Volley lines:

#
# 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.*

Additionally I've tried commenting out the Proguard lines in buildTypes, since the module will be open source and I'm not needing obfuscation, but still no luck.

This is the build.gradle file:

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

Could I be making the AAR file wrong?

Thanks!

Bernie answered 19/9, 2018 at 18:58 Comment(5)
Can you post your gradle file?Glutamine
@FcoP. Added, thanksBernie
That's the proguard file, I meant the build.gradle fileGlutamine
@FcoP. sorry, misread your comment. There it is. ThanksBernie
weird, I made a test project using your files and it still compiles, I set both release and debug to use proguard. can you check? github.com/fcopardo/stackOverflowQuestion1Glutamine
S
2

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

The thing that I concerned about, was this:

consumerProguardFiles 'proguard-library.pro'

You used this line of code in both release and defaultConfig block and I believe this causes the issue.

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 rules doesn't seem to be the issue. At least, you can try updating Appcompat then testing the project as well as I created a test project and it worked with your current codes.

Saintmihiel answered 26/9, 2018 at 20:12 Comment(1)
-keep tells proguard not to obfuscate the matched specification. guardsquare.com/en/products/proguard/manual/…Liriodendron
G
2

better comment out -dontwarn and add -verbose (on top), while testing the rules.

... the required rule might either be:

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

or with two ** (the same applies to all the other entries, with a single *):

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

it could be even explicitly spelled-out, alike:

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

about like this:

-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.**
Gnarl answered 27/9, 2018 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.