Search code examples
javaandroidzxingaar

Getting Failed resolution of: Lcom/google/zxing/RGBLuminanceSource; when implementing AAR


I am building an AAR in which i have Zxing library implemented. When i try to use this AAR in another application it gives java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/zxing/RGBLuminanceSource; in following method

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

and when i use above method directly in application without implementing it in AAR it runs perfectly

Following are the dependencies used in 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'

and following are the dependencies used in application

    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'

Solution

  • AAR is built with your code only. No dependencies end up in AAR by default. Here is an option on how to include dependencies into AAR.

    Also, it may be helpful to know about transitive dependencies: Transitive dependencies not resolved for aar library using gradle

    Update

    To fix the issue with META-INF files you can use Gradle packagingOptions. Inside of your build.gradle file find android block and insert the following:

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