Android Studio doesn't recognize GSON Library - android

I am making an app in Android Studio and I need to use GSON library. I have downloaded gson-2.8.2-javadoc. And then I followed this way
File->New->New Module->Import .JAR/.AAR
Package->gson-2.8.2-javadoc->Finish
After that
File->Project Structure->app->Dependencies->Module
Dependency->gson-2.8.2-javadoc->OK
but Android Studio did not recognize the Gson. I googled and saw the this as solution :
dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
}
but this is not working too.
My gradle version was 4.4 and I upgraded it 4.9 , again this is not working too. Here is my build.gradle file:
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
}
allprojects {
repositories {
mavenCentral()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
By the way, I tried same ways different versions of Gson Library, it did not work as well. So, what is the reason for this issue? And What is the exact solution?

I think you may have to had
buildscript {
repositories {
mavenCentral()
}
}
allprojects {
repositories {
mavenCentral()
}
}
to your application build.gradle file. Then build and it will be good

At first, Make sure you removed gson MODULE from your Project.
Read How to delete a module in Android Studio ?
"File -> Project Structure" -> Select gson Module and then Click Red Mark Negative Sign.
FYI
You should use latest version.
implementation 'com.google.code.gson:gson:2.8.5'
Then Clean-Rebuild-Run.

Related

Dependency Failed to resolve: com.github.barteksc:android-pdf-viewer:2.8.1

I know that this has been asked before, but despite adding maven in repositories cant resolve this error. please help.
Following is my module level gradle file:
Following is my top level gradle file:
Try this:
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
or if you want a more stable version:
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
and add jCenter in your repositories:
allprojects {
repositories {
jcenter()
………..
}
}
Change your module/build.gradle script.
repositories {
google()
jcenter()
}
dependencies {
//....
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
}
To use the implementation DSL you have to update the gradle plugin for android in the top level file:
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
//
}
}
The fact that JCenter is deprecated will not prevent you from using this library.
"JFrog will keep JCenter as a read-only repository indefinitely. Customers and the community can continue to rely on JCenter as a reliable mirror for Java packages".
You can add jcenter in your settings.gradle file:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
So there is nothing to worry about.
If you really want to sleep good at night, you can try to clone the library itself from Github and make your own local copy.
I will take some time and work but if you manage to do it, you will have an offline local version of the library.
Maybe I will try to do it myself in the future.
Anyway just wanted to give you all the options.
I personally still use JCenter.

how to change default project structure dependencies

every time i create a new android studio project, the layout editor will be broken because the dependency com.android.support:appcompat-v7:28.0.0-alpha3 will spoil the layout editor with:
Render problem
Failed to load AppCompat ActionBar with unknown error.
Tip: Try to refresh the layout.
thanks to user JLC answer, the problem is solved, but if i create a new project, the problem will come back
so my question is how can i change it so that every new project will use dependency com.android.support:appcompat-v7:27.1.1 instead? thanks
If you are using Android Studio 3.0 and above then make sure you are using the same content in build.gradle like-
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
And for below Android Studio 3.0 and starting from support libraries 26.+ your project build.gradle must look like this-
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
For the detailed knowledge check the below docs-
Build Dependencies
Building Android apps
Configure your build

Android Studio importing Jetbrains Exposed

I am new to Android development. I have created a new project with Android Studio and need to import jetbrains exposed library into my existing project. When I tried to follow the exposed Getting Started guide to add the following into the build.grade file:
repositories {
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
dependencies {
compile 'org.jetbrains.exposed:exposed:0.10.2'
}
I get the following error:
Could not find method compile() for arguments [org.jetbrains.exposed:exposed:0.10.2] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I have been searching the internet for a couple of days now but still could not find out what's the problem.
This is the build.gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.40'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
repositories {
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
dependencies {
compile 'org.jetbrains.exposed:exposed:0.10.2'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Hopefully someone could help me.
Thanking in advance.
You should put this dependency in the module level build.gradle file - by default, this is in the app folder, as opposed to the project level build.gradle file, which is in the root of your project.
Also, you should use implementation instead of compile, since you're using a newer than 3.0 version of the Android Gradle plugin:
repositories {
maven {
url "https://dl.bintray.com/kotlin/exposed"
}
}
dependencies {
implementation 'org.jetbrains.exposed:exposed:0.10.2'
}

Why I have syntax error in IDE after adding a gradle project to another repo even when the building works?

I have a kotlin multiplatform project A setup for iOS and Android, it works well. It has a common module for sharing business logic, and platform-android and platform-ios module for implementing the platform API.
After I adding the common and platform-android module from project A to another Android project B, the Android Studio IDE reports tons of syntax error, but the codes build and run from Android studio without a problem.
The syntax looks like the kotlin-stdlib is not there while it's indeed in the build.gradle, otherwise it won't build.
For instance:
val filterMap = mutableMapOf<String, MenuFilter>()
Android studio will say Unresolved reference: mutableMapOf
Some facts:
common module has the problem
no problem for platform-android.
and of course, no problem when I use IDEA to edit project A
my build.gradle for common module looks like this:
apply plugin: 'kotlin-platform-common'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
sourceSets {
main.kotlin.srcDirs += 'main/'
test.kotlin.srcDirs += 'test/'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.kotlin
}
artifacts {
archives sourcesJar
}
kotlin {
experimental {
coroutines "enable"
}
}
The rootProject build.gradle in Android Studio is:
buildscript {
ext{
kotlin_version = '1.2.41'
anko_version = '0.10.4'
dagger_version = '2.15'
support_lib_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And this is the settings.gradle file for Android project B
def projectA_path = "path/to/projectA"
include(":common")
project(":common").projectDir = new File("$projectA_path/common")
include(":platforms:android")
project(":platforms:android").projectDir = new File("$projectA_path/platforms/android")
Even the two are from different projects, according to the setup here. Shouldn't the two just work? What am I missing here?
IDE version:
IDEA Ultimate 2018.1
Android studio 3.1.2
Try adding this in build.gradle
apply plugin: 'org.jetbrains.kotlin.multiplatform'

Kotlin Error : Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7

I installed the Kotlin plugin into my app (v. v1.1.1-release-Studio2.2-1) and then selected "Configure Kotlin in Project" I selected compiler and runtime version of 1.0.7. Kotlin updated my Gradle files. Now when I try to build in I get:
Error: A problem occurred configuring project ':app'.
Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7.
Required by:
MyApplication:app:unspecified
I'm not sure what I'm missing here.
replace
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
^
|
JRE = Java Runtime Environment
with
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
^
|
JDK = Java Development Kit
Since the version with jre is absolute , just replace and sync the project
Official Documentation here Thanks for the link # ROMANARMY
In Project level build.gradle use only this version
ext.kotlin_version = '1.3.31'
Remove other versions
This will only work with the latest version of android studio 3.4
UPDATE: Try to use the latest version of kotlin with latest Android studio to avoid an error.
The split of kotlin-stdlib into kotlin-stdlib-jre7 and kotlin-stdlib-jre8 was only introduced with Kotlin 1.1, that's why the dependency cannot be resolved, the package version simply does not exist.
It looks like the update to your project files failed at some point and set the Kotlin version to 1.0.7. If this is a new project and there's nothing holding you back from using 1.1.1, I'd switch to that. Your problem should be gone after doing this.
If you hereafter removing Jcenter from Gradle then add this to your gradle
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
So main point is that, add mavenCentral() and maven { url "https://jitpack.io" } in gradle
In "build.gradle" file, change current Kotlin version that line and press synk:
ext.kotlin_version = '1.1.1'
/// That will look like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
A new solution if you use Android Studio 3.2, I solved this issue by added mavenCentral() to build.gradle of the project:
buildscript {
ext.kotlin_version = '1.3.0'
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
}
You should add the line as this order, the credited is for this answer
Starting with Kotlin 1.1.2, the dependencies with group org.jetbrains.kotlin are by default resolved with the version taken from the applied plugin. You can provide the version manually using the full dependency notation like:
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
If you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions. Instead of kotlin-stdlib, use one of the following dependencies:
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
buildscript {
**ext.kotlin_version = '1.1.1'** //Add this line
repositories {
**jcenter()** //Add this line
google()
}
dependencies {
// classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
**jcenter()** //Add this line
google()
maven { url "https://jitpack.io" }
}
}
I have solved this issue using deselection of the Offline work option in Settings
This is what worked for me:
Using Gradle 4.8.1
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'}
}
allprojects {
repositories {
mavenLocal()
jcenter()
google()
maven {
url "$rootDir/../node_modules/react-native/android"
}
maven {
url 'https://dl.bintray.com/kotlin/kotlin-dev/'
}
}
}
If you are using Android Studio 3.2 & above,then issue will be solved by adding google() & jcenter() to build.gradle of the project:
repositories {
google()
jcenter()
}
Please check current version of your Kotlin in below path,
C:\Program Files\Android\Android Studio\gradle\m2repository\org\jetbrains\kotlin\kotlin-stdlib\1.0.5
change to that version (1.0.5) in project level gradle file.
You can see in your above path does not mentioned any Java - jre version, so remove in your app level gradle file as below,
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
build.gradle (Project)
buildScript {
...
dependencies {
...
classpath 'com.android.tools.build:gradle:4.0.0-rc01'
}
}
gradle/wrapper/gradle-wrapper.properties
...
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Some libraries require the updated gradle. Such as:
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines"
GL
In case a (transitive) dependency still uses the jre variant of the Kotlin library, you can force the use of the jdk variant with the help of a resolution strategy:
configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails details ->
details.requested.with {
if (group == "org.jetbrains.kotlin" && name.startsWith("kotlin-stdlib-jre")) {
details.useTarget(group: group, name: name.replace("jre", "jdk"), version: version)
details.because("Force use of 'kotlin-stdlib-jdk' in favor of deprecated 'kotlin-stdlib-jre'.")
}
}
}
}
}
After fixing the build.gradle version, It started working
4.0.0 to 3.5.0
Reload sdk maybe the problem is solved
Simple Steps:
Click File > Project Structure
Click Dependencies >
Find and Click org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21 (or whatever your current version is)
Under Details > update section, click [update variable][update
dependencies]
Click Ok
In my case problem solved by adding the following lines in build.gradle file:
allprojects {
repositories {
jcenter()
google()
maven { url "https://jitpack.io" } //remember to add this line
}
}

Categories

Resources