在AAR文件java.lang.NoClassDefFoundError中找不到活动类 [英] Activity Class cannot be found in AAR file java.lang.NoClassDefFoundError
问题描述
我创建了一个android库,其中包含一个用于打开相机意图的Activity类,该库工作正常,除了调用该Activity的部分显示在屏幕上之外,并且应用因以下错误而崩溃
I have created an android library, it contains an Activity Class that uses to open camera intent, the library works perfect excepts for the part that invokes the Activity to appear over the screen and the app crashes throwing following errors
Suppressed: java.lang.ClassNotFoundException: com.me.scanner.ScannerViewActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 14 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
以下是我将使用此库的其他项目的build.gradle
文件
Following is my build.gradle
file of other project that will use this library
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.me.app:gpio_lib@aar'
compile ('com.me.app:scanner@aar'){ transitive = true; }
testCompile 'junit:junit:4.12'
}
请注意:
-
此库可完美用作模块项目,
This library works perfectly as module project,
compile project(':scannercontrol')
,但在项目中用作aar
时不能使用
but not when using it as aar
in the project
我尝试了是否使用{ transitive = true; }
,没有变化
I have tried with and without { transitive = true; }
, no change
更新1-2017-09-14
活动ScannerViewActivity
是我的库的一部分,这是从一个库类中调用的方式
The activity ScannerViewActivity
is part of my library, this is how it is been called from one of the library class
Intent scannerView = new Intent();
scannerView.putExtra(MESSAGE_RECEIVER, new MessageReceiver());
scannerView.setClass(context, ScannerViewActivity.class);
scannerView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scannerView);
此context
是android.app.Application
的实例,而不是任何Activity
this context
is the instance of android.app.Application
and not of any Activity
更新2-2017-09-14
幸运的是,在Google各处漫游了2天后,我遇到了问题在AAR中找不到的类的答案.我已将库的依赖项添加到项目中,并能够使用aar
运行它,但这不是我的问题的解决方案,
Fortunately after roaming 2 days all over google I come across the answer of question Class not found in aar. I have added the dependencies of the library to the project and able to run it with aar
but this is not the solution of my issue,
以下是我当前面临的流程和问题
-
我正在开发与特定条形码扫描器硬件通信的库的沙盒版本,因为开发人员不一定拥有硬件,他们将使用库的沙盒版本,从而允许开发人员使用他们的设备相机作为条形码扫描仪,
I'm developing a sandbox version of a library that communicates with a specific barcode scanner hardware, since the developer doesn't necessarily have the hardware they will use the sandbox version of the library that will allow developer to use their device camera as a barcode scanner,
我们希望开发人员从提供的SDK
中提取AAR
文件scanner.aar
和scanner-sandbox.aar
并使用它们中的任何一个
We want the developer to extract the AAR
files scanner.aar
and scanner-sandbox.aar
from provided SDK
and use either of them
如果希望运行库的沙盒版本,我们不希望开发人员为其.gradle
文件添加额外的依赖关系,
We don't want the developer to add extra dependency to their .gradle
file if they want to run the sandbox version of the library,
以下是用于将设备相机模拟为条形码扫描仪的依赖项
Following is the dependency that is being used to simulate device camera as a barcode scanner
compile 'me.dm7.barcodescanner:zxing:1.9.8'
在不将其发布到 maven存储库之类的任何存储库中以及开发人员同步.gradle
文件时安装的库的所有依赖项的情况下,分发aar文件的可能方法是什么?
What is the possible way to distribute an aar file without publishing them to any repository like maven repository and all dependencies of the library installed when developer syncs the .gradle
file
推荐答案
zeeshan
我也遇到了同样的问题,我做了很多研究和研究.我希望它能对您有所帮助
I have faced same issue i did lot of research & i fixed hopefully it helps you
为项目创建.aar文件:
Creating .aar file for a project:
- 对于库模块,.aar文件由Android Studio自动创建.对于具有任何资源文件或具有 库,.aar文件可以按照后续步骤生成.
- 检查插件.它应如下所示,如果在应用程序中,则更改为库,然后应用插件:"com.android.library"
- 打开打开的右边框gradle栏,然后转到您的项目"->任务"->构建",然后双击assembleRelease. 在构建项目时,您将找到两个.aar app/build/outputs/aar/中的文件(app-debug.aar和app-release.aar) 目录.在项目中使用.aar文件:
-
获取"app-release.aar"文件并将其粘贴到项目的libs文件夹中.如果没有libs文件夹,则创建一个名为 应用程序目录中的库".
- For a library module, .aar file is automatically created by Android Studio. For a project with the any resource file or with the libraries, .aar file can be generated as mentioned in next steps.
- Check the plugin. It should be in as follows, change to library if it is in application, apply plugin: 'com.android.library'
- Open the open right border gradle bar and go to "your project"->Tasks->build and then double click on the assembleRelease. On Building the project, You will find the two .aar files(app-debug.aar and app-release.aar) inside app/build/outputs/aar/ directory. Using .aar file in your project:
Get the "app-release.aar" file and paste it in the libs folder of your project. If libs folder is not present, create a directory named "libs" inside app directory.
将以下内容添加到应用gradle文件,存储库{ flatDir { dirs'libs' }}依赖项{ compile(name:'app-release',ext:'aar')}
Add the following to the app gradle file, repositories { flatDir { dirs 'libs' } } dependencies { compile(name:'app-release', ext:'aar') }
错误的假设:System.err:java.lang.NoClassDefFoundError:
解决方案:
Solution:
.aar gradle依赖项必须在项目应用程序中手动添加 摇动
.aar gradle dependencies has to be added manually in your project app gradle
这篇关于在AAR文件java.lang.NoClassDefFoundError中找不到活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!