Cannot Import GSON/JSON in Android Studio Using Maven - java

I'm having a problem importing com.google.gson using Maven. Right now, I'm getting the following errors:
Error:(24, 17) Failed to resolve: junit:junit:4.12
and
Error:(22, 13) Failed to resolve: com.google.code.gson:gson:2.5
Here is the build.gradle for the Project:
buildscript {
repositories {
jcenter()
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
repositories {
jcenter()
mavenCentral()
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and here is the build.gradle for the "Module: App":
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.name.testversion"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.5'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.3.0'
}
Please tell me what to do to fix these problems and successfully import "com.google.code.gson:gson:2.5" into Android Studio to use with this project.
Also, just so you know, I'm newer to Android Studio, just to put that into perspective, and this is my first time using Maven in Android Studio, but not first time using Maven in general.

Change your project gradle to this one
//Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

On your Project build.grade try using:
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Instead of just repositories {...}

Related

cannot resolve symbol gson

I have big troubles using the gson library. When building the project i get:
"Cannot resolve symbol gson"
I have googled for hours but no solution seems to work. Here is my build.gradle file:
android {
compileSdkVersion 24
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "com.example.jasminkrhan.vaktija"
minSdkVersion 24
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.8.2'
}
And also my build.gradle file for the project:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter {
url "http://jcenter.bintray.com/"
}
}
repositories {
maven {
url "http://repo1.maven.org/maven2"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter {
url "http://jcenter.bintray.com/"
}
}
repositories {
maven {
url "http://repo1.maven.org/maven2"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Does someone see something wrong with my code? Why does not android studio find the library? I have already tried to restart Android Studio but that doesn't help.
compile is outdated. Please use implementation
implementation 'com.google.code.gson:gson:2.8.5'
You need these in Project Level build.gradle
buildscript {
repositories {
google()
jcenter()
}
.....
allprojects {
repositories {
google()
jcenter()
}
}
Try to write the repositories all in one, and use the functions included for Google dependencies and jcenter already, instead of providing the URL. Like so:
buildscript {
...
repositories {
google()
jcenter()
}
}
And also:
allprojects {
repositories {
google()
jcenter()
}
}
It's also important that you start using the implementation tag instead of compile in gradle, as its being deprecated:
implementation 'com.google.code.gson:gson:2.8.5'

Execution failed for task for ':app:transformClassesWithMultidexlistForDebug'

hi when i compile my project the error below shows up
Execution failed for task for ':app:transformClassesWithMultidexlistForDebug'.
java.io.IOException: Can't write [C:\Users\user\app\app\build\intermediates\multi-dex\debug\componentClasses.jar] (Can't read [C:\Users\user\.gradle\caches\transforms-1\files-1.1\support-core-ui-25.2.0.aar\8bd3003da78946d664f3d05c69c73bfa\jars\classes.jar(;;;;;;**.class)] (Duplicate zip entry [classes.jar:android/support/v4/view/ViewPager$2.class]))>
to note i used all the answers from this Thread
+delete .Gradle Folder
+Added multiDexEnabled true
but it didn't work for me
my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "net.amiapp.candiesstory"
minSdkVersion 15
targetSdkVersion 27
multiDexEnabled true
ndk {
moduleName "player_shared"
}
}
sourceSets {
main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] // レガシー ndk-build サポートを無効にする
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dependencies {
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.google.android.gms:play-services-ads:11.8.0'
implementation 'com.google.android.gms:play-services-games:11.8.0'
implementation 'com.google.android.gms:play-services-plus:11.8.0'
compile files('libs/dagger-1.2.2.jar')
compile files('libs/javax.inject-1.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/support-v4-19.0.1.jar')
}
}
here the project.gradle
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.1.1'
}
}
allprojects {
repositories {
mavenCentral()
google()
maven { url "https://maven.google.com" }
jcenter()
maven { url "https://maven.google.com" }
}
}
any help is appreciated , thanks in advance
Remove these lines and delete these jar files
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/support-v4-19.0.1.jar')
Your compileSdk is set to 27, not 19, and NineOldAndroids is a deprecated library for several years
You should also find the proper implementation dependencies for javax.inject and dagger rather than using jars
You can also remove these repos as jcenter includes Maven Central
mavenCentral()
maven { url "https://maven.google.com" }
maven { url "https://maven.google.com" }

Android studio error (17, 0) could not find method maven()

Project Gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:3.1.0' // google-services plugin
}
repositories {
google()
}
}
allprojects {
// ...
dependencies {
// ...
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
Module Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "com.example.example"
minSdkVersion 19
targetSdkVersion 23
}
splits {
abi {
enable true
reset()
include 'x86', 'armeabi-v7a'
universalApk true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.google.firebase:firebase-core:11.2.2'
compile project(':rateMyApp')
compile files('libs/android-async-http-1.4.6.jar')
compile files('libs/com-uvxghdmx-ucuwreks167365.jar')
compile files('libs/org.apache.http.legacy.jar')
compile files('libs/universal-image-loader-1.9.3.jar')
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.balysv:material-ripple:1.0.2'
compile 'com.google.android.gms:play-services-ads:11.2.2'
}
apply plugin: 'com.google.gms.google-services'
Getting Error
Error:(17, 0) Could not find method maven() for arguments [build_2rpdxydj6arlyq4m2v87kde3g$_run_closure1$_closure2$_closure3#70ca8e63] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Please Help Me...
You can work this one around like by adding the mavenCentral() to the repositories, then add the code to the repositories below the mavenCentral() line, and then resync:
allprojects {
repositories {
jcenter()
mavenCentral()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
this should remove the conflict with the sync.

Error:(29, 17) Failed to resolve: junit:junit:4.12 Android Studio 2.1.2

I'm using Android Studio 2.1.2 and it is giving error "Error:(29, 17) Failed to resolve: junit:junit:4.12" Similar error for any third party library. I have tried removing and adding the junit library from Project Settings but still it giving same error. I'm not sure whether this proxy issue? I have checked connection from Studio for maven link and it is showing successful. Following is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
defaultConfig {
applicationId "com.example.administrator.myapplication"
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
}
Project Level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
repositories {
jcenter()
mavenCentral()
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Please let me know if I'm missing something and how to resolve this.
After lot of efforts I was able to resolve this issue. Issue lies with the proxy settings, I have added following lines in gradle.properties file and gradle synced successfully with dependencies resolved
systemProp.http.proxyHost=x.x.x.x
systemProp.https.proxyPort=xxxx
systemProp.https.proxyHost=x.x.x.x
systemProp.http.proxyPort=xxxx

Use ZXing library on an Android Project

I'm trying to build a project with the ZXing library. I used the instructions in this post to integrate the library into my Android project and I believe I have done that successfully. However, when I try to build the entire project, there is an error in the Gradle build that refers to a dependency or something similar. However, I think I've already solved all the issues with dependencies.
The returned error in Gradle :
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:prepareDebugDependencies'.
The message is
Dependency QRR:android:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency.
Here is the full Gradle log using Stacktrace.
EDIT:
In this project, I used the full ZXing library, not the Embedded or the Minimal.
After the download of the full repo of ZXing here, I've added the android folder as a new Module of the project. I've added the dependency on the Gradle file. Then, I've dowloaded the core on the Maven repo, then I imported it as JAR Library. I've added the dependency in the android:build-gradle.
The app:build.gradle is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.mehdi.qrr"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile project(':android')
compile 'com.google.zxing:core:3.2.1'
}
repositories {
jcenter()
}
The Project:build-gradle is:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
repositories {
mavenCentral()
maven {
url "http://dl.bintray.com/journeyapps/maven"
}
}
The android:build-gradle (Gradle file of the ZXing project):
apply plugin: 'com.android.application'
android {
signingConfigs {
}
compileSdkVersion 21
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.google.zxing.client.android"
minSdkVersion 15
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.google.zxing:core:3.2.1'
}

Categories