Gradle Android 项目中缺少构建任务 installRelease

Gradle build task installRelease missing in Android project

Gradle 似乎在我正在处理的项目中丢失了构建类型。我可以按如下方式重新创建一个最小的问题。我有以下文件:

build.gradle
local.properties
src/main/AndroidManifest.xml

build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:+'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 1
        targetSdkVersion 23
    }

    buildTypes {
        debug {

        }
        release {

        }
    }
}

local.properties:

sdk.dir=/path/to/android-sdk-linux

src/main/AndroidManifest.xml:

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

我希望 Gradle 生成任务 installDebuginstallRelease,因为我将 debugrelease 定义为 buildTypes。然而,事实并非如此。命令 gradle tasks 产生:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

...

Install tasks
-------------
installDebug - Installs the Debug build.
installDebugAndroidTest - Installs the android (on device) tests for the Debug build.
uninstallAll - Uninstall all applications.
uninstallDebug - Uninstalls the Debug build.
uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build.
uninstallRelease - Uninstalls the Release build.

Verification tasks
------------------
...

出了什么问题?为什么任务 installRelease 不可用?

首先要发布,您需要在根项目中创建 keystore。 您需要在 build.gradle.

中提供这些详细信息

如果需要,您可以创建两个 signingConfigs 调试和发布。

终于在buildTypeslink到那个了。

android {
        signingConfigs {
        debug {
          keyAlias 'alias'
          keyPassword 'password'
          storeFile file('../xyz.jks')
          storePassword 'password'
        }
      }
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        defaultConfig {
            minSdkVersion 1
            targetSdkVersion 23
        }

        buildTypes {
            debug {
              signingConfig signingConfigs.debug
            }
            release {
             signingConfig signingConfigs.debug
            }
        }

然后 installRelease 也将在 gradle 任务中可用

希望对您有所帮助。

对于不打算发布应用程序的边缘情况(例如测试混淆后的 apk),您可以跳过使用生产密钥签名并执行更简单的操作:

android {
    signingConfigs {
    debug {}
  }
    buildTypes {
        debug {
          signingConfig signingConfigs.debug
        }
        release {
         signingConfig signingConfigs.debug
        }
    }