0

I need to exclude a class which is present in a third party library jar file. i don't know the exact syntax format to exclude a group. i tried the following syntax for excluding a class named XMLConstants.class inside the library "javax.xml.stream.jar".

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:multidex:1.0.1'
compile files('libs/xsdlib-1.5.jar')
compile files('libs/relaxngDatatype-2.2.jar')
compile files('libs/javax.xml.stream.jar'){
    exclude module: 'javax-xml-stream'
}
compile files('libs/pull-parser-2.jar')
compile files('libs/javax.xml.bind.jar')
compile files('libs/java-rt-jar-stubs-1.5.0.jar')
compile files('libs/jaxen-core.jar')
compile files('libs/jaxen-dom4j.jar')
compile files('libs/jaxen-1.1-beta-2.jar')
}

If i use this syntax i am getting the following error.

Error:(52, 0) Could not find method exclude() for arguments [{module=javax-   
xml-stream-XMLConstants}] 

I am new to android so i need to know how to write a syntax to exclude a particular class in a third party library jar file. If there is any other method to do this please suggest. Thanks in advance.

Rakesh Polo
  • 431
  • 1
  • 9
  • 27

1 Answers1

0

Check this post and also this. Both says that you need to add the excluding class name inside sourceSets.

EDIT : Try it like this. I didn't got any error while compiling.

sourceSets {
        main {
            java {
                srcDir 'libs'
                exclude 'org.apache.http.protocol.RequestDate.class'
            }
        }
    }
Community
  • 1
  • 1
Somnath Pal
  • 1,570
  • 4
  • 23
  • 42
  • I checked both the links but in my case i need to exclude a "xxyyzz.class", which is present inside a external jar library. i tried something like this sourceSets { app { libs { exclude 'java/xml/stream/XMLConstants/**' } } } still getting error. – Rakesh Polo Jan 08 '17 at 06:35
  • Thanks @Somnath Pal , i am still getting the duplicate class error. This is my library path C:\Users\Xyz\abc\WORK SPACE\MyApplication\app\libs and the class to be excluded is "XMLConstants.class" – Rakesh Polo Jan 08 '17 at 07:33
  • You might be getting the duplicate error because you added two similar jar files in lib folder. Like if you add org.apache.legacy.jar and httpcore.jar you will get duplicate error while building. I see you have included `javax.xml.stream.jar` and `javax.xml.bind.jar`. The error might be due those two. – Somnath Pal Jan 08 '17 at 07:37
  • yea exactly the same problem i am facing. I have two jars which contains same two classes inside it. If i remove any one library i am getting import errors. The thing is i need both the libraries to support my project. Can you help me in this? – Rakesh Polo Jan 08 '17 at 08:00
  • Tell me for which class you need the import and I'll check if one can be removed. – Somnath Pal Jan 08 '17 at 08:07
  • import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Characters; import javax.xml.stream.events.EndElement; import javax.xml.stream.events.EntityReference; import javax.xml.stream.events.StartDocument; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; i need class for these imports. – Rakesh Polo Jan 08 '17 at 08:11
  • I checked all the imports. I think for all the above imports, you can just use `stax-api-1.0.jar`. This jar file contains all the above imports. – Somnath Pal Jan 08 '17 at 08:41
  • hey sorry for the late reply. I imported the stax-api-1.0.jar. but now the conflict is i get duplicate error for two other jars files. so i removed one jar file and getting import errors that requires following classes java.beans.IntrospectionException.class java.beans.Introspector.class java.beans.PropertyDescriptor.class javax.swing.tree.TreeNode.class javax.swing.table.AbstractTableModel.class javax.swing.tree.TreeNode.class javax.swing.tree.DefaultTreeModel.class – Rakesh Polo Jan 08 '17 at 10:19
  • Remove all related jars. Go to http://www.findjar.com/ and check for all the classes you want to import. it will show you the common jars which contain those classes. Download the best one – Somnath Pal Jan 08 '17 at 10:39
  • http://www.findjar.com/jar/pull-parser/pull-parser/2.1.10/pull-parser-2.1.10.jar.html?all=true bro if i click the download option in the above link it takes me to some other maven page. – Rakesh Polo Jan 08 '17 at 11:03
  • Without full code, I can only say that you can download the jar files from any website. This website will give you the name of the jar file which supports all your classes. So find the jar file and download it from anywhere you like. – Somnath Pal Jan 08 '17 at 11:45