After update of Android Gradle plugin and Android Studio, if you getting error or warning something like this :
Configuration ‘compile’ is obsolete and has been replaced with ‘implementation
then don’t worry. Just relax, here is solution to get rid of this:
WARNING: Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html Affected Modules: app
The Error in Android Studio.
Configuration ‘androidTestCompile’ is obsolete and has been replaced with ‘androidTestImplementation’ and ‘androidTestApi’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Configuration ‘androidTestApi’ is obsolete and has been replaced with ‘androidTestImplementation’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Configuration ‘testCompile’ is obsolete and has been replaced with ‘testImplementation’ and ‘testApi’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Configuration ‘testApi’ is obsolete and has been replaced with ‘testImplementation’.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Solution
1) Use the new dependency configurations in gradle file Replace ‘compile’ with an ‘implementation’. For example:
dependencies { compile 'com.android.support:support-v4:27.0.3' }
this should be like :
dependencies { implementation 'com.android.support:support-v4:27.0.3' }
2) And Replace ‘testCompile’ with an ‘testImplementation’. For example:
testCompile 'junit:junit:4.12'
this should be like :
testImplementation 'junit:junit:4.12'
3) And Replace ‘androidTestCompile’ with an ‘androidTestImplementation’. For example:
androidTestCompile'junit:junit:4.12'
this should be like :
androidTestCompileImplementation 'junit:junit:4.12'
3) For library replace compile
with api
4) Upgrade classpath com.google.gms:google-services to classpath ‘com.google.gms:google-services:3.2.0’ in file in build.gradle (Use latest one)
5) File -> Invalidate Cache
Now its all done. Just run your project now.
If above solution not working, then try this one:
1) Close the project.
2) Delete .gradle folder.
3) Open again the project
Now it will work, I’m sure.
More About this Android Studio Error
The old way of declaring dependencies in build.gradle was as below :
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile files('libs/example_dependency.jar') compile 'com.android.support:support-v4:21.+' }
Since gradle version 3, compile has deprecated and has been replaced with implementation or api. The new way of declaring dependencies would like below :
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' implementation files('libs/example_dependency.jar') implementation 'com.android.support:support-v4:21.+' }
Now the question arises whether to use implementation or api?
Lets see the difference between and use case of both.
Implementation Vs Api in Android Gradle plugin 3.0
While using Android Gradle plugin 3.0 in your project, you might have noticed that compile
keyword has been now deprecated in favour of implementation
and api
. Let’s understand both of them with an example.
Sample application(Kotlin) can be found here.
Let us assume a project with four library modules.
- LibraryA
- LibraryB
- LibraryC
- LibraryD
For which the dependency tree looks like this:
All Library modules contain a simple class file.
LibraryD:
class ClassD { fun tellMeAJoke():String{ return "You are funny :D" } }
LibraryC:
class ClassC { fun tellMeAJoke(): String { return "You are funny :C" } }
LibraryB:
class ClassB { val b = ClassD() fun whereIsMyJoke(): String { return b.tellMeAJoke() } }
LibraryA:
class ClassA { val c = ClassC() fun whereIsMyJoke(): String { return c.tellMeAJoke() } }
From above Class files, it is observed that LibraryA and LibraryB is dependent on LibraryC and LibraryD respectively. Therefore, these dependencies needs to be added to build.gradle files.
Compile (2.0) or Api (3.0):
New api
keyword is exactly the same as the old compile
keyword. Thus, if all compile
is replaced with api
, it works just fine. Now, let’s add dependency of LibraryD using api keyword in LibraryB.
dependencies { . . . . api project(path: ':libraryD') }
Similarly, LibraryB is added to the app module.
dependencies { . . . . api project(path: ':libraryB') }
Now, both LibraryB and LibraryD can be accessed from the app module. In sample App, both libraries are accessed like this:
Implementation (3.0):
It’s time to find out how implementation
is different than api
. Again back to the example, this time let’s import LibraryC to LibraryA using implementation
keyword.
dependencies { . . . . implementation project(path: ':libraryC') }
Same for App module.
dependencies { . . . . implementation project(path: ':libraryA') }
Now, if we try to access LibraryC from app module, Android studio will throw an error. Like this:
Which implies that LibraryC can’t be accessed directly from App module if we use implementation
instead of api
. So, what is the benefit of this?
Implementation vs api:
In the first scenario, LibraryD is compiled by using api
. If any change is implemented inside LibraryD, gradle needs to recompile LibraryD, LibraryB and all other modules which import LibraryB as any other module might use implementation of LibraryD.
Please comment here if this works for you 🙂