Gradle: program type already present - java

I am trying to start working with libaums project ( https://github.com/magnusja/libaums ).
I wrote a class Log which mirrors the methods of android.util.Log, yet writes everything into a file (since I cannot get access to debug output stream on android). I was unable to understand how to build a package out of the module I wrote and include it as a local dependency into the libaums project ( Gradle compilation: package not found ), so I decided just to import Log.java (which is a com.iglogger package that implements Log class) in both of libaums projects (app and the actual libaums library). That in turn created a problem on the stage of merging dex files - so that app project depends on both Log class and libaums and yet libaums uses Log class too.
13684:09:44:38.705 [ERROR] [com.android.build.gradle.internal.tasks.DexMergingTaskDelegate] java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
13861:Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
13649:09:44:38.698 [ERROR] [org.gradle.api.Project] D8: Program type already present: com iglogger.Log
13685:Program type already present: com.iglogger.Log
13862:Program type already present: com.iglogger.Log
13871:Program type already present: com.iglogger.Log
After some research, the solution seems to be that I have to exclude the dependency on Log.java (i.e. com.iglogger.Log) while importing libaums into app project . But how exactly I suppose to do that in my case? It seems that I have declare a dependency on Log.java in the libaums project, so that I will be able to exclude it later on.. Right? If so, then how?

So the problem resolved with a removal of the files (com/iglogger) from the app project that depends on lib project.
I.e. the import statement in java seems to include not only declaration of packages to use, but, optionally, the objects of the packages themselves.

Related

Compiling application with Tika with Java 13 - problems loading modules

I'm trying to migrate a Java application that uses Tika from OracleJDK 1.8 to OPenJDK 13.
My IDE is Eclipse.
I have created the file module-info.java to indicate the required modules for my application.
In order to be able to use Tika classes such as AbstractParser, Detector, etc., I have added requires org.apache.tika.core; in module-info.java.
My code also uses the class org.apache.tika.parser.pdf.PDFParserConfig to extract embedded images:
PDFParserConfig pdfConfig = new PDFParserConfig();
pdfConfig.setExtractInlineImages(true);
context.set(PDFParserConfig.class, pdfConfig);'
I get the compilation error:
PDFParserConfig cannot be resolved to a type
Eclipse suggests to add requires org.apache.tika.parsers; to module-info.java: Eclipse suggestion screenshot.
When I add this module requirement to module-info.java, the application compiles properly.
That is, at this stage we have included in module-info.java:
module myapp {
/** others ... */
requires org.apache.tika.core;
requires org.apache.tika.parsers;
}
However, when trying to execute the compiled application, we get the error:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Admin\.m2\repository\org\apache\tika\tika-parsers\1.24\tika-parsers-1.24.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.tika.parser.onenote.OneNoteParser not in module
Inspecting the project Libraries in Eclipse, I can see that tika-core and tika-parsers (v1.24) are both modular: Eclipse Java Build Path
In conclusion: If I don't add org.apache.tika.parsers as a required module, the application won't compile, and if I add it I get the runtime error saying org.apache.tika.parser.onenote.OneNoteParser is not in the module.
I have inspected the JAR files for these packages to see the dependencies they have. The core packages seems to be right:
$ jar --file=tika-core-1.24.jar --describe-module
No module descriptor found. Derived automatic module.
org.apache.tika.core#1.24 automatic
requires java.base mandated
contains org.apache.tika
contains org.apache.tika.concurrent
contains org.apache.tika.config
contains org.apache.tika.detect
contains org.apache.tika.embedder
contains org.apache.tika.exception
contains org.apache.tika.extractor
contains org.apache.tika.fork
contains org.apache.tika.io
contains org.apache.tika.language
contains org.apache.tika.language.detect
contains org.apache.tika.language.translate
contains org.apache.tika.metadata
contains org.apache.tika.mime
contains org.apache.tika.parser
contains org.apache.tika.parser.digest
contains org.apache.tika.parser.external
contains org.apache.tika.sax
contains org.apache.tika.sax.xpath
contains org.apache.tika.utils
...but the 'parsers' jar gives an error:
$ jar --file=tika-parsers-1.24.jar --describe-module
Unable to derive module descriptor for: tika-parsers-1.24.jar
Provider class org.apache.tika.parser.onenote.OneNoteParser not in module
Does this mean the jar package for parsers is not well formed?
Is there any workaround for this?
Thank you.
EDIT:
If I try with version 1.24.1, I get the execution error:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Admin\.m2\repository\org\apache\tika\tika-parsers\1.24.1\tika-parsers-1.24.1.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.tika.parser.external.CompositeExternalParser not in module
That is: the failing class is CompositeExternalParser instead of OneNoreParser.
Inspecting META-INF/services/org.apache.tika.parser.Parser of tika-parsers-1.42.1.jarI can see the entryorg.apache.tika.parser.external.CompositeExternalParser` but the package does not contain this class.
So, it seems to be an error in this META-INF file. Id this due to an error when compiling the package and submitting it to Maven Central?
I've found a JIRA issue, TIKA-2929, where they say "Apache Tika needs to be on the Java Classpath, not the module path". I've tried this, but, as explained before, I get a compilation error if I don't add it to the module path and set requires org.apache.tika.parsers;.
This is a hard puzzle...
Ran into the same issues.
Also found the faulty entries in
org.apache.tika.parser.Parser (and also org.apache.tika.parser.Detector) in META-INF/services/
A quick fix is to ...
Unpack those files
delete the lines that seem to reference non existing classes
pack them back into the jar
My project compiled after that.
For sure no longterm solution, but since even older versions i tried ran into that problem, it might help out some people.

Unable to derive module descriptor: Provider {class X} not in module

I am getting this error message when I try to compile my new modularized Java 11 application:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\inter\.m2\repository\xalan\xalan\2.7.2\xalan-2.7.2.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.bsf.BSFManager not in module
This appears to be an issue from a dependency of a dependency. I can't even find which module is pulling it in so I can update it.
I am using openjdk 11.0.2, IntelliJ 2018.3.4, Maven
Any advice how I can troubleshoot or fix this? I have found very little documentation on this issue.
Xalan
I had a look at their bug tracker following their index page and wasn't able to find this reported and not sure how actively is the library being maintained either.
General Explanation
Just to explain what has caused the issue in your code, I would share a screenshot and then try to add details around it.
So within the JAR that for version 2.7.2, there are service declarations (META-INF/services) which include org.apache.xalan.extensions.bsf.BSFManager as one of them. The service file here has to indicate the Provider thereby for itself and the class is supposed to be present on the modulepath to be resolved for reliable configuration of modules.
In this case for the module xalan(automatic module), the service listed doesn't have the provider class packaged within the dependency itself. (See the package org.apache, it doesn't further have package bsf and the class BSFManager thereby. Hence the exception as you get.
Short term hack
One of the tweaks to get that resolved would be to get update the library jar (patch it) and get rid of the service file if you're not using it. Or to add the provider copied from the corresponding artifact.
If you don't directly depend on this artifact or its parent dependencies, you can let those remain on the --classpath and get resolved as an unnamed module for your application.
Long term solve
An ideal way would be to report this to the maintainers and getting it resolved. It depends though on how actively are they maintaining it e.g. the last release for xalan was almost 5 years back, might just want to look for an actively participated alternative in my opinion.
I tried to install update for TestNG in eclipse:
"Help -> Check for updates -> deselect all and select TestNG check box. Then
install latest version i installed the version which starts with 7.2.0.
It fixed the issue for me.

Error building cordova android project with uppercase letters in package name

I have a Cordova app several years old, which has capital letters in its package name.
It was working well before, with older versions of Cordova and building in Eclipse.
Now I cannot build it with Cordova 6.1.1 and gradle. It gives errors.
I have detected that it doesn't create the folder structure and files for the app class at src folder. It should create PackageFirst folder, then inside it PackageSecond since package name is PackageFirst.PackageSecond
I generated and copied those files myself. The cordova prepare command deletes the folder, so I run prepare and then copy the folder back in. Then I try compile. But it breaks again and the build doesn't succeed, with this error:
Here is my LogCat.
BUILD FAILED at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)
Caused by: com.android.dx.cf.iface.ParseException: class name (PackageFirst/PackageSecond/MainActivity) does not match path (packagefirst/packagesecond/MainActivity.class)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:520)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
I have been researching and the only solution I find, is that I need to make the package name all lowercase. Since this app is published at Google Play, I believe I cannot do that since it would be a different package name.
I have been able to build this perfectly for years until now.
Can anyone give me a solution to this problem that doesn't involve changing the package name? Thank you so much for any help!
EDIT:
I could get the package to build successfully, but it doesn't run on the device. With all the testing I had left uppercase in one place and lowercase in another. Once I fixed that, it built, but when trying to run it on the device, I get the following error:
Error: Failed to install apk to device: pkg: /data/local/tmp/android-debug.apk
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]
Kindly name your packages in lower case. As I can see from error you posted above
com.android.dx.cf.iface.ParseException: class name
(PackageFirst/PackageSecond/MainActivity) does not match path
(packagefirst/packagesecond/MainActivity.cla ss)
the package names have different case. This might be a cause

Module dependency - package does not exist in compile time

I've read a lot of questions & answers in SO, without any luck. Example: Module packages not found at compile time in IntelliJ
The thing is that I've a project which formed by:
Module A
Module B
Module C
Module B has a dependency with module A and module C.
So the imports in module B, to code are like
import com.moduleA.Fragment1;
import com.moduleA.Fragment2;
import com.moduleA.SomeInterface;
Those lines are correctly imported in "coding-time", I can work with those classes correctly.
However when I do compile, it crashes with a:
Error:(8, 32) error: com.moduleA does not exist
I've tried adding this module A as Android Library but it's not OK for me, because Android requires generating constants fields (http://tools.android.com/tips/non-constant-fields).
I don't know what else to do.
Any tips?
Sorry for the people who asked for an example of code, but it was as basic as that, with a Gradle build.
I fixed the issue by creating a library project within my project. So module A is actually a Library.
I said that I couldn't use library because of the non-constant-field, but that error was generated by AndroidAnnotations, which explains how to create library modules with it:
https://github.com/excilys/androidannotations/wiki/Library-projects#referencing-ids-by-name
Thanks.

android external jar issue

In search of a way to upload an image, I added a lot of external JARs following this simple explanation:using-multipartentity-in-android-applications
But when I try co compile the code I'm getting an error:
[2012-12-14 11:37:55 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/apache/http/ConnectionClosedException;
I have never used external libraries before. Can someone tell me how to fix that?
The same class/classes may be defined in multiple libraries. That's why you are getting this error. You need to check which classes are overlapping and resolve the issue by re-organizing or removing some external libraries.
It might also be an eclipse issue. Try cleaning your project.
See the following threads:
Unable to execute dex: Multiple dex files define Lcom/myapp/R$array;
Unable to execute dex: Multiple dex files define
Error when building apk - "Multiple dex files define Lcom/google/ads/Ad"
If you type
org.apache.http.auth.AUTH auth;
somewhere in your code, then select the AUTH part, right click it and hit Declarations > Workspace, you'll where this class is declared. It's in android.jar, but in my project it was also in two separate versions of httpclient-4.x.x.jar (one had got added in as a dependency of another part of the project).
Removing the earlier version of httpclient-4.x.x.jar seems to have fixed this for me; you may find you have multiple libraries included in your project that declare this class (other than android.jar) and removing one or more will help too.
Right click the src and gen folders and select Build Path > Remove from Build Path. then Right Click > Build Path > Use as Source Folder

Categories

Resources