在Android库AAR中使用Volley时找不到字节码

时间:2018-09-19 18:58:23

标签: android android-volley proguard android-proguard

我在一个内置到AAR文件中的图书馆项目中使用Volley。

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

abstract class ParentExampleActivity extends AppCompatActivity{ public abstract void reset(View view); public abstract void update(View view); } class ExampleActivity extends ParentExampleActivity { public void reset(View view) {...} public void update(View view) {...} public void alter(View view) {...} }

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

这是我的图书馆项目的Proguard文件:

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

关于什么可能导致这种情况的任何提示?

更新:我已经针对# 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.* Proguard文件尝试了此https://stackoverflow.com/a/27052696/1020311,但是在构建主项目时仍然遇到相同的错误。

我还尝试了默认的consumerProguardFiles文件以及我的Volley行:

proguard-library.pro

另外,我尝试注释掉# # 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.* 中的Proguard行,因为该模块将是开源的,并且我不需要混淆,但是仍然没有运气。

这是build.gradle文件:

buildTypes

我可以把AAR文件弄错吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

  

无法找到 com/android/volley/Response$Listener

的字节码

我关心的是:

consumerProguardFiles 'proguard-library.pro'

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

Check the comment:

  

consumerProguardFiles 应该在defaultConfig中指定 而不是   buildTypes / release ,以便在使用应用程序的情况下也可以使用   在调试和发布模式下均采取防护措施(例如,避免使用65k dex   方法限制)

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

答案 1 :(得分: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.**