简体   繁体   English

从3rd Party Library导入课程。 Android Studio 3.1,Gradle 3.1

[英]Import class from 3rd Party Library. Android Studio 3.1, Gradle 3.1

After updating Android Studio and gradle to 3.1, I changed all compile statements to implementation . 在将Android Studio和gradle更新为3.1之后,我将所有的compile语句更改为implementation But when I build, android studio cannot resolve imports found in 3rd party libraries. 但是当我构建时,android studio无法解析在第三方库中找到的导入。

Scenario: Main projects imports sub-module which also import a jar file. 场景:主项目导入子模块,该子模块也导入jar文件。

When I try to import a class from the jar file into the main project, android studio is not able to resolve it. 当我尝试将类文件从jar文件导入到主项目中时,android studio无法解决它。

How can I import the single file without having to add the jar file as a dependency in the main project? 如何导入单个文件而不必在主项目中添加jar文件作为依赖项?

You should use api instead, it is the new compile or have the dependency directly in your main project. 您应该改为使用api ,它是新的compile或者直接在主项目中具有依赖项。 Just changing implementation to api will fix the issue but you consider using implementation wherever possible to improve build time. 只需将implementation更改为api解决此问题,但您可以考虑使用implementation以缩短构建时间。

You can see the difference between api and implemenation here . 您可以在此处查看apiimplemenation之间的区别。

The answer by @nongthonbam-tonthoi is correct but he does not explain why. @ nongthonbam-tonthoi的回答是正确的,但他没有解释原因。

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 . 如果B依赖于A ,则它不能使用A implementation声明的任何dep。

api - Make this available to other modules that depend on this module.ie 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. API -使这个可依赖如果添加此module.ie其他模块说GSON在模块DEP A使用api ,而不是implementation ,依赖所有其他模块A可以使用GSON,而无需再次声明它。

Long version 长版

implementation is a way of declaring dependencies for only a given module. implementation是一种仅声明给定模块依赖性的方法。 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. 另一方面, compile将依赖关系“泄漏”到其他模块,以便您可以导入和使用dep引入其他模块的类。 If you want this behavior, the new way of doing it is to use api . 如果您想要这种行为,执行此操作的新方法是使用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. 此更改特别针对多模块项目,因为它可以帮助gradle避免在构建期间不更改模块的情况下重新编译模块。

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. 但是,如果您要从旧项目迁移,则很可能会(ab)使用编译来使用其他模块中声明的依赖项,而无需再次明确声明它们。

You can keep using compile but remember that it's is deprecated and will be removed soon. 您可以继续使用compile但请记住它已被弃用,很快就会被删除。

See here for a deeper explanation. 请参阅此处以获得更深入的解释。

Edit build.gradle (Module:app) and change SDK version to 27.1.1 Eg: 编辑build.gradle (Module:app)并将SDK版本更改为27.1.1例如:

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 并重建项目并重新启动android studio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM