Joshua Lewis
Joshua Lewis

Reputation: 31

Cannot resolve method 'newFactory' in XMLInputFactory MSGraph

The problem occurs within the XmlFactory java file from com.fasterxml.jackson.dataformat This is being used by the the MSGraph sdk for Java https://github.com/microsoftgraph/msgraph-sdk-java

I am trying to use it to send emails but get the following runtime error

Caused by: java.lang.NoSuchMethodError: No static method newFactory(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljavax/xml/stream/XMLInputFactory; in class Ljavax/xml/stream/XMLInputFactory; or its super classes

After some investigating it seems that the XMLInputFactory and XMLOutputFactory comes from an older version (stax:stax-api:1.0.1 I think according to the gradle dependency tree) before newFactory replaced newInstance Hence why the newFactory method cannot be resolved

Though I could be missing something I believe that this is the where the problem is, just that I don't know how to fix it.

Here is my build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.organisation.appname"
        minSdk 26
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }

    packagingOptions {
        exclude 'project.properties'
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/AL2.0'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/io.netty.versions.properties'

        //pickFirst 'META-INF/services/javax.xml.stream.XMLInputFactory'
    }
}

dependencies {
    //implementation 'javax.xml.stream:stax-api:1.0'
    //implementation 'stax:stax:1.2.0'
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation('com.microsoft.graph:microsoft-graph:5.29.0') {
        //exclude group: "stax", module: "stax"
        //exclude group: "stax", module: "stax-api"
    }
    implementation 'com.google.guava:guava:30.1.1-android'
    implementation('com.azure:azure-identity:1.2.5') {
        //exclude group: "stax", module: "stax"
        //exclude group: "stax", module: "stax-api"
    }
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.31"
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Though I believe the problem is as mentioned above, here is some other potentially relevant information. The error occurs when calling


this.interactiveBrowserCredential = new InteractiveBrowserCredentialBuilder()
            .clientId(clientId)
            .redirectUrl(myRedirectURL)
            .build();

Here is the relevant section of my code

public Emailing() {
        this.interactiveBrowserCredential = new InteractiveBrowserCredentialBuilder()
            .clientId(clientId)
            .redirectUrl(myRedirectURL)
            .build();

        this.tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, interactiveBrowserCredential);

        this.graphClient = GraphServiceClient
            .builder()
            .authenticationProvider(tokenCredentialAuthProvider)
            .buildClient();
}

public void emailForVisitorArrival(String name, String purpose, List<String> recipients) {
        Date timestamp = Calendar.getInstance().getTime();
        String timestampText = (String) DateFormat.format("dd MMM yyyy | hh:mm a", timestamp);

        Message message = new Message();
        message.subject = name + " has arrived.";
        ItemBody body = new ItemBody();
        body.contentType = BodyType.TEXT;
        body.content = myEmailContent;
        message.body = body;
        LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
        Recipient toRecipients = new Recipient();
        for (String recipient: recipients) {
            EmailAddress emailAddress = new EmailAddress();
            emailAddress.address = recipient;
            toRecipients.emailAddress = emailAddress;
            toRecipientsList.add(toRecipients);
            message.toRecipients = toRecipientsList;
        }

        graphClient.me()
                .sendMail(UserSendMailParameterSet
                        .newBuilder()
                        .withMessage(message)
                        .withSaveToSentItems(false)
                        .build())
                .buildRequest()
                .post();
}

I have tried the commented out lines in the build.gradle

Thank you for any help you can give me, let me know id more information is required and I will update the question.

Upvotes: 3

Views: 567

Answers (1)

Jatin Telang
Jatin Telang

Reputation: 61

I solved this via adding these dependencies in app's gradle.

implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.2")
implementation("javax.xml.stream:stax-api:1.0-2")

and excluding these from ACS-SDK's chat dependency(using chat dependency in my case).

implementation("com.azure.android:azure-communication-chat:2.0.3") {
    exclude(group = "com.microsoft", module="trouter-client-android") //ignore this exclusion
    exclude(group = "javax.xml.stream", module = "stax-api")
    exclude(group = "stax", module = "stax")
    exclude(group = "stax", module = "stax-api")
}

Upvotes: 0

Related Questions