Gradle Dependencies - java

I'm trying to use JSON Simple to parse a JSON file into Java in a Gradle JavaFX project.
I've tried a few things:
Adding implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' to the build.gradle file. ... Won't let me do import org.json.simple.JSONParser;
Error: error: package org.json.simple does not exist
Adding implementation 'com.googlecode.json-simple:json-simple:1.1.1' to the build.gradle file. ... gives same error as above.
In IntelliJ, I've tried manually downloading and adding the jar file:
First I added a new directory to the root called 'libs' and added the jar file there. Went to File > Project Structure > Libraries > '+' > Java, selected the file and pressed the module I wanted to add it to. Then I added requires json.simple; into the module-info.java file.
IntelliJ does not come up with any errors, but when I do gradle run, it comes up with an error in the modules-info.java file that says error: module not found: json.simple
I have no idea why that last error is happening, I made sure the module was applied to the root as well (Where the modules-info.java file is.
I've tried gradle clean, invalidating caches and removing caches altogether.
If anyone knows why this issue is occuring (or can suggest another way I can parse a JSON file), that would be great!
Thanks in advance.

Related

Importing missing package (sun.security.pkcs.PKCS10) automatically in Android Studio

I have to use PKCS10 class into my project. I tried to import sun.security.pkcs.PKCS10; into my activity code but studio shows "Cannot resolve symbol PKCS10" at both places where I have imported it and where I want to instantiat it.
Note: I haven't added any dependency or library (like .jar) to my project.
I want to know whether can it be automatically?
The class sun.security.pkcs10.PKCS10 was removed from the JDK. Using a class that was internal and already removed from the JDK is going a step beyond just using an internal API.
Put this into your build.gradle file.
compileJava {
options.forkOptions.javaHome = file(System.properties['java.home'])
}
And if this does not work, put this into you build.gradle(app) dependencies.
compile group: 'com.sun', name: 'rt', version: '1.5.0_06'

Add .jar to dependencies in React Native

I am quite new to React Native but I need to create an Android Module for the Honeywell CT50 barcode scanner.
I have created a libs folder in MyApp/android/app as stated in these two SO posts:
Post 1
Post 2
The second post says I must then add the package name to the dependencies something like this:
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile 'com.facebook.react:react-native:0.19.+'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
}
This the part I am struggling with, the formatting of the name. Here's what the contents of the .jar file looks like:
I would like to reference the AidcManager to do this I have tried:
compile "com.honeywell.aidc.AidcManager"
compile "com.honeywell.aidc:AidcManager"
compile "com.honeywell.aidc:"
Each time I get an error during my build saying something like:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not find com.honeywell.aidc:AidcManager:.
Searched in the following locations:
file:/Users/XXX/.m2/repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
file:/Users/XXX/.m2/repository/com/honeywell/aidc/AidcManager//AidcManager-.jar
https://jcenter.bintray.com/com/honeywell/aidc/AidcManager//AidcManager-.pom
https://jcenter.bintray.com/com/honeywell/aidc/AidcManager//AidcManager-.jar
file:/XXX/node_modules/react-native/android/com/honeywell/aidc/AidcManager//AidcManager-.pom
file:/XXX/node_modules/react-native/android/com/honeywell/aidc/AidcManager//AidcManager-.jar
file:/Users/XXX/Library/Android/sdk/extras/android/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
file:/Users/XXX/Library/Android/sdk/extras/android/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.jar
file:/Users/XXX/Library/Android/sdk/extras/google/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.pom
file:/Users/XXX/Library/Android/sdk/extras/google/m2repository/com/honeywell/aidc/AidcManager//AidcManager-.jar
Required by:
MyApp:app:unspecified
Can anyone point me in the right direction? Many thanks in advance.
Just copy .jar files in libs folder and import classes or packages from it in your .java file. There is no need to reference them in .gradle file.
Answering wayyyyyyy to late, but might be helpful to others who are looking for this answer,
just add .jar in libs, then add below into gradle
implementation files('libs/<YourJarName>.jar')
Or
Right click the jar and select "Add jar to project" option

Gradle Transitive Dependencies from Modules

Disclaimer:
I'm new to Gradle, have read a lot of docs, and I don't know whether my maven-style understanding is tripping me out, or whether it's the sleep dep (kids - don't ask), but I'm still not getting it.
Problem Background:
I have a project that consists of several modules.
One of the modules, let's call it data-structure defines a data structure
Another module, data-structure-fabsearch, defines an implementation for a data source for the data structure, and finally
A third module, fabsearch-common, defines some common data source classes (eg: connection management to a fabsearch data source etc).
The reason I've done it like this is because there's actually another module that also uses the fabsearch-common stuff.
Anyway, my data-structure-fabsearch build.gradle looks something like this:
dependencies {
compile project(:data-structure)
compile project(:fabsearch-common)
}
The fabsearch-common module declares depedencies for the fabsearch api (let's call it fabsearch-api-1.0.0).
So, the dependency tree for data-structure-fabsearch should look like this:
- data-structure-fabsearch
- data-structure
- fabsearch-common
- fabsearch-api-1.0.0
This was all working wonderfully last night. This morning I came to work and all of a sudden those dependencies don't resolve anymore. References to fabsearch-api-1.0.0 classes are no longer found.
What I've Tried
1. In the parent build.gradle:
project(':data-structure-fabsearch'){
apply plugin: 'java'
dependencies {
compile project(path: ':data-structure', configuration: 'compile')
compile project(path: ':fabsearch-common', configuration: 'compile')
}
}
I've tried this with and without the configuration setting.
2. In the data-structure-fabsearch build.gradle file, adding the configuration parameter.
3. Restarting IntelliJ
4. Clicking the refresh icon in the Gradle tool window (repeatedly)
5. Reading all about transitive dependencies in the Gradle user guides
6. Drinking tea (repeatedly)
None of the above work.
What I'm Expecting
I'm expecting that the fabsearch-common dependencies (the fabsearch-api jars) should also be included in the data-structure-fabsearch dependency tree. All references to fabsearch-api classes in data-structure-fabsearch should resolve etc etc.
My Question[s]
Whilst this is possible in Maven, is it possible in Gradle?
What do I have to do to get it to work?
How much sleep dep can you take without dying?
Many thanks for any help.
Turns out the problem wasn't gradle at all. The problem was IntelliJ.
It got it's knickers into a proper twist!
Solution:
1. Close the project in IntelliJ
2. Delete the .idea directory
3. Delete all .iml files and any other IntelliJ cra-useful files
4. Open project in IntelliJ, choose same directory. Problem disappears.

how to fix build.gradle for map application on android

I am developing a basic map example but I'm in trouble with android studio.
I got this error :
Error:(9, 0) Cause: startup failed:build file 'C:\Users\Tugrul\AndroidStudioProjects\ServiceTextSender\build.gradle': 9: expecting '}', found ':' # line 9, column 31.
compile ‘com.android.support:appcompat-v7:+’1 error
And here an error resource from my build.gradle file(dependencies :
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
compile ‘com.android.support:appcompat-v7:+’
compile ‘com.google.android.gms:play-services:3.1.36′
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
How can I fix this? Could you help me please? I'm in trouble with Android Studio. I'm in trouble with this error.
Regarding the error:
Looks like you are adding this to the wrong gradle file, should be added to the app's gradle file (the one in the app's folder), not the module (the top one). The top one specifies the gradle plugin to use and as you have it listed here, I'm assuming you are editing the wrong build-gradle file.
If you instead check in the same directory that contains the src-folder (not inside src) you will find the app's dependencies section where you can add that line.
And/Or you are just using the wrong characters should be ' and not ‘.
Suggested easier solution:
Create a new Google Map Activity using the template when starting a new project, or right click in your project package and choose (in the popup menu):
New -> Google -> Google Maps Activity
And everything will be set up for you.
I think your problem is character error. ' and ‘ are different.
Try change this :
classpath 'com.android.tools.build:gradle:1.3.0'
compile ‘com.android.support:appcompat-v7:+’
compile ‘com.google.android.gms:play-services:3.1.36′
to this
classpath 'com.android.tools.build:gradle:1.3.0'
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:3.1.36'
Edit:
These are gradle Files:
You need to add your
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:3.1.36'
}
lines to Module Specific build.gradle file shown as above.

How to solve conflict dependence with SKMaps.jar and guava

I'm working on a project which use OSM by Skobbler. my project needs to use a Google's library called guava. I have the SKMaps.jar placed on libs/SKMaps.jar, also another jars too. On the other side i got some dependencies like:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:19.1.+'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile group: 'com.google.guava', name: 'guava', version: '18.0'
}
This cause a conflict when you build the project, like this
Error:Execution failed for task ':app:dexPlatoDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/home/opt/android-studio/sdk/build-tools/21.1.0/dx --dex --no-optimize --output /home/alex/Android/proy/app/build/intermediates/dex/plato/debug --input-list=/home/alex/Android/proy/app/build/intermediates/tmp/dex/plato/debug/inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/common/annotations/Beta;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302)
at com.android.dx.command.dexer.Main.run(Main.java:245)
at com.android.dx.command.dexer.Main.main(Main.java:214)
at com.android.dx.command.Main.main(Main.java:106)
As we can see here the conflict arise because SKMaps has a dependence from
Lcom/google/common/annotations/Beta;
And I say SKMaps because i have been doing some tests on a side project to detect which jars has conflicts, and SKMaps.jar and guava combinations has this conflict. Now here is my question:
How can i exclude the guava dependence from SKMaps.jar?
or
Is possible to aisle both jars to not cause conflict?
Thanks
There are 2 options, the first being easier than the second one:
Deleting the Google package from the already compiled 'SKMaps.jar' file
make a backup of the SKMaps.jar file
open the .jar file with an archive reader, for ex. 7zip
from the 3 folders, enter 'com' -> then select the google folder & delete it
close the archive saving it automatcally, there will be no error after this
*BUT you have to ensure the availibility of the Guava library along with the SKMaps.jar in the project (have it included in the lib folder)
Write a gradle script which decompiles the SKMaps.jar and recompiles it with the com/google/ folder excluded (which is basically the same thing as the first option) and including the new jar in the build process
the difference is that this option will not modify the original SKMaps.jar but make a copy of it without the duplicate package
Applying either approach, the project will compile without any problems.

Categories