为特定的flavor版本设置debuggable true

Luk*_*uke 2 android gradle

如何定义在releaseBuildConfig中启用了debuggable = true,但仅针对一组特定的flavor:

这是示例代码,包括试用(不起作用):

flavorDimensions "project", "environment"

productFlavors {
    basic  {
        dimension "project"
    }

    advanced {
        dimension "project"
    }

    flavorDevelopment {
        dimension "environment"
        applicationId "ch.myproject.app.development"
        debuggable true  // this does not work
    }

    flavorTest {
        dimension "environment"
        applicationId "ch.myproject.app.test"
        debuggable true  // this does not work

    }

    flavorIntegration {
        dimension "environment"
        applicationId "ch.myproject.app.integration"
        debuggable true  // this does not work
    }

    flavorProduction {
        dimension "environment"
        applicationId "ch.myproject.app.production"
        // just here debuggble has to be on the default (in buildTypes.debug  = on  AND in buildTypes.release = off )
        // this is working
    }
Run Code Online (Sandbox Code Playgroud)

"debuggable true"语句在上面的代码示例中不起作用.但它应该给你一个印象,我试图做的.

我唯一有效的版本是flavorProduction.我在使用默认行为,工作正常.

但所有其他内部版本的flavorDevelopment,flavorTest,flavor Integration,以及我想要启用的调试功能.

我尝试了第二种方法:

    applicationVariants.all { variant ->
    // setting all releases expecting the Production one to debuggable
    if (!variant.buildType.name.contains("ProductionRelease")) {
        variant.buildType.debuggable = true
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到的消息是:

Error:(132, 0) Cannot set readonly property: debuggable for class: com.android.build.gradle.internal.api.ReadOnlyBuildType
Run Code Online (Sandbox Code Playgroud)

有人知道如何使用gradle配置它吗?

谢谢你提前了

Ben*_* P. 9

debuggable是的属性BuildType对象,而不是ProductFlavor对象,所以(如你发现),写debuggable true一个产品味道在里面不会有任何效果.

通常,您将拥有debug构建类型和release构建类型,然后您将拥有类似flavorProductionDebug和的构建变体flavorProductionRelease.听起来这对你来说还不够,你需要保持你的debugrelease构建类型之间的任何不同,同时也要保持debuggable true.

为此,您可以创建第三种构建类型.

buildTypes {
    debug { ... }
    release { ... }
    releaseDebuggable {
        initWith release
        debuggable true
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你的releaseDebuggable构建类型将与你的release构建类型完全一样,但是可调试!

这具有fooReleaseDebuggable为所有产品风格创建构建变体的副作用.如果要取消除以外的所有内容flavorProductionReleaseDebuggable,可以使用variantFilter界面.

  • 非常感谢您的提议!正如您所提到的,将会有许多 applicationVariants。我使用的是flavorDimensions,所以2 个projectflavors x 4 environmentFlavors x 3 buildTypes 将产生24 个applicationVariants。即使使用变体过滤器,定制整个 ci(jenkins 脚本等)也需要付出巨大的努力。我真的不敢相信实际上没有办法只为某些 applicationVariants 修改 `buildTypes.release` 配置......!? (2认同)

Jia*_*eng 7

有一种方法可以实现这一点,只需在每个预期产品风格的源集的清单中将debuggable属性设置为 true。

步骤 1:创建产品口味的源集。
在 app/src/[flavor-name] 中设置新源并在其中创建AndroidManifest.xml。就像下面这样:

在此处输入图片说明

第 2 步:在清单中定义debuggable

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:debuggable="false"
        tools:ignore="HardCodedDebugMode" />
</manifest>
Run Code Online (Sandbox Code Playgroud)

tools:ignore="HardcodedDebugMode" 用于抑制警告。

就是这样,你可以走了。无需修改 build.gradle文件。