Android:加载库并将其仅用于开发分支而不用于发行 [英] Android: Loading a library and using it only for development branch not for release

查看:120
本文介绍了Android:加载库并将其仅用于开发分支而不用于发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述: 我只想在开发环境上使用库,而不要在发行版(应用程序商店发行版)上使用库. 而且我也不希望该库也内置在发行版apk中.

Problem Statement: I want to use a library only on development environment but not on release (app store release). And I don't want that library to get built in release apk also.

我的环境

所以,我有一个这样的环境设置:

So, I have an environment setup like this:

发展-

  1. 调试
  2. 发布

商店-

  1. 调试
  2. 发布->进入商店

在gradle中,我添加了-

In gradle I have added -

debugCompile 'com.some.library'

加载该库以进行开发-调试存储-调试

然后我创建了两个Application类,

And then I have created two Application classes,

  1. ApplicationWithoutDebugLibrary 扩展了MultiDexApplication-不初始化库的应用程序类.
  2. ApplicationWithDebugLibrary 扩展了ApplicationWithoutDebugLibrary-初始化库的应用程序类
  1. ApplicationWithoutDebugLibrary extends MultiDexApplication - Application class which doesn't initializes the library.
  2. ApplicationWithDebugLibrary extends ApplicationWithoutDebugLibrary - Application class Which initialises the library

我已经在gradle中定义了为不同的风味加载不同的应用程序文件.

And I have defined in gradle to load different Application file for different flavour.

productFlavors {
            Development {
                applicationId "xyzzzz"
                manifestPlaceholders = [application:"com.xyz.ApplicationWithDebugLibrary"]
            }
            store {
                applicationId "11111"
                manifestPlaceholders = [application:"com.xyz.ApplicationWithoutDebugLibrary"]
            }
}

在清单中,我写了这个:

And in manifest I have written this:

<application
    android:name="${application}"...

因此,对于Debug来说,它工作正常,但是当我构建Store-Release/Development-Release apk时,它无法编译ApplicationWithDebugLibrary.java,因为我使用的是库,该库未在gradle文件中进行编译以供发布味道.

So, for Debug it is working fine but when I am building Store-Release/ Development-Release apk it is not able to compile ApplicationWithDebugLibrary.java, as I am using the library, which is not compiled in gradle file for release flavour.

因此,有什么方法可以避免为商店发布风格加载此类,或者有其他替代解决方案,其中我只能在开发环境中加载该库.

So, is there any way in which we can avoid loading this class for Store release flavour, or any alternate solution in which I can load that library only in Development environment.

推荐答案

因此,对于Debug来说,它工作正常,但是当我构建Store-Release/Development-Release apk时,由于我正在使用该库,因此无法编译ApplicationWithDebugLibrary.java,因为该库未在gradle文件中进行编译以实现发行版本

So for Debug it is working fine but when I am building Store-Release/ Development-Release apk it is not able to compile ApplicationWithDebugLibrary.java, as I am using the library, which is not compiled in gradle file for release flavour

根据逻辑,没有办法神奇地删除代码使用的库,而且由于缺少符号,因此仍然可以正确编译所有剩余库.因此,您必须创建虚拟"库,该库具有与调试库相同的API,但没有方法主体.另外,您可以用一些代码包装lib,这些代码以后可以交换为生产版本,而该版本不使用任何库依赖项.

By logic there's no way to magically remove library your code uses and still have all remainings properly compile as there're simply missing symbols. So you must create "dummy" library, with the same API as your debug one but with no methods body. Alternatively you can wrap your lib with some code that can be later swapped for production with version that uses no library dependencies.

Android Gradle插件可以根据构建的类型来帮助构建具有不同版本的依赖项:

Android Gradle plugin can help building with different version of dependencies based of what type of build it is:

编译配置用于编译主应用程序. 其中的所有内容都会添加到编译类路径中 打包在最终的APK中.还有其他可能的配置 将依赖项添加到:

The compile configuration is used to compile the main application. Everything in it is added to the compilation classpath and also packaged in the final APK. There are other possible configurations to add dependencies to:

  • 编译:主要应用程序
  • androidTestCompile:测试应用程序
  • debugCompile:调试构建类型
  • releaseCompile:发布构建类型.
  • compile: main application
  • androidTestCompile: test application
  • debugCompile: debug Build Type
  • releaseCompile: release Build Type.

docs: http://tools.android.com/tech-docs/new-build-system/用户指南#TOC-Dependencies-Android-Libraries-and-Multi-project-setup

这篇关于Android:加载库并将其仅用于开发分支而不用于发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆