简体   繁体   English

运行时的NoClassDefFoundError在Android Studio中的.AAR类中

[英]NoClassDefFoundError at Runtime on class inside .AAR in Android Studio

I've been researching for many hours about a possible solution to this but whatever I try it just fails. 我已经研究了很多小时关于可能的解决方案,但无论我尝试它只是失败。 Here's the explained situation: 这是解释的情况:

I have this library project on Android Studio which generates the app-debug.aar file. 我在Android Studio上有这个库项目,它生成app-debug.aar文件。 Then I add this new module in the implementing project, following the wizard to import .JAR/.AARs I pick the .aar and update the dependency for :app in the Project Structure. 然后我在实现项目中添加这个新模块,按照向导导入.JAR / .AARs我选择.aar并在项目结构中更新:app的依赖项。

Once the project is cleaned (even using ./gradlew clean --Mac OS X) I can make references of this class and its methods in any of the activities. 一旦清理了项目(甚至使用./gradlew clean --Mac OS X),我可以在任何活动中引用该类及其方法。

Now at runtime, once built and installed on the real devices the app will crash throwing the aforementioned Runtime Exception. 现在,在运行时,一旦在真实设备上构建和安装,应用程序将崩溃抛出上述运行时异常。

Note: I'm importing the .aar because I would need to protect the code and it has resources so a .jar is out of the question. 注意:我正在导入.aar因为我需要保护代码并且它有资源,所以.jar.aar的。 And if I import the library module (with source and all instead of an .aar ) then the app has no issues at runtime. 如果我导入库模块(使用source和all而不是.aar ),那么应用程序在运行时没有问题。

I've tried everything and if anyone can throw some light on what this could be, it'll be highly appreciated. 我已经尝试了所有的东西,如果有人能够对这可能会有所了解,那么我将非常感激。

The class extends ViewGroup and is instantiated at runtime in case that implies anything. 该类扩展了ViewGroup并在运行时实例化,以防任何事情。 The code was given to me so major changes might not be possible unless it's absolutely necessary. 代码是给我的,所以除非绝对必要,否则可能无法进行重大改变。

Thanks beforehand to all! 先谢谢大家!

Armando 阿曼多

For those, who are still looking for the solution, following two options worked for me to solve the exact same problem as OP has mentioned. 对于那些仍在寻找解决方案的人来说,遵循两个选项可以解决与OP提到的完全相同的问题。

  • Include the (problematic) library dependency in the target module as well, ie the in my case i was including the protobuf-lite as a dependency to my library module but getting noclassdefFound error when lib imported as .aar in app module. 在目标模块中也包含(有问题的)库依赖项,即在我的情况下,我将protobuf-lite包含为我的库模块的依赖项,但是当在app模块.aar lib导入为.aar时, noclassdefFound出现noclassdefFound错误。 As a workaround, i added the protobuf-lite dependency to my app module too and it worked like charm. 作为一种解决方法,我也将protobuf-lite依赖项添加到我的应用程序模块中,它就像魅力一样。

  • Second option that worked for me that instead of adding the gradle dependency `compile 'com.google.protobuf:protobuf-lite:3.0.1', i downloaded the protobuf-jar from maven and added manually to the libs of my library module, and the problem got solved. 第二个选项对我有用,而不是添加gradle依赖`compile'c​​om.google.protobuf:protobuf-lite:3.0.1',我从maven下载了protobuf-jar并手动添加到我的库模块的库中,问题解决了。

Don't know what's wrong with the gradle plugin, but hope it helps someone else looking for the same problem. 不知道gradle插件有什么问题,但希望它可以帮助其他人寻找同样的问题。

The NoClassDefFound error has actually happening to a backward-compatibility library being used by this class I mentioned on the question ("Class A"). NoClassDefFound错误实际上发生在我在问题上提到的这个类使用的向后兼容库(“A类”)。 No details were given other than Class A couldn't be found and later on found out that another class had a similar issue BUT was pointing at that compat lib with the same exception and I noticed that Class A was also calling its methods and implementing its callbacks. 除了A类之外没有给出任何细节,后来又发现另一个类有类似的问题但是指向同一个例子的compat lib我注意到A类也在调用它的方法并实现它回调。 By bumping up the min version (to use the native API) I could overcome that issue. 通过提高min version (使用本机API),我可以克服这个问题。 It was indeed a problem of including that backward-comp library in the .aar or so it seems. 这确实是一个问题,似乎在.aar左右包含了.aar -comp库。 Should I understand this issue a lot better I'll update this "answer." 我是否应该更好地理解这个问题,我会更新这个“答案”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM