我想有我的Gradle构建创建一个发布签名apk文件使用Gradle。

我不确定代码是否正确,或者我在做gradle构建时是否缺少一个参数?

这是build.gradle/build.gradle中的一些代码。节文件:

android {
    ...
    signingConfigs {
        release {
            storeFile(file("release.keystore"))
            storePassword("******")
            keyAlias("******")
            keyPassword("******")
        }
    }
}

Gradle构建成功完成,在我的build/apk文件夹中,我只看到…-release-unsigned.apk和…-debug-unaligned.apk文件。

对如何解决这个问题有什么建议吗?


当前回答

我有几个问题,我把下面的行放在错误的地方:

signingConfigs {
    release {
        // We can leave these in environment variables
        storeFile file("d:\\Fejlesztés\\******.keystore")
        keyAlias "mykey"

        // These two lines make gradle believe that the signingConfigs
        // section is complete. Without them, tasks like installRelease
        // will not be available!
        storePassword "*****"
        keyPassword "******"
    }
}

确保你把signingConfigs部分放在了android部分:

android
{
    ....
    signingConfigs {
        release {
          ...
        }
    }
}

而不是

android
{
    ....
}

signingConfigs {
   release {
        ...
   }
}

很容易犯这个错误。

其他回答

另一种方法是定义一个只在发行版上运行的任务。

android {
  ...
  signingConfigs {
     release {
        // We can leave these in environment variables
        storeFile file('nameOfKeystore.keystore')
        keyAlias 'nameOfKeyAlias'

        // These two lines make gradle believe that the signingConfigs
        // section is complete. Without them, tasks like installRelease
        // will not be available!
        storePassword "notYourRealPassword"
        keyPassword "notYourRealPassword"

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

task setupKeystore << {
final Console console = System.console();
if (console != null) {
    //def keyFile = console.readLine(“\nProject: “ + project.name + “Enter keystore path: "))
    //def keyAlias = console.readLine(“Project: “ + project.name + “Enter key alias: ")
        def storePw = new String(console.readPassword(“Project: “ + project.name + “. Enter keystore password: "))
        def keyPw  = new String(console.readPassword(“Project: “ + project.name + “.Enter keystore password: "))

    //android.signingConfigs.release.storeFile = file(keyFile);
    //android.signingConfigs.release.keyAlias = keyAlias
        android.signingConfigs.release.storePassword = storePw
        android.signingConfigs.release.keyPassword = keyPw
}
}

//Validate t
def isReleaseConfig = gradle.startParameter.taskNames.any {it.contains('Release') }
if (isReleaseConfig) {
    setupKeystore.execute();
}

您可以从命令行请求密码:

...

signingConfigs {
  if (gradle.startParameter.taskNames.any {it.contains('Release') }) {
    release {
      storeFile file("your.keystore")
      storePassword new String(System.console().readPassword("\n\$ Enter keystore password: "))
      keyAlias "key-alias"
      keyPassword new String(System.console().readPassword("\n\$ Enter keys password: "))
    } 
  } else {
    //Here be dragons: unreachable else-branch forces Gradle to create
    //install...Release tasks.
    release {
      keyAlias 'dummy'
      keyPassword 'dummy'
      storeFile file('dummy')
      storePassword 'dummy'
    } 
  }
}

...

buildTypes {
  release {

    ...

    signingConfig signingConfigs.release
  }

  ...
}

...

if-then-else块防止在构建版本时请求密码。虽然else分支是不可达的,它欺骗Gradle创建一个安装…发布的任务。

基本信息。正如https://stackoverflow.com/a/19130098/3664487所指出的,“Gradle脚本可以使用System.console()提示用户输入。readLine方法。”不幸的是,Gradle总是会要求密码,即使你在构建一个调试版本(参见如何使用Gradle创建一个发布签名apk文件?)幸运的是,这是可以克服的,如上所述。

如果您想避免在构建中硬编码您的密钥库和密码。gradle,你可以使用属性文件解释这里:处理签名配置与gradle

基本上:

1)创建一个myproject。属性文件在/home/[username]/。签名内容如下:

keystore=[path to]\release.keystore
keystore.password=*********
keyAlias=***********
keyPassword=********

2)创建gradle。属性文件(可能在你的项目目录的根目录)的内容:

MyProject.properties=/home/[username]/.signing/myproject.properties

3)在你的构建中参考它。这样Gradle:

    if(project.hasProperty("MyProject.properties")
        && new File(project.property("MyProject.properties")).exists()) {

    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("MyProject.properties"))))

    signingConfigs {
        release {
            storeFile file(props['keystore'])
            storePassword props['keystore.password']
            keyAlias props['keyAlias']
            keyPassword props['keyPassword']
        }
    }
}

Android工作室 转到文件->项目结构或按Ctrl+Alt+Shift+S

查看图片

单击Ok

然后在构建中生成signingconfig。gradle文件。

如果您像我一样,只是希望能够在您的设备上运行该版本以进行测试,那么可以考虑为签名创建第二个密钥存储库,这样您就可以简单地将其密码放入您的构建中。Gradle无需担心您的市场密钥存储安全。

您可以通过单击Build/Generate Signed APK/ create new…来创建一个新的密钥库。