无法为'dialog'原因创建组件:java.awt.HeadlessException

时间:2017-08-27 20:54:25

标签: android gradle android-gradle

将gradle更新到版本3.0.0-beta3后,当我尝试显示一个对话框以询问密钥的密码时出现此错误。

我尝试添加System.setProperty('java.awt.headless', 'false'),但它没有用,我在谷歌上找不到任何新内容....

  

错误:(20,1)无法为'对话'原因创建组件:   java.awt.HeadlessException异常

     
    

java.awt.HeadlessException(无错误消息)

  
import groovy.swing.SwingBuilder

gradle.taskGraph.whenReady { taskGraph ->
        def storePassword = ''
        def keyPassword = ''
        if (System.console() == null) {
            new SwingBuilder().edt {
                dialog(modal: true, title: 'Enter password', alwaysOnTop: true, resizable: false, locationRelativeTo: null, pack: true, show: true) {
                    vbox {
                        label(text: "Please enter store passphrase:")
                        def input1 = passwordField()
                        label(text: "Please enter key passphrase:")
                        def input2 = passwordField()
                        button(defaultButton: true, text: 'OK', actionPerformed: {
                            storePassword = input1.password;
                            keyPassword = input2.password;
                            dispose();
                        })
                    }
                }
            }
        } else {
            storePassword = System.console().readPassword("\nPlease enter store passphrase: ")
            keyPassword = System.console().readPassword("\nPlease enter key passphrase: ")
        }

        if (storePassword.size() <= 0 || keyPassword.size() <= 0) {
            throw new InvalidUserDataException("You must enter the passwords to proceed.")
        }

        storePassword = new String(storePassword)
        keyPassword = new String(keyPassword)

        android.signingConfigs.Keys.storePassword = storePassword
        android.signingConfigs.Keys.keyPassword = keyPassword
}

4 个答案:

答案 0 :(得分:5)

只需在System.setProperty('java.awt.headless', 'false')之前添加import groovy.swing.SwingBuilder并使用./gradlew --stop

杀死gradle deamon

它给你的东西:

apply plugin: 'com.android.application'

System.setProperty('java.awt.headless', 'false')

import groovy.swing.SwingBuilder

答案 1 :(得分:2)

要解决此问题,您必须按此顺序添加这3行(其他订单不起作用)

apply plugin: 'com.android.application'

import groovy.swing.SwingBuilder

System.setProperty('java.awt.headless', 'false')

答案 2 :(得分:0)

def doPromptForPassword(msg) {
if (System.console() == null) {
    System.setProperty('java.awt.headless', 'false') // worked for me
    def ret = null
    new SwingBuilder().edt {
        dialog(modal: true, title: 'Enter password', alwaysOnTop: t

答案 3 :(得分:-1)

对我来说,当我两次添加System.setProperty('java.awt.headless', 'false')时,它终于可以工作了:

  • 导入后立即
  • System.console() == null检查之后

但是只有在System.console() == null检查之后,这种情况发生。

请不要忘记如上所述./gradlew --stop