#StackBounty: #android #kotlin #htmlunit HtmlUnit library gives error on Android: Program type already present: org.apache.http.auth.Co…

Bounty: 50

I’m trying to run HtmlUnit on Android. There is a duplicate org.apache library that is causing a problem. I need to exclude some module. But already excluded httpclient and added the correct Android version. But still, it’s not working.

The other threads related to this question are not about Kotlin or are outdated.

Consider below code:

https://github.com/JimClermonts/HtmlUnit-for-Android

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    doAsync {
        val webClient = WebClient()
        var page: HtmlPage = webClient.getPage<HtmlPage>("http://www.google.com")
        val searchBox = page.getElementByName<HtmlInput>("q")
        searchBox.valueAttribute = "htmlunit"

        val googleSearchSubmitButton: HtmlSubmitInput = page.getElementByName("btnG") // sometimes it's "btnK"
        page = googleSearchSubmitButton.click()

        val resultStatsDiv: HtmlDivision = page.getFirstByXPath("//div[@id='resultStats']")

        System.out.println(resultStatsDiv.asText()) // About 309,000 results

        webClient.close()
    }
}


dependencies {
   //Project Dependencies
    implementation group: 'commons-io', name: 'commons-io', version: '2.6'
//    implementation group: 'commons-logging', name: 'commons-logging', version: '1.2'
    implementation group: 'commons-net', name: 'commons-net', version: '3.6'
    implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit-core-js', version: '2.34.0'
    implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit-cssparser', version: '1.3.0'
    implementation group: 'net.sourceforge.htmlunit', name: 'neko-htmlunit', version: '2.34.0'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
    implementation group: 'org.apache.commons', name: 'commons-text', version: '1.6'
    implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-client', version: '9.4.15.v20190215'
    implementation group: 'xalan', name: 'xalan', version: '2.7.2'

    //Project Transitive Dependencies
    implementation group: 'commons-codec', name: 'commons-codec', version: '1.11'
    implementation('org.apache.httpcomponents:httpmime:4.5.7') {
        exclude module: 'httpclient'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'
//    implementation group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11'
    implementation group: 'org.eclipse.jetty', name: 'jetty-client', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty', name: 'jetty-http', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty', name: 'jetty-io', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty', name: 'jetty-util', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty', name: 'jetty-xml', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-api', version: '9.4.15.v20190215'
    implementation group: 'org.eclipse.jetty.websocket', name: 'websocket-common', version: '9.4.15.v20190215'
    implementation group: 'xalan', name: 'serializer', version: '2.7.2'
    implementation group: 'xerces', name: 'xercesImpl', version: '2.12.0'
    implementation group: 'xml-apis', name: 'xml-apis', version: '1.4.01'


    implementation "org.jetbrains.anko:anko-commons:0.10.1"
    implementation "org.jetbrains.anko:anko-design:0.10.1"
    implementation "org.jetbrains.anko:anko-coroutines:0.10.1"

}

<uses-permission android:name="android.permission.INTERNET" />

error message:

> Task :app:transformClassesWithDexBuilderForDebug UP-TO-DATE

> Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
AGPBI: {"kind":"error","text":"Program type already present: org.apache.http.auth.ContextAwareAuthScheme","sources":[{}],"tool":"D8"}

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
  Program type already present: org.apache.http.auth.ContextAwareAuthScheme


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.