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

XGo*_*het 44 android gradle android-gradle-plugin

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

dependencies {
    compile project(':custom_lib')
}
Run Code Online (Sandbox Code Playgroud)

当我触发构建过程(Menu 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
Run Code Online (Sandbox Code Playgroud)

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

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

所以我的问题是:

  • 为什么这个双重构建似乎语无伦次?
  • 在启动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')
}
Run Code Online (Sandbox Code Playgroud)

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 {
}
Run Code Online (Sandbox Code Playgroud)

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

Kan*_*ley 65

把它放在你的app依赖项中:

dependencies {
    debugCompile project(path: ':custom_lib', configuration: "debug")
    releaseCompile project(path: ':custom_lib', configuration: "release")
}
Run Code Online (Sandbox Code Playgroud)

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

android {

    defaultConfig {
        defaultPublishConfig 'release'
        publishNonDefault true
    }

}
Run Code Online (Sandbox Code Playgroud)

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

编辑:如果您在修改gradle文件后不进行清理/重建,可能会遇到问题,如此答案中所述.

  • 这是此页面中唯一的干净解决方案. (3认同)

Xav*_*het 21

在左侧的"Build Variants"面板窗口中,您应该看到两个模块,并且旁边是当前的"活动"变体.例如

app          debug
custom_lib   debug
Run Code Online (Sandbox Code Playgroud)

在调用时,Build > Make Project我们正在构建项目中当前变体中的每个模块.

但是,由于当前的Gradle限制(https://code.google.com/p/android/issues/detail?id=52962),构建appdebug需要构建release变体custom_lib,因此最终构建两者.

我建议不要使用Make Project,而是使用下面的选项说Make Module app.此选项将更改applib基于当前选择Project面板或基于当前编辑器,并会一直做构建当前模块只我们所需要的.

(看看这个,我们注意到它没有捷径,所以我们加了一个).

  • 构建更快的正确方法是仅构建所需的模块(而不是整个项目),并让它构建其依赖项. (4认同)

归档时间:

查看次数:

27354 次

最近记录:

6 年,9 月 前