在gradle和Android Studio中调试和发布的不同依赖项

时间:2022-09-03 22:49:02

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项目?怎么样?

3 个解决方案

#1


52  

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文件中执行上述操作。

#2


34  

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:

因此,现在应用程序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.

这将发布所有依赖项的构建类型。请注意,如果编译依赖项需要花费大量时间,则此解决方案可能不适合您。

#3


1  

You are able to do this using next pattern

您可以使用下一个模式执行此操作

build_variant_name dependency_configurations "dependency"
build_variant_name dependency_configurations project(path: ':libName', configuration: 'build_variant_name of libName')

build_variant_name dependency_configurations“dependency”build_variant_name dependency_configurations project(path:':libName',configuration:'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-variants dependency 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

#1


52  

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文件中执行上述操作。

#2


34  

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:

因此,现在应用程序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.

这将发布所有依赖项的构建类型。请注意,如果编译依赖项需要花费大量时间,则此解决方案可能不适合您。

#3


1  

You are able to do this using next pattern

您可以使用下一个模式执行此操作

build_variant_name dependency_configurations "dependency"
build_variant_name dependency_configurations project(path: ':libName', configuration: 'build_variant_name of libName')

build_variant_name dependency_configurations“dependency”build_variant_name dependency_configurations project(path:':libName',configuration:'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-variants dependency 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