android - 当应用程序处于调试状态时,为什么 Gradle 在 Release模式下构建我的模块

标签 android gradle android-gradle-plugin

我正在制作一个新的 Android 项目,其中包含标准的 'app' 模块以及一个库项目(我们称之为 'custom_lib')。在 appbuild.gradle 文件中,我将模块链接为:

dependencies {
    compile project(':custom_lib')
}

当我触发构建过程(菜单 Build > Make Project)时,我在 Gradle 控制台中得到以下输出

Executing tasks: [clean, :app:compileDebugSources, :custom_lib:compileDebugSources]

Configuration on demand is an incubating feature.
:app:clean
:custom_lib:clean
:app:preBuild
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:custom_lib:compileLint
:custom_lib:copyReleaseLint UP-TO-DATE
:custom_lib:mergeReleaseProguardFiles UP-TO-DATE
:custom_lib:preBuild
:custom_lib:preReleaseBuild
:custom_lib:checkReleaseManifest
:custom_lib:prepareReleaseDependencies
:custom_lib:compileReleaseAidl
:custom_lib:compileReleaseRenderscript
:custom_lib:generateReleaseBuildConfig
:custom_lib:generateReleaseAssets UP-TO-DATE
:custom_lib:mergeReleaseAssets
:custom_lib:generateReleaseResValues UP-TO-DATE
:custom_lib:generateReleaseResources
:custom_lib:packageReleaseResources
:custom_lib:processReleaseManifest
:custom_lib:processReleaseResources
:custom_lib:generateReleaseSources
:custom_lib:compileReleaseJava
:custom_lib:processReleaseJavaRes UP-TO-DATE
:custom_lib:packageReleaseJar
:custom_lib:compileReleaseNdk
:custom_lib:packageReleaseJniLibs UP-TO-DATE
:custom_lib:packageReleaseLocalJar UP-TO-DATE
:custom_lib:packageReleaseRenderscript UP-TO-DATE
:custom_lib:bundleRelease
:app:prepareComAndroidSupportAppcompatV72102Library
:app:prepareComAndroidSupportSupportV42102Library
:app:prepareTestDoubleBuildCustom_libUnspecifiedLibrary
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJava
:app:compileDebugNdk
:app:compileDebugSources
:custom_lib:preDebugBuild
:custom_lib:checkDebugManifest
:custom_lib:prepareDebugDependencies
:custom_lib:compileDebugAidl
:custom_lib:compileDebugRenderscript
:custom_lib:generateDebugBuildConfig
:custom_lib:generateDebugAssets UP-TO-DATE
:custom_lib:mergeDebugAssets
:custom_lib:generateDebugResValues UP-TO-DATE
:custom_lib:generateDebugResources
:custom_lib:packageDebugResources
:custom_lib:processDebugManifest
:custom_lib:processDebugResources
:custom_lib:generateDebugSources
:custom_lib:compileDebugJava
:custom_lib:compileDebugNdk
:custom_lib:compileDebugSources

BUILD SUCCESSFUL

Total time: 2.184 secs

令我困惑的是,构建机制触发了 Debug 构建(如第一行所说),但几乎立即,Gradle 使用了任务 :app:preReleaseBuild 这使我的 custom_lib 模块要使用发布配置构建。

然后,在应用程序完全构建后,Gradle 使用 Debug 配置编译我的模块。

所以我的问题是:

  • 为什么要进行这种看似不连贯的双重构建?
  • 当我启动调试构建过程时,如何确保库是使用调试配置构建的?

编辑:

app/build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.deezer.testdoublebuild"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        debug{
            debuggable true
        }
        release {
            debuggable false
            minifyEnabled false
        }
    }
}

dependencies {
    compile project(':custom_lib')
}

custom_lib/build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.deezer.mylibrary"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
}

注意:我正在使用 Android Studio 1.0 RC 1/Gradle 2.2,并通过从头开始创建一个新项目来重现此问题,添加一个空Android 库模块和“瞧”

最佳答案

把它放在你的应用依赖中:

dependencies {
    debugCompile project(path: ':custom_lib', configuration: "debug")
    releaseCompile project(path: ':custom_lib', configuration: "release")
}

并在你的库的 build.gradle 中添加:

android {

    defaultConfig {
        defaultPublishConfig 'release'
        publishNonDefault true
    }

}

然后库将以与应用相同的模式构建。与此答案的先前版本相反,我已经确认库不需要 flavor (这可能是由于 Gradle 或 Android 插件版本 - 我使用的是 Gradle 2.14 和 Android 插件 2.1.0 并且不需要它) .

编辑:如 this answer here. 中所述,如果在修改 gradle 文件后不清理/重建,您可能会遇到麻烦。

关于android - 当应用程序处于调试状态时,为什么 Gradle 在 Release模式下构建我的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27277433/

相关文章:

android - 在可展开的 ListView 中展开所有子项

android - 在模块 classes.jar com.google.android.gms :play-services-measurement-impl: 中发现重复的类 com.google.android.gms.internal.measurement.zzdu

android - assembleDebug 不适用于 Android Studio 中的 Gradle2.4

android - Firebase 表示 "Domain not whitelisted"用于白名单中的链接

Android:traceview 和 systrace 工具的区别

android - HealthConnect - 权限对话框在 Release模式下不起作用

gradle - 使用Cobertura会产生NoClassDefFoundError

Android数据绑定(bind)依赖与支持库冲突

android - 如何创建 Gradle 任务

android - 错误 : cannot find symbol class DataBindingComponet after merge two branches