1

After updating Android Studio and gradle to 3.1, I changed all compile statements to implementation. But when I build, android studio cannot resolve imports found in 3rd party libraries.

Scenario: Main projects imports sub-module which also import a jar file.

When I try to import a class from the jar file into the main project, android studio is not able to resolve it.

How can I import the single file without having to add the jar file as a dependency in the main project?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bubunyo Nyavor
  • 2,511
  • 24
  • 36

3 Answers3

2

You should use api instead, it is the new compile or have the dependency directly in your main project. Just changing implementation to api will fix the issue but you consider using implementation wherever possible to improve build time.

You can see the difference between api and implemenation here.

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
1

The answer by @nongthonbam-tonthoi is correct but he does not explain why.

Short version

Implementation - hide this dependency from other modules(that depend on this module). if B depends on A, it cannot use any dep declared in A using implementation.

api - Make this available to other modules that depend on this module.i.e if you add say GSON as a dep in module A using api rather than implementation, all other modules that depend A can use GSON without declaring it again.

Long version

implementation is a way of declaring dependencies for only a given module. What this means is that, the dependency can only be used in that particular module. compile on the other hand "leaks" the dependencies to other modules so you can import and use the classes that dep brings in other modules. If you want this behavior, the new way of doing it is to use api.

This change is particularly targeted at multi-module projects as it can help gradle avoid re-compiling a module during a build when it does not change.

However if you're migrating from an old project, chances are you are (ab)using compile to use dependencies declared in other modules without explicitly declaring them again.

You can keep using compile but remember that it's is deprecated and will be removed soon.

See here for a deeper explanation.

0

Edit build.gradle (Module:app) and change SDK version to 27.1.1 Eg:

defaultConfig {
        applicationId "com.projectname"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

implementation 'com.android.support:appcompat-v7:27.1.1'

and rebuild the project and restart android studio

LEGEND MORTAL
  • 336
  • 3
  • 18