How Do you add a dependecy to an apps gradle file? - android

Hey guys recently I was reading an answer and I came across this
Add the following dependency to your app's Gradle file and run the application.
debugCompile 'com.amitshekhar.android:debug-db:1.0.0'
My question is this where do I paste this to?
I copied This to the build.gradle file in the android studio and I got a lot of errors saying
Error:(9, 0) Gradle DSL method not found: 'debugCompile()' Possible causes:<ul><li>The project 'DB' may be using a version of the
Android Gradle plug-in that does not contain the method (e.g.
'testCompile' was added in 1.1.0). Upgrade
plugin to version 2.3.3 and sync projectThe project 'DB'
may be using a version of Gradle that does not contain the method. Open Gradle wrapper fileThe
build file may be missing a Gradle plugin. Apply Gradle plugin
Any Pointers on how to add a dependency would be appreciated! PS The official documentation just confuses me even more so please keep the answer simple Thanks :)

You need to add the dependency inside the dependencies block in your module build.gradle not in your project build.gradle.
You can see the sample how adding the dependency from UniversalMusicPlayer sample. Here the sample of module build.gradle without the license:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.android.uamp"
minSdkVersion 17
targetSdkVersion 26
versionCode 2
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
abortOnError true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
provided 'com.google.android.wearable:wearable:2.0.3'
compile 'com.google.android.gms:play-services-cast-framework:11.0.1'
compile 'com.google.android.support:wearable:2.0.3'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:mediarouter-v7:26.1.0'
compile 'com.android.support:leanback-v17:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.google.android.exoplayer:exoplayer:r2.5.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support:support-annotations:26.1.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}
Take a look at the dependencies block:
dependencies {
...
}
You need to add debugCompile 'com.amitshekhar.android:debug-db:1.0.0'
there. So it will become:
dependencies {
...
debugCompile 'com.amitshekhar.android:debug-db:1.0.0
}

Related

Unable to merge dex when a module is imported

I know there are many questions on this, but I tried the solutions of most of them and neither of them worked.
I have an app in which I have imported a module. When I try to run the app, I am getting Unable to merge dex exception.
build.gradle (app module)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.meditab.imspatient"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2' // Constraint layout
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" // Kotlin
// Support libraries
implementation "com.android.support:appcompat-v7:$appcompat_version"
implementation "com.android.support:support-v4:$appcompat_version"
compile project(path: ':commonutils')
}
build.gradel (imported module)
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
buildToolsVersion '27.0.2'
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.jetbrains:annotations-java5:15.0'
compile 'com.jakewharton:butterknife:8.8.1'
// Support libraries
compile "com.android.support:appcompat-v7:$appcompat_version"
compile "com.android.support:design:$appcompat_version"
// Rx related libraries
compile 'io.reactivex:rxjava:1.1.1'
compile 'io.reactivex:rxandroid:1.1.0'
//compile 'com.netflix.rxjava:rxjava-core:0.+'
// Networking related libraries
compile 'com.squareup.okhttp3:logging-interceptor:3.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-jackson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'
compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
compile 'com.google.android.gms:play-services-base:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
compile 'com.estimote:sdk:0.10.+#aar'
}
build.gradle (project level)
buildscript {
ext.kotlin_version = '1.2.21'
ext.appcompat_version = '27.0.2'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Needed for the common utils module
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I have tried deleting the .gradle folder, cleaning the project and rebuilding it. Nothing works. I also tried using implementation instead of compile in the build.gradle of the imported module.
Any help will be appreciated.
Thanks in advance.
It seems that 2 libraries you're declaring a dependency on are causing duplicate entries.
The problem is that you can't have the exact same class or annotation declared by 2 different libraries. If they were different versions of the same library probably the one with the highest version would be picked, but if they are completely different libraries, there is no way to tell which implementation should be included in the dex file.
The first one is:
'com.netflix.rxjava:rxjava-core:0.+'
Why would you use this? You have already defined a dependency on RxJava ('io.reactivex:rxjava:1.1.1') and they probably have many classes in common, the first that comes to my mind and that I checked is rx.Observable. The last update for that repo is Novermber 2014, more than 3 years ago. Besides conflicts, this fact alone should be a very good reason not to use it.
The second problem is:
'org.jetbrains:annotations-java5:15.0'
I imagine that you're importing this in order to get org.jetbrains.annotations.Nullable, this is already available in your project because Kotlin stdlib has a dependncy on org.jetbrains:annotations that also declares it. Hence the conflict.
Remove these 2 dependencies, you most likely don't actually need them anyway, and the project compiles.
I'm not 100 percent sure but I think you need to enable multidex in your app. Check this link : Enable Multidex
Just trying to help.
In my previous project which has multiple modules If I enable Proguard I was getting the same error.
That error frustrating and In my case I solved problem after inspecting
the project dependency graph(Using gradle to find dependency tree may help you while finding dependencies)
Play service's different version was causing in my case

Android Studio can't resolve Espresso 3.0.0

According to the Android Espresso documentation to date:
Add Espresso dependencies
To add Espresso dependencies to your project, complete the following steps:
Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.
Add the following lines inside dependencies:
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
androidTestCompile 'com.android.support.test:runner:1.0.0'
I created a new project and the generated app/gradle file was like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.app.test"
minSdkVersion 24
targetSdkVersion 26
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:26.+'
testCompile 'junit:junit:4.12'
}
When I change it to the following:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.app.test"
minSdkVersion 24
targetSdkVersion 26
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.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
// App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:1.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
}
I get the following errors:
Error:(29, 24) Failed to resolve: com.android.support.test:runner:1.0.0
    Install Repository and sync project
Error:(30, 24) Failed to resolve: com.android.support.test.espresso:espresso-core:3.0.0
    Install Repository and sync project
I have tried clicking on the "Install repository and sync project" link but nothing happens. I have also tried looking through the SDK manager, but I can't really see anything.
As the solution from comment is solving the problem I am adding it as an answer for others:
Be sure to add Google's maven link to main build.gradle file:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Simply,
Adding google() into allprojects > repositories will do the trick here...
allprojects {
repositories {
google()
jcenter()
}
}
you can downgrade the version, somehow sometimes the targeted version is unavailable from the repo.
use IDE-UI to select the latest version:
Right click on your project folder, select Open Module Settings
search for the package you want in the dependencies, for example
This way, you'll never get an un available version
(of course remember to set proper repo in build.gradle, too)
use these versions
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

Android NoClassDefFoundError for library's class inside a dependency

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).

Error:(28, 13) Failed to resolve: com.squareup.okhttp3:okhttp:3.2.0

I have a problem with android studio.
I have added the .jar file of okhttp from this link : https://github.com/square/okhttp to my libs directory on my android studio. and when I try to add this line : compile 'com.squareup.okhttp3:okhttp:3.2.0' in the dependencies in build.gradle(Module:app), after trying to syncing it I get error 28, 13. Actually after researching I found out that I can't compile anything in dependencies and sync it remember that I have checked my "android SDK build tool" is installed.
hear is the whole of my code in build.gradle(app) directory:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.kasra.stormy"
minSdkVersion 14
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'], )
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile files('libs/okhttp-3.2.0.jar')
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
I'll be very glad and thankful if someone help me fix this problem.
Thank you very much:)
Either your internet is bad and you can't download that file from JCenter, or you are just missing a repositories section in your gradle file.
apply plugin: 'com.android.application'
repositories {
jcenter()
}
Also, if you are going to be compiling using gradle instead of a JAR file, then delete the file libs/okhttp-3.2.0.jar and remove the line that says compile files('libs/okhttp-3.2.0.jar').
It is also worth mentioning that you don't need any lines that start with compile files('libs/ because compile fileTree(dir: 'libs', include: ['*.jar'], ) already includes all JAR files within the libs/ folder.
It may help someone. In my case after update com.squareup.okhttp3 to the latest version, I got this error in Android Java libaray. Then I update the Java version to 1.8 which solved my problem.
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
you should change the build.gradle(app):
use
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
instead of
compile 'com.squareup.okhttp3:okhttp:3.2.0'
or just follow the guide as mentioned here.
Just Replace the dependencies with folowng dependencies and they will work for sure.(100%).
if in future this issue comes again then go to https://square.github.io/okhttp/ and update your dependencies with latest one
//define any required ok http without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
//define a BOM and its version
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
Just remove compile files('libs/okhttp-3.2.0.jar')
I solved my problem by editing my build.gradle as below:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion '25.0.0'
defaultConfig {
minSdkVersion 21
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.txt'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile project(':library')
//compile('com.squareup.okhttp3:okhttp:+') {
// exclude group: 'org.json'
//}
//replaced as below:
compile 'com.squareup.okhttp3:okhttp:3.9.1'
}
Check your code in app.gradle
apply plugin: 'com.android.application'
repositories {
jcenter()}
OR
make your compile okhttp to offline mode, next you uncheck offline mode in setting -> gradle. and try it again
You will not need this
compile 'com.squareup.okhttp3:okhttp:3.2.0'
rather you will need
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
As of today the 30th of September 2020
Check https://square.github.io/okhttp/ for documentation

Provided dependencies can only be jars

I have an android projct in Android Studio, was all previously working the last time I used it, however, I've upgraded Android Studio and now I am getting a weird problem.
In the error output in Android Studio I have the following:
Warning: Project MyApp: provided dependencies can only be jars. com.google.android.gms:play-services.6.5.87 is an Android Library
Warning: Project MyApp: provided dependencies can only be jars. com.MyCompany.MyLibrary:aar:1.0 is an Android Library.
Below is my build.gradle file.
apply plugin: 'android'
android {
compileSdkVersion 21
buildToolsVersion "20.0.0"
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
defaultConfig {
//applicationId "com.MyCompany.MyApp"
minSdkVersion 14
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
debuggable false
}
}
repositories {
mavenLocal()
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
compile 'com.MyCompany:CritiMon:1.0'
compile 'com.MyCompany:Library:1.1'
provided 'com.MyCompany:MyLibrary:1.0'
compile 'com.MyCompany:NavigationDrawerManager:2.1'
provided 'com.google.android.gms:play-services:+'
compile files('libs/ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar')
}
Your provided dependencies can only be jars, your custom Android Libraries in your project should be compile at build time.
Change this:
provided 'com.MyCompany:MyLibrary:1.0'
provided 'com.google.android.gms:play-services:+'
to
compile 'com.MyCompany:MyLibrary:1.0'
compile 'com.google.android.gms:play-services:+'
I restore the Android Plugin Version from 1.1.0 to 1.0.0 and it work.
You can do it by the order:
Right-click your project.
Select `Open Module Settings`
Select `Project`
Change the `Android Plugin Version`

Categories

Resources