android studio cannot resolve import org.json.JSONObject - android

I used libgdx to build my project and I am having issues using the JSONObject class. When I add import org.json.JSONObject, it says it cannot resolve. How do I add that library to my project?
Here is what I have tried without success:
I downloaded the json-simple-1.1.jar and put it in core/build/libs folder. Could not use import.org.json.simple.JSONObject either.
When browsing the tree in Android Studio as "Packages" dropdown, I can see the classes I want under "Core -> Libraries -> org -> json" but I cannot add them to my project. I get a "cannot refractor, class is in a jar file" error.
Is there something I have not tried yet to solve this issue? I feel I will run into this again as I try to use other external libraries.
Thank you.

If you are using LibGdx, it does not come with a Gradle setup, making multi-project builds streamlined.
You need to add implementation 'org.json:json:<version>' to the dependencies block of your top-level build.gradle.
To find the latest version, see http://mvnrepository.com/artifact/org.json/json/
For example, using the latest version at the time of writing:
dependencies {
implementation 'org.json:json:20200518'
}

Related

How to access LibraryExtension in custom gradle plugin

I would like to write a custom gradle plugin that manipulates either com.android.build.gradle.AppExtension or com.android.build.gradle.LibraryExtension. The basic form of the plugin is:
class AndroidLibrary : Plugin<Project> {
override fun apply(project: Project) {
var lib: LibraryExtension = project.extensions.getByName("android") as LibraryExtension
lib.minSdkVersion = "26"
}
}
The problem is that the class LibraryExtension cannot be resolved. That class is contained in "com.android.tools.build:gradle:7.1.2". Note, there is no problem accessing either of these classes in a gradle.build.kts script file. I just can't access either of those classes from within a custom plugin like seen above. I've tried adding a dependency to com.android.tools.build.gradle 7.1.2 in the build script, but that doesn't work. I also tried adding it as a buildscript dependency, and that didn't help either. I also got the
com.android.tools.build:gradle jar file and added it as a dependency, but that too didn't work.
It seems to me to be reasonable to expect a custom plugin of being able to manipulate the android build settings but I just can't find a way to resolve either LibraryExtension or AppExtension from within the custom plugin project.
If someone knows what I need to do to resolve those two classes, that would be greatly appreciated. I'm currently under the impression, that this is an architectural limitation of with android's gradle plugin and that I really can't access the "android" build section using LibraryExtension or AppExtension from a custom plugin and dsl. If that is the case, that too would be useful to know.
check this answer for your question, it is gonna solve your problem.
Initially I was building the plugin using IDEA. I could build the project by using a "gradlew build" command. I noticed that I would get the following message whenever I tried to sync the project:
This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.1.1 or newer
I had been ignoring that issue since I could successfully build the project using gradle commands. Here's a link the explains what the message means: https://intellij-support.jetbrains.com/hc/en-us/community/posts/4405168877202-This-version-of-the-Android-Support-plugin-for-IntelliJ-IDEA-or-Android-Studio-cannot-open-this-project-please-retry-with-version-2020-3-1-or-newer-
One of the suggestions to this problem was to build the project in Android Studio instead of waiting for IDEA to incorporate later android build. I did that and that indeed fixed that issue. The additional side effect was that I could now build the plugin. I was able to add dependencies for both gradleApi and to com.android.build.gradle and was able to resolve AppExtension of LibraryExtension. I had added the same dependencies I when I was using IDEA IDE. I had been customarily building my custom gradle plugin using IDEA. This was my first custom gradle plugin that I was going to manipulate an "android" build configuration.

Class not resolved when implementing custom library in Gradle Android

I have created an android library and published it in Jitpack.
Currently, my build is getting success in Jitpack. but when I add the implementation URL in my project build Gradle gets success but when I try to use library classes it does not get resolved
In the external library tab located in the project structure, it does not have the library which I have implemented so might be .jar file is not getting generated.
Help me If you have any solution or any way to identify this issue.
If I understand correctly you say that the gradle build or 'project sync' after a change in the gradle files works fine. But you cannot use any of the code from your library.
Can you verify that your library is loaded in the External Libraries?
It would be something with your repository name in it and ends with '#aar' (eg. com.github.nickname:libraryname:version#aar)
If you cannot find it, please check if your library module build.gradle has the line below somewhere at the top of the file.
apply plugin: 'com.android.library'
The issue was the Repository was private so we need to provide an authentication token. The reference link is given below
https://jitpack.io/docs/PRIVATE/

Error when adding libraries on Android Studio

I'm trying to create an app for android (google Tango) that uses thrift to send some information to a client. I take a sample Tango app which runs without Thrift. Then when I add try to add some of the Thrift dependencies (like slf4j-log4j12-1.7.12.jar) I suddenly get errors like
error: package com.google.atap.tangoservice does not exist
I don't get what's wrong, it worked before...
Why does this happen, and how can I fix it?
Thank you
UPDATE
The way I added the library was by clicking right on App>new>module then I chose import jar, and selected the jar. Then I went to project structure and in dependencies I added the module of the jar I imported.
Adding it like this did not help me resolve the conflicts I had before:
import org.slf4j.Logger;
giving me the error "cannot resolve symbol 'Logger'".
And it also introduced the errors previously mentioned.
Have you tried adding a dependency to your build.gradle file like this:
dependencies {
compile 'com.google.atap.tangoservice'
}
If you are using local jar dependency then add it like this to the build.gradle:
dependencies {
compile files('libs/something_local.jar')
}

Importing library gives error in Android studio

I am new to Android Studio. I am trying to work using the android-map-utils library. But it is giving me the following error.
`Error:(64) A problem occurred evaluating project ':library'.
> No such property: sonatypeUsername for class: org.gradle.api.internal.project.DefaultProject_Decorated`
Can anyone help me with this issue and also tell me a easy way to import libraries in android studio?
I had a similar problem while I was migrating from eclipse. I resolved my issue by using the gradle method of including the library (as opposed to importing it as a module and using it as a dependency).
Add the following dependency to your Gradle build file:
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.3+'
}
source: http://googlemaps.github.io/android-maps-utils/#start
I wasn't able to comment.
Applying gradle dependency as link doesn't allow us to view it's source properly.
So if you are interested in viewing source too, in Android Studio itself
You should try adding library as a module project, i.e.,
File -> New -> Import Module -> (Dir of Library)
after importing, you'll face an error, which can be resolved from - Adding Maps Utils Module

Using an android-library with gradle in Android Studio

I've been trying to do get the gamebaseutil into my project.. I switched from eclipse to android studios 'cause it seemed impossible to import it in Eclipse.. Now I don't understand the importing of android studios very well so I'm still trying to import and use it..I have really no idea what i do wrong or what i have to do... I'm pretty sure I imported GameBaseUtil into android studios but now I want to make use of it in my project.. I have added " include '(:libraries):BaseGameUtils', ':Name' " to my settings.gradle.. and when I run it, there are no gradle build errors but still I can't instantiate "GameHelper mHelper;" for example. Or "extends GameBaseActivity"
Please tell me what to do. I'm really desperate 'cause I have been reading and trying the whole day...
Including the lib in gradle.settings is the first step.
The next step is to add the lib as a dependency of your main project (in the build.gradle):
If you have the sources of the library :
dependencies {
compile project(':GameBaseUtil')
...
}
If your library is in a maven repository :
dependencies {
compile '<groupid>:<artifactid>:<version>'
...
}
there other ways to define a dependency : here

Categories

Resources