How to add Library in android studio using build.gradle file
Here is my build.gradle(Module) file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.akshay.popupdemo"
minSdkVersion 19
targetSdkVersion 23
versionCode 7
versionName "1.0.7"
multiDexEnabled true
}
packagingOptions {
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.scottyab:secure-preferences-lib:0.1.3'
compile 'com.google.code.gson:gson:2.3'
compile 'com.esri.arcgis.android:arcgis-android:10.2.7'
}
and my build.gradle(project) file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
allprojects {
repositories {
jcenter()
maven {
url 'http://dl.bintray.com/esri/arcgis'
}
}
}
I Got The Following Error
D:\Yogesh_Kadam_Projects\Android-Projects\Current Projects\PopUpDemo\app\build.gradle
Error:Error:line (36)Failed to resolve: com.scottyab:secure-preferences-lib:0.1.3
Error:Error:line (37)Failed to resolve: com.google.code.gson:gson:2.3
Error:Error:line (38)Failed to resolve: com.esri.arcgis.android:arcgis-android:10.2.7
I know how to add using library Folder and then adding dependencies using project structure dialog box.
How to add library using build.gradle file ?
What Setting should i made into android studio to download library by its own?
Please Explain.
Log IMAGE
#yogesh
try to add dependencies using Module Setting -> Dependencies -> Library Dependency
paste dependency path on search area
for ex. com.google.code.gson:gson and this will search & rectify the dependency with proper version
Related
I am creating a blank project in which I add mapbox navigation package compile 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3' in the gradle file. It shows error in appcompat file like this:-
Error:Failed to resolve: com.android.support:appcompat-v7:26.1.0
eventhough I use different version of Appcompat file. Before adding navigation package, everything was fine. Can someone please help me to find out this weird error.
Here is my app's gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.example.aadhilahmed.test7"
minSdkVersion 15
targetSdkVersion 25
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(dir: 'libs', include: ['*.jar'])
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:25.3.1'
compile 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Specify:
compile ('com.mapbox.mapboxsdk:mapbox-android-navigation:0.6.3') {
transitive = false;
}
Try this.Starting from version 26 of support libraries make sure that the repositories section includes a maven section
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
For Gradle build tools plugin version 3.0.0
allprojects {
repositories {
jcenter()
google()
}
}
I created an Android library named "core" that use the Logger library (https://github.com/orhanobut/logger).
Here its build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
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(dir: 'libs', include: ['*.jar'])
compile 'com.orhanobut:logger:1.15'
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
}
Then I build a .aar of core library.
I add this library as a dependency into my application, by putting the .aarcore files in the libs folder.
That's the build.gradle of my application :
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "com.package.test"
minSdkVersion 16
targetSdkVersion 25
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
}
dependencies {
compile 'com.mypackage:core:1.0#aar'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:multidex:1.0.1'
}
As you can see I have multidex activated.
It compile just fine but at runtime, at the first call to Logger I get an exception:
stack=java.lang.NoClassDefFoundError: Failed resolution of: Lcom/orhanobut/logger/Logger;
Even if I set transitive = true
compile (com.mypackage:core:1.0#aar) {
transitive=true
}
it doesn't work.
Thank you !
I had to do a couple of things in concert to get it to work.
1) In your library project
Add the following to your project-level gradle file:
buildscript {
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
Be sure to use the right version of the plugin for your gradle version, check here.
Add the following to your module-level gradle file:
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.YourPackage'// Doesn't have to be github, just an example
Also in this file, make sure your dependencies (the ones you want to be transitive) use api instead of implementation if you are using Gradle 3.4+. If you are using a version of Gradle <3.4, compile is the way to go. Example:
dependencies {
api 'com.google.android.gms:play-services-location:15.0.1'
}
2) In your app project (which uses the library)
Add the following to your module-level gradle file:
dependencies {
implementation('com.github.YourPackage:yourRepo:version#aar') {
transitive=true
}
}
Add the following to you project-level gradle file (but this will depend on where your library is served from, for me it's jitpack):
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
credentials { username 'yourAuthKey' }// Only for private repositories
}
}
}
Note: You shouldn't add the auth key as a string in build.gradle, put it in a property in your gradle.properties file.
Note 2: JitPack allows you to use for example development-SNAPSHOT as a version number for a gradle dependency. Android Studio caches these dependencies and will not re-download it when you push to your development branch. To overcome this, use commit hashes as version numbers during development or clear (delete) the cache files, located on Windows at ~/.gradle/caches/modules-2/metadata-x.xx/descriptors/com.github.YourPackage/yourRepo. (Needles to say, I learned this the hard way).
:app:mergeDebugAssets
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK kotlin/internal/internal.kotlin_builtins
File1: /Users/KD/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-compiler-embeddable/1.0.4/172b43fbc03b521fed141484b212d6725fa671a9/kotlin-compiler-embeddable-1.0.4.jar
File2: /Users/KD/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-runtime/1.0.4/8e25da5e31669f5acf514bdd99b94ff5c7003b3b/kotlin-runtime-1.0.4.jar
My build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.app2par.ctime"
minSdkVersion 16
// minSdkVersion 21
// targetSdkVersion 23
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled false
// multiDexEnabled true
}
dexOptions {
preDexLibraries true
javaMaxHeapSize "2g" // Use gig increments depending on needs
incremental true
}
buildTypes {
debug {
minifyEnabled false
// testCoverageEnabled true
// ext.betaDistributionReleaseNotes = getCrashlyticsBetaMessage()
// ext.betaDistributionGroupAliases = 'team'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
// dataBinding {
// enabled = true
// https://code.google.com/p/android/issues/detail?id=187443&q=attachments%3D0&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
// }
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':firebasesync')
compile project(':liboid')
compile project(':cloudtimemodel')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude group: 'com.android.support'
}
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// compile('com.crashlytics.sdk.android:crashlytics:2.5.5#aar') {
// transitive = true;
// }
// compile 'com.google.android.gms:play-services:5.0.89'
// compile 'com.google.android.gms:play-services:7.0.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// compile 'com.firebase:firebase-client-android:2.0.3.+'
// compile 'com.google.android.gms:play-services-safetynet:8.3.0'
// compile 'com.google.android.gms:play-services-auth:8.3.0'
compile 'com.google.android.gms:play-services-plus:7.0.0' // play-services-plus:7.0.0 : office-mover
// compile 'com.google.android.gms:play-services-auth:8.3.0' // play-services-auth:8.3.0 : ShoppingList++
// compile 'com.google.android.gms:play-services-identity:7.0.0'
}
buildscript {
// ext.kotlin_version = '1.0.0-rc-1036'
ext.kotlin_version = '1.0.4'
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// classpath 'org.ajoberstar:grgit:1.1.0'
// classpath 'io.fabric.tools:gradle:1.+'
// classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.5.0-x'
}
}
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
How to fix or diagnose this?
You should remove compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" from your dependencies section and move it to buildscript { dependencies { ... } }.
You did put compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" in the wrong build.gradle file
You may use kotlin plugin's build-in converters to deal with this. According to Kotlin Docs:
Configuring Kotlin in the project
When adding a new Kotlin file, IntelliJ IDEA (and Android Studio)
automatically prompts us as to whether we'd like to configure the
Kotlin runtime for the project. However, currently, converting
existing Java file does not prompt this action. Therefore we have to
invoke it manually (via Find Action):
We are then prompted for the version of Kotlin. Choose the latest
available from the list of installed versions.
After we configure Kotlin, build.gradle file for the application
should be updated. Now we can see that apply plugin: 'kotlin-android'
and the dependencies were added.
(For more details how to set up gradle for your project, please check
Using Gradle)
The last thing to do is to sync the project. We can press Sync Now
in a prompt or invoke an action Sync Project with Gradle Files.
From: https://kotlinlang.org/docs/tutorials/kotlin-android.html
Check link above for more information.
Hope it will help.
you need to apply only one plugin, in your case apply plugin: 'kotlin-android-extensions' and only compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" this dependency. it worked for me
I have new clear android app. I want to add volley to my app to make HTTP request. I am developing app with Android studio using Gradle. Here is screenshot for my app:
Ass you see i am going to add volley to my app adding this line of code com.mcxiaoke.volley:library-aar:1.0.0 to build.gradle file
I tried to add Retrofit too. But it gives same type of error: Failed to resolve: bla bla bla.
So i think in my situation gradle can not sync any other library different than com.android......
And i have tried add this lines of code to my build.gradle too. but nothing is changing
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
repositories {
mavenCentral()
}
}
Please advise
Edit: After comments added build.gradle files for project and for module.
Project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
allprojects {
repositories {
jcenter()
}
}
Module:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "domain.testgradle"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
Volley is now published by the Android Open Source Project on jCenter:
dependencies {
compile 'com.android.volley:volley:1.0.0'
}
You can use
com.mcxiaoke.volley:library-aar:1.0.1
Or its latest version (Not aar)
compile 'com.mcxiaoke.volley:library:1.0.19'
And set
buildToolsVersion "23.0.1"
and use
'com.google.code.gson:gson:2.3'
Add this
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:+'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
I import project from eclipse to android studio, and got above message after Sync project with gradle files. I am new with android studio, don't know how to solve this error.
Following is the Gradle file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId "com.s5.selfiemonkey1"
minSdkVersion 14
targetSdkVersion 14
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile 'com.google.android.gms:play-services:+'
compile 'com.google.code.gson:gson:2.2.2'
compile files('libs/AF-Android-SDK.jar')
compile files('libs/AndroidSwipeLayout-v1.1.8.jar')
compile files('libs/FlurryAnalytics-5.3.0.jar')
compile files('libs/android-viewbadger.jar')
compile files('libs/commons-codec-1.4.jar')
compile files('libs/google-play-services.jar')
compile files('libs/okhttp-2.5.0.jar')
compile files('libs/okio-1.6.0.jar')
compile files('libs/parcelgen.jar')
compile files('libs/scribe-1.1.2.jar')
}
Build Gradle from Project
// 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.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
Please advice. Thanks in advance.
Since GSon is in mavenCentral , you have to add mavenCentral() in buidscripts phase.
Gson's latest version is currently 2.3.1 https://github.com/google/gson/releases
Try with compile 'com.google.code.gson:gson:2.3.1'
you have to add the Gson library in the dependency using project structure, now to update the gradle, for the add the .jar file to your build.gradle file like
dependencies {
compile files('libs/gson-2.2.4.jar')
}
if it dosent works try changing it to
dependencies{
compile 'com.google.code.gson:gson:2.2.+'
}
make sure you are also connected to the internet so that jars will be downloaded online through maven repo.