前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android dependencies 基础知识

Android dependencies 基础知识

作者头像
Oceanlong
修改2018-12-05 15:20:41
2.5K0
修改2018-12-05 15:20:41
举报

概述

在build:gradle2.x的时代,我们在进行第三方依赖时,会有一些尴尬的问题。比如,我们制作了一个库,依赖了Glide2.0 。项目组集成我们的库,同时也需要使用Glide4.0 。

由于Glide4.0与Glide2.0包名相同,项目组只能使用一个。一般面对重复包含的问题,我们会使用provided。这样我们自己的库,就只在编译时用一下Glide2.0的接口,Glide2.0本身的实现没有包含进去。到真正运行时,我们的库使用Glide4.0的实现。

仔细想想,这样肯定也是有问题的。且不说Glide两个版本实现的差异,版本差距这么大,接口肯定也有不兼容的情况。

所幸在build:gradle3.x中,google为我们提供了新的依赖方式。

Dependency configurations

api

替代了之前的compile。调用compile之后,依赖会被完全加入到库或者项目中。如果自制库中compile了第三方依赖,项目在使用自制库时,会将自制库的所有依赖加入项目中。

项目中可以直接用自制库和第三方依赖。

implementation

这个设置与compile类似,区别是当我们使用implementation时,我们其实希望不要将自制库的第三方依赖泄露给项目方。项目方只会在运行时,用到第三方依赖,编译期无法获取它们。

除此之外,implementation的使用还会加快我们的编译速度。因为我们其实隔离了第三方依赖和项目。

compileOnly

替代了之前的provided。这个是制作库时常用的方式。当我们使用这样的方式引入库时,我们不会将第三方依赖的实现加入到我们的自制库中。我们只在编译时使用了第三方依赖。

当项目使用我们的自制库时,需要自行加入我们的第三方依赖,以保证其在运行时的正确运行。这个非常适用于,当我们的自制库想要使用一些特定的,普遍的通用库时。

runtimeOnly

当使用runtimeOnly时,我们在编译期无法使用这个依赖,但却将它的内容带进了包,运行时可以使用。这个使用比较少,不展开。

Dependency with flavors

我们也可以根据不同的flavors使用不同的依赖。

代码语言:javascript
复制
android {
    ...

    flavorDimensions "country"

    productFlavors {
        china {
            applicationIdSuffix  "zh"
            dimension "country"
            buildConfigField "String" , "channel" , '"1"'
            buildConfigField "Integer" , "ImageLoader" , "1"


        }
        usa {
            applicationIdSuffix "usa"
            dimension "country"
            buildConfigField "String" , "channel" , '"2"'
            buildConfigField "Integer" , "ImageLoader" , "2"
        }
    }
}

dependencies {
     ...
    // china
    chinaImplementation 'com.github.bumptech.glide:glide:4.8.0'
    // usa
    usaImplementation 'com.facebook.fresco:fresco:1.11.0'
    ...
}

使用起来很简单,我们只需要在implementation前面加上我们对应的flavorDimensions即可。

在上例中,我们假设在中国使用glide,在美国使用fresco。

但实际起来,却不能只是这样。由于glide和fresco接口并不相同。

如果我们不能接项目与依赖的实现完全解耦合,我们将无法真正的根据flavors选择不同实现

我们需要为它们封装相同的接口层。

所以,我们可以这样:

build.gralde

代码语言:javascript
复制
    productFlavors {
        china {
            applicationIdSuffix  "zh"
            dimension "country"
            buildConfigField "String" , "channel" , '"1"'
            buildConfigField "Integer" , "ImageLoader" , "1"


        }
        usa {
            applicationIdSuffix "usa"
            dimension "country"
            buildConfigField "String" , "channel" , '"2"'
            buildConfigField "Integer" , "ImageLoader" , "2"
        }
    }
dependencies {
    ...
    /* -------------------------------- ImageLoader-------------------------------*/
    // interface
    implementation project(':imageloader')
    // china
    chinaImplementation 'com.github.bumptech.glide:glide:4.8.0'
    // usa
    usaImplementation 'com.facebook.fresco:fresco:1.11.0'

    /*----------------------------------------------------------------------------*/
    ...

}

通过上面的配置,我们引入了我们不同渠道的公共接口层,然后再根据渠道引入各自的实现层。

然后,我们在接口层的build.gradle中,我们加入:

代码语言:javascript
复制
dependencies {
    ...
    compileOnly 'com.github.bumptech.glide:glide:4.8.0'
    compileOnly 'com.facebook.fresco:fresco:1.11.0'
    ...
}

这样,我们可以根据一个参数,在ImageLoader中决定选择谁的实现。

ImageLoaderFactory.java:

代码语言:javascript
复制
public class ImageLoaderFactory {

    public static final int GLIDE_IMPL = 1;
    public static final int FRESCO_IMPL = 2;

    static public ImageLoader createImageLoader(int impl){
        ImageLoader imageLoader = null;
        switch (impl){
            case GLIDE_IMPL:
                imageLoader = new GlideImpl();
                break;

            case FRESCO_IMPL:
                imageLoader = new FrescoImpl();
                break;

            default:
                Log.e("ImageLoader" , "there is no ImageLoader implement");
                break;
        }

        return imageLoader;

    }
}

这样我们就可以在项目中,使用:

代码语言:javascript
复制
        ImageView imageView = new ImageView(this);
        ImageLoader imageLoader = ImageLoaderFactory.createImageLoader(BuildConfig.ImageLoader);
        imageLoader.displayImage(imageView, null);

这样我们就实现了,一套代码,多种实现的自由切换了。

还有一个问题

在上面的代码中,我们根据国家,选用了不同的图片框架实现,用了统一的接口,看起来美美的,其实还有一个致命缺点。

我们再仔细看一下项目的build.gradle

代码语言:javascript
复制
    productFlavors {
        china {
            applicationIdSuffix  "zh"
            dimension "country"
            buildConfigField "String" , "channel" , '"1"'
            buildConfigField "Integer" , "ImageLoader" , "1"


        }
        usa {
            applicationIdSuffix "usa"
            dimension "country"
            buildConfigField "String" , "channel" , '"2"'
            buildConfigField "Integer" , "ImageLoader" , "2"
        }
    }
dependencies {
    ...
    /* -------------------------------- ImageLoader-------------------------------*/
    // interface
    implementation project(':imageloader')
    // china
    chinaImplementation 'com.github.bumptech.glide:glide:4.8.0'
    // usa
    usaImplementation 'com.facebook.fresco:fresco:1.11.0'

    /*----------------------------------------------------------------------------*/
    ...

}

我们终究还是implementation了图片,我们依然可以在项目中,使用Glide或Fresco。如果开发同学使用Glide或Fresco,项目就又与实现耦合了,这样我们就功亏一篑了。

所以,我们可以把build.gradle改为:

代码语言:javascript
复制
    productFlavors {
        china {
            applicationIdSuffix  "zh"
            dimension "country"
            buildConfigField "String" , "channel" , '"1"'
            buildConfigField "Integer" , "ImageLoader" , "1"


        }
        usa {
            applicationIdSuffix "usa"
            dimension "country"
            buildConfigField "String" , "channel" , '"2"'
            buildConfigField "Integer" , "ImageLoader" , "2"
        }
    }
dependencies {
    ...
    /* -------------------------------- ImageLoader-------------------------------*/
    // interface
    implementation project(':imageloader')
    // china
    chinaRuntimeOnly 'com.github.bumptech.glide:glide:4.8.0'
    // usa
    usaRuntimeOnly 'com.facebook.fresco:fresco:1.11.0'

    /*----------------------------------------------------------------------------*/
    ...

}

这样,我们在项目中,将无法直接使用Glide或Fresco,因为我们没有对它们进行编译。但运行时,我们却加入了它们。这样就完全实现了接口与实现的分离。

以上就是关于Android依赖的一些基础知识。

如有问题,欢迎指正。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • Dependency configurations
    • api
      • implementation
        • compileOnly
          • runtimeOnly
          • Dependency with flavors
            • 还有一个问题
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档