当前位置: 首页 > 知识库问答 >
问题:

为什么Gradle在应用程序处于调试状态时以发布模式构建我的模块

卢勇
2023-03-14

我正在制作一个新的Android项目,带有标准的'app'模块,以及一个库项目(我们称之为'custom_lib')。在应用程序的构建中。gradle文件,我将模块链接为:

dependencies {
    compile project(':custom_lib')
}

当我触发构建过程(菜单Build

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

让我困惑的是,构建机制触发了调试构建(如第一行所述),但Gradle几乎立即使用了task:app:preReleaseBuild,这使我的自定义库模块使用发布配置构建。

然后,在应用程序完全构建之后,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')
}

自定义库/构建。格拉德尔:

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库模块和“瞧”来重现这个问题


共有3个答案

林劲
2023-03-14

Gradle现在支持Flavor buildType Compile指令,因此KaneORiley的答案现在可以改进如下:

图书馆的build.gradle:

android {
    defaultPublishConfig 'release'
    publishNonDefault true
    productFlavors {
        library {
    }
}

应用程序的构建。格拉德尔:

configurations {
    devDebugCompile
    devReleaseCompile
    storeDebugCompile
    storeReleaseCompile
}

android {
    .....
}

dependencies {
    (...)
    devDebugCompile    project(path: ':path:to:lib', configuration: 'devDebug')
    devReleaseCompile  project(path: ':path:to:lib', configuration: 'devRelease')
    storeDebugCompile  project(path: ':path:to:lib', configuration: 'storeDebug')
    storeReleaseCompile project(path: ':path:to:lib', configuration: 'storeRelease') 
}
蒋胡非
2023-03-14

在左侧的“Build Variants”面板窗口中,您应该看到两个模块,以及它们旁边的当前“active”Variants。例如

app          debug
custom_lib   debug

调用Build时

但是,由于当前的梯度限制(https://code.google.com/p/android/issues/detail?id=52962),在debug中构建app将需要构建custom_librelease变体,因此您最终会同时构建这两个版本。

我建议不要使用制作项目,而是使用下面的选项,上面写着制作模块应用。此选项将基于Project面板中的当前选择或基于当前编辑器从app更改为lib,并且将始终只执行构建当前模块所需的操作。

(研究这一点,我们注意到它没有捷径,所以我们添加了一条)。

仇经武
2023-03-14

把这个放在你的应用依赖项中:

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

在你的图书馆里。gradle补充:

android {

    defaultConfig {
        defaultPublishConfig 'release'
        publishNonDefault true
    }

}

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

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

 类似资料:
  • 我有Android Studio与Flutter插件安装。Flutter SDK也通过Android Studio安装在我的Mac上,我知道它的路径。 我想这是因为我已经安装了Flutter SDK从Android Studio和我应该更新一些pathes。那么使用Xcode在发布模式下构建flutter应用程序的步骤是什么呢?

  • 这是我在发布模式下构建应用程序时发现的错误日志 /库/框架/单声道。framework/External/xbuild/Xamarin/Android/Xamarin。Android常见的目标:错误:执行任务链接程序集时出错:错误XA2006:引用元数据项“Xamarin”。形式。BindableProperty Xamarin。形式。BindableProperty::Create(System

  • 我试图上传我的与科尔多瓦客户端命令行命令,但谷歌播放一直说我在调试模式下构建的应用程序。 这是我用来构建apk的步骤(以下是答案) 这是谷歌的截图错误

  • 我有一个应用程序,现在我要发布它。我已经创建了一个apk文件,并且安装了它,但是当我向服务器发送登录请求时,它返回NULL。 奇怪的是,当我用调试构建启动应用程序时,它工作得很好。该问题仅发生在释放模式。所以看了这篇文章后我认为问题出在proguard 这是我目前所做的。 null 这是我的proguard-rules.pro文件。

  • 我的构建中有以下代码。格拉德尔: 要生成去释放APK,我使用以下命令: 当上传生成的APK()在谷歌播放我得到了以下错误: 您上载了一个可调试的APK。出于安全原因,您需要先禁用调试,然后才能将其发布到Google Play中。了解有关可调试APK的更多信息。 我能够通过在android清单上硬编码生成一个不可调试的APK。但是构建配置仍然像一个可调试的构建,正如您在生成构建中看到的那样。配置(我

  • 我有应用程序上传在玩商店。它在调试模式下运行良好,但当我从play store下载相同的APK(发布模式)时,它就崩溃了。我无法确定堆栈跟踪没有给出准确的错误位置。 java.lang.RuntimeException:在Android.os.AsyncTask$3上完成(asynctask.java:318)在java.util.concurrent.FutureTask.FinishCompl