iphonem
iphonem

Reputation: 65

Proper use of 'implementation'

I have a project with the following structure.

+-MyApplication
+-MyLibrayOne
+-MyLibrayTwo

MyApplication is my main application whereas MyLibrayOne and MyLibrayTwo are two libraries imported into project. MyApplication uses some classes of MyLibrayOne and MyLibrayOne uses some classes of MyLibrayTwo.

In the .gradle file of MyLibrayOne I have used - compile project(':MyLibrayTwo'). Everything works fine. But if I replace compile with implementation it can not import the classes from MyLibrayTwo. It gives error: cannot find symbol class XXXX error.

Upvotes: 2

Views: 123

Answers (2)

Itamar Kerbel
Itamar Kerbel

Reputation: 2568

First I encourage you to have a look here in the Google I/O 2017. compile and implementation are not the same thing. compile lets the application access to all sub-dependencies that the sub-library has access to. This means that if MyLibrayOne depends on MyLibraryTwo and you need also access to the class from MyLibrarayTwo you'll have to direct gradle to expose the classes from MyLibrayTwo using the compile directive. If this is not needed then implementation is enough. I can only guess that your case is the former and so you'll need to keep using compile. Since compile is now deprecated use the api directive. They are the same. Also, have a look here in the gradle documentation.

Upvotes: 1

shizhen
shizhen

Reputation: 12583

Use api instead of implementation will solve your problem. I.e.

dependencies {
    // Enforce the runtime dependencies
    api project(':MyLibrayOne')
    api project(':MyLibrayTwo')
}

Simply to explain, api will let the depending project see all the classes of the depended projects but implementation cannot do this.

Upvotes: 2

Related Questions