Android: Loading a library and using it only for development branch not for release

陌路散爱 提交于 2019-12-11 02:38:06

问题


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.

My appraoch:

So, I have an environment setup like this:

Development -

  1. Debug
  2. Release

Store -

  1. Debug
  2. Release -> This goes to play store

In gradle I have added -

debugCompile 'com.some.library'

Which loads this library for Development - Debug and Store - Debug

And then I have created two Application classes,

  1. ApplicationWithoutDebugLibrary extends MultiDexApplication - Application class which doesn't initializes the library.
  2. ApplicationWithDebugLibrary extends ApplicationWithoutDebugLibrary - Application class Which initialises the library

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}"...

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.


回答1:


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

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 plugin can help building with different version of dependencies based of what type of build it is:

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:

  • compile: main application
  • androidTestCompile: test application
  • debugCompile: debug Build Type
  • releaseCompile: release Build Type.

docs: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Dependencies-Android-Libraries-and-Multi-project-setup



来源:https://stackoverflow.com/questions/43201205/android-loading-a-library-and-using-it-only-for-development-branch-not-for-rele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!