Java 在 gradle 和 Android Studio 中调试和发布的不同依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22439593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Different dependencies for debug and release in gradle and Android Studio
提问by Meteorite
I have an Android project, that depends on pure Java project. Both of them depend on another Java library, also in my multiproject gradle set in Android Studio. I have two versions of that library and want both Android and Java projects to depend on one of them in debug mode, and another - in release.
我有一个 Android 项目,它依赖于纯 Java 项目。它们都依赖于另一个 Java 库,也在我在 Android Studio 中设置的多项目 gradle 中。我有该库的两个版本,并希望 Android 和 Java 项目在调试模式下依赖其中一个,而在发布中依赖另一个。
Is it possible for Android project? For pure Java project? How?
Android项目可以吗?对于纯Java项目?如何?
回答by athor
Build Types (debug, release, or custom) can have their own dependencies.
构建类型(调试、发布或自定义)可以有自己的依赖项。
To specify a dependency specific to a build type, do the following:
要指定特定于构建类型的依赖项,请执行以下操作:
dependencies {
debugCompile "mydebugdependency"
releaseCompile "myreleasedependency"
}
If your java project and android project are both using gradle, you can do the above in both of their build.gradle files.
如果您的 java 项目和 android 项目都使用 gradle,您可以在它们的 build.gradle 文件中执行上述操作。
回答by takecare
My buildDebug
dependency was also getting ignored. My setup is the app module and a library module, and I have the need to propagate the build type from the app to the library modules, i.e., when I compile the debug type on the app I want to get the library debug type too.
我的buildDebug
依赖也被忽略了。我的设置是应用程序模块和库模块,我需要将构建类型从应用程序传播到库模块,即,当我在应用程序上编译调试类型时,我也想获得库调试类型。
As mentioned, I tried having a specific dependency for each build type on the app gradle
file, but to no avail:
如前所述,我尝试为app gradle
文件上的每种构建类型设置特定的依赖项,但无济于事:
buildTypes {
debug {
debuggable true
applicationIdSuffix ".debug"
dependencies {
debugCompile project(":library")
}
}
}
Ultimately what did the trick for me was this: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
最终对我有用的是:http: //tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
So, now the library dependency is managed (as usual) in the global dependencies scope in the app gradle
file:
因此,现在库依赖项在app gradle
文件的全局依赖项范围内(像往常一样)进行管理:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
}
and had to add this to the library's gradle build file:
并且必须将其添加到库的 gradle 构建文件中:
android {
publishNonDefault true
}
This publishes all of the dependencies' build types. Note that if it takes a lot of time to compile your dependency, this solution might not be right for you.
这将发布所有依赖项的构建类型。请注意,如果编译依赖项需要花费大量时间,则此解决方案可能不适合您。
回答by yoAlex5
You are able to do this using next pattern
您可以使用下一个模式执行此操作
build_variant_namedependency_configurations"dependency"
build_variant_namedependency_configurationsproject(path: ':libName', configuration: 'build_variant_name of libName')
build_variant_namedependency_configurations"dependency"
build_variant_namedependency_configurations项目(路径:':libName',配置:'build_variant_name of libName')
For example
例如
dependencies {
FreeDebugImplementation "dependency"
PaidReleaseApi project(path: ':libName', configuration: 'release')
}
You can read more about build variants - https://developer.android.com/studio/build/build-variantsdependency configurations - https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#new_configurations
您可以阅读有关构建变体的更多信息 - https://developer.android.com/studio/build/build-variants依赖项配置 - https://developer.android.com/studio/build/gradle-plugin-3-0- 0-迁移#new_configurations