首页 > 解决方案 > 获取失败的解决方案:Lcom/google/zxing/RGBLuminanceSource;实施 AAR 时

问题描述

我正在构建一个 AAR,在其中实现了 Zxing 库。当我尝试在另一个应用程序中使用此 AAR 时,它会给出 java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/zxing/RGBLuminanceSource;以下方法

public static String decodeQRImage(Bitmap bMap) {
    String value = null;

    int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Reader reader = new QRCodeReader();
    try {
        Result result = reader.decode(bitmap);
        value = result.getText();
    } catch (NotFoundException e) {
        e.printStackTrace();
        value = e.getMessage();
    } catch (ChecksumException e) {
        e.printStackTrace();
        value = e.getMessage();
    } catch (FormatException e) {
        value = e.getMessage();
        e.printStackTrace();
    }
    return value;
}

当我直接在应用程序中使用上述方法而不在 AAR 中实现它时,它运行完美

以下是 AAR 中使用的依赖项

    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    implementation 'com.kaopiz:kprogresshud:1.2.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
    implementation 'com.github.f0ris.sweetalert:library:1.5.1'
    implementation 'com.google.android.gms:play-services-vision:18.0.0'
    implementation 'com.scottyab:rootbeer-lib:0.0.7'
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation "androidx.versionedparcelable:versionedparcelable:1.1.0"

    implementation 'com.google.android.play:core:1.6.3'
    implementation 'org.jsoup:jsoup:1.11.3'

以下是应用程序中使用的依赖项

    implementation project(":ziplibrary-debug")
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

标签: javaandroidzxingaar

解决方案


AAR 仅使用您的代码构建。默认情况下,AAR 中没有依赖项。这是有关如何将依赖项包含到 AAR中的选项。

此外,了解传递依赖关系可能会有所帮助:Transitive dependencies notsolved for aar library using gradle

更新

要解决META-INF文件问题,您可以使用 Gradle packagingOptions。在您的build.gradle文件查找android块内并插入以下内容:

android {
    ...

    packagingOptions {
        exclude 'META-INF/retrofit.kotlin_module'
    }
}

推荐阅读