在AAR文件java.lang.NoClassDefFoundError中找不到活动类

我创建了一个android库,其中包含用于打开相机意图的Activity类,该库工作正常,除了调用Activity出现在屏幕上的部分外,该应用程序因抛出以下错误而崩溃

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文件

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

请注意:

>该库可以完美地用作模块项目,

compile project(':scannercontrol')

但是在项目中用作aar时不可以
>我尝试了有无{传递=真; }, 没变
>我已将我的AAR文件重命名为scan.aar
>我采用了aar文件格式/[library-project]/build/outputs/aar/scanner-release.aar

更新1-2017-09-14

活动ScannerViewActivity是我的库的一部分,这是从一个库类中调用它的方式

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);

此上下文是android.app.Application的实例,而不是任何Activity的实例

更新2-2017-09-14

幸运的是,在google上漫游了2天后,我遇到了问题Class not found in aar的答案.我已经将库的依赖项添加到了项目中,并能够通过aar运行它,但这不是我的问题的解决方案,

以下是我目前面临的流程和问题

>我正在开发与特定条形码扫描器硬件通信的库的沙箱版本,因为开发人员不一定具有硬件,他们将使用库的沙箱版本,从而允许开发人员将其设备摄像头用作条形码扫描器,
>我们希望开发人员从提供的SDK中提取AAR文件scanner.aar和scanner-sandbox.aar并使用其中任何一个
>如果开发人员要运行库的沙盒版本,我们不希望开发人员对其.gradle文件添加额外的依赖关系,
>以下是用于将设备相机模拟为条形码扫描仪的依赖项

compile 'me.dm7.barcodescanner:zxing:1.9.8'

更新的问题是

在开发人员同步.gradle文件时,不将aar文件发布到任何存储库(如maven存储库)以及已安装库的所有依赖项而分发它们的可能方法是什么?

解决方法:

zeeshan

我遇到了同样的问题,我做了很多研究和研究.我希望它能对您有所帮助

Creating .aar file for a project:

  1. 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.
  2. Check the plugin. It should be in as follows, change to library if it is in application, apply plugin: ‘com.android.library’
  3. 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:
  4. 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.

  5. Add the following to the app gradle file, repositories {
    flatDir {
    dirs ‘libs’
    } } dependencies {
    compile(name:’app-release’, ext:’aar’) }

  6. On sync, your project is ready to use the library files. Note : .aar gradle dependencies has to be added manually in your project app
    gradle
    Example:below

错误的假设:System.err:java.lang.NoClassDefFoundError:

解:

.aar gradle dependencies has to be added manually in your project app
gradle

上一篇:修改Android .aar文件的内容/转换为.zip格式


下一篇:如何发布另一个Android库项目依赖的AAR?