Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure compile using Gradle Dependencies in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Gradle dependency configuration compile in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

After AndroidStudio was upgraded to 3.0, the gradle version was also upgraded to version 3.0.0.

When the gradle plug-in is upgraded to 3.0.0 and above, we will find that when adding dependencies in gradle, we will recommend you to use implementation or api instead of compile. Today, we will briefly introduce the use and difference between the two!

Classpath 'com.android.tools.build:gradle:3.0.0'

When you create a new Android project, the dependency in build.gradle defaults to implementation, not the previous compile. In addition, gradle version 3.0.0 and above also rely on the instruction api. This article mainly introduces the difference between implementation and api.

Dependency in the build.gradle file of app generated by default in the new project:

Dependencies {implementation fileTree (include: ['* .jar'], dir: 'libs') implementation' com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' 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'}

Api instruction

It's exactly the same as the compile instruction, no difference, you change all the compile to api, there's nothing wrong with it.

Implementation instruction

The characteristic of this directive is that for dependencies compiled with the command, projects with dependencies on the project will not be able to access any programs in the dependencies compiled with the command, that is, hide the dependency internally and not expose it to the outside.

Simply put, dependencies that use implementation instructions are not passed. For example, there is a module that is testLib,testLib dependent on Glide:

Implementation 'com.github.bumptech.glide:glide:3.8.0'

At this point, the java code in testsdk can access Glide.

The other module is app,app dependent on testLib:

Implementation project (': testLib')

At this point, because testsdk uses the implementation instruction to rely on Glide, Glide cannot be referenced in app.

However, if testLib uses api to reference Glide:

Api 'com.github.bumptech.glide:glide:3.8.0'

The effect of the compile instruction before gradle3.0.0 is exactly the same, and the module of app can also refer to Glide, which is the difference between api and implementation.

Suggestion

Compile has been abandoned in the 3.x version of gradle and will be removed by google at the end of 2018, so don't use compile.

The dependency should first be set to implementation, if there are no errors, then use implementation, if there is an error, then use the api instruction

That's all for "how to configure compile with Gradle dependencies in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report