gradle从文件加载文本并使用外部lib进行signingConfigs

时间:2014-07-28 19:46:16

标签: android gradle

我正在尝试将保存在json中的用户,别名和密码数据加载到字符串巫婆字符集UTF-8中,以便能够点击我的应用程序。

以下列出了我之前尝试过的内容:

releasePlay {
    storeFile file("android_market_key")
    storePassword = "xxx"
    keyAlias = "yyy"
    keyPassword = "zzz"
}

它工作但没有像łóżćź这样的特殊字符...... 所以我创建了一个文件,写了一个密码,我使用了那个配置::

releasePlay {
    storeFile file("android_market_key")
    storePassword = "xxx"
    keyAlias = file('password.properties').getText('UTF-8')
    keyPassword = "zzz"
}

它正在运作但是不够安全。另一种方法是使用它:

releasePlay {
    storeFile file("android_market_key")

    def console = System.console()
    if (console != null) {
        storePassword = System.console().readLine("\n\$ Enter keystore password: ")
        keyAlias System.console().readLine("\n\$ Enter key alias: ")
        keyPassword = System.console().readLine("\n\$ Enter key password: ")
    }
}

它正在运行但每次手动写入所有数据的速度也很慢。 我决定使用外部json lib从文件中读取所有数据,并通过解析值将它们用作签名参数。但是我卡住了我不知道如何在singnin clousure中使用这个外部库。整个问题在于releasePlay clousure。这是我的代码:

    buildscript {
        repositories {
            maven { url 'http://download.crashlytics.com/maven' }
        }

        dependencies {
            classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
            classpath 'org.codehaus.jackson:jackson-mapper-asl:1.9.0'
        }
    }
    apply plugin: 'com.android.application'
    apply plugin: 'crashlytics'

    android {
        compileSdkVersion 'Google Inc.:Google APIs:14'
        buildToolsVersion "19.1.0"

        defaultConfig {
            applicationId "x.xxxxxxxxx.rorlistservers"
            minSdkVersion 14
            targetSdkVersion 20
            versionCode 7
            versionName "1.5"
        }

        signingConfigs {

            releasePlay {

                def versionPropsF = file('sign.properties').getText('UTF-8')
!Error! >>>     def versionProps = new ObjectMapper().readValue(versionPropsF, HashMap.class);
                storeFile file("android_market_key")
                storePassword = versionProps['storePassword']
                keyAlias = versionProps['keyAlias']
                keyPassword = versionProps['keyPassword']
            }

        }

        lintOptions {
            checkReleaseBuilds true
            abortOnError false
        }

        buildTypes {
            debug {
                //res
            }

            release {
                //res
            }
        }

        productFlavors {
            play {
                signingConfig signingConfigs.releasePlay
            }
        }

    }

    repositories {
        maven { url "http://dl.bintray.com/populov/" }
        maven { url "http://www.bugsense.com/gradle/" }
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.0'
        compile 'com.google.code.gson:gson:1.7.2'
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.loopj.android:android-async-http:1.+'
        compile 'com.google.android.gms:play-services:5.0.77'
        compile project(':libraries:ViewPagerIndicator-')
        compile 'com.crashlytics.android:crashlytics:1.+'

    }

这是堆栈跟踪:

Error:Could not find org.codehaus.jackson:jackson-mapper-asl:1.9.0.
Required by:
    MyAppName:app:unspecified

TL;博士

  1. 我想从文件加载文本(以json格式保存),将其加载到 哈希表并将其保存为storeFile,storePassword等值。
  2. 失败,因为我不知道该怎么办。
  3. 我加入了dependecies clousure json图书馆,但我仍然得到了错误。
  4. 我需要帮助。

2 个答案:

答案 0 :(得分:2)

这是解决方案,我使用了ConfigSlurper: 在build.gradle中,我写道:

buildscript {
    repositories {
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'crashlytics'

android {
    compileSdkVersion 'Google Inc.:Google APIs:14'
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "x.xxxxxxxxx.rorlistservers"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 7
        versionName "1.5"
    }

    signingConfigs {

        releasePlay {

            def fileJsonConfig = file('sign.properties').getText('UTF-8')
            def config = new ConfigSlurper('app').parse(fileJsonConfig)
            storeFile file(config.storeFile)
            storePassword = config.storePassword
            keyAlias = config.keyAlias
            keyPassword = config.keyPassword

            println fileJsonConfig
            println config
        }

    }

    lintOptions {
        checkReleaseBuilds true
        abortOnError false
    }

    buildTypes {
        debug {
            //res
        }

        release {
            //res
        }
    }

    productFlavors {
        play {
            signingConfig signingConfigs.releasePlay
        }
    }

}

repositories {
    maven { url "http://dl.bintray.com/populov/" }
    maven { url "http://www.bugsense.com/gradle/" }
    maven { url 'http://download.crashlytics.com/maven' }
}

dependencies {
    compile 'com.google.code.gson:gson:1.7.2'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.loopj.android:android-async-http:1.+'
    compile 'com.google.android.gms:play-services:5.0.77'
    compile project(':libraries:ViewPagerIndicator-')
    compile 'com.crashlytics.android:crashlytics:1.+'

}

sign.properties的结构是(UTF-8 charset!):

environments {
  app {
    storeFile = "android_market_key"
    storePassword ="xx"
    keyAlias = "yy"
    keyPassword = "zz"
  }
}

答案 1 :(得分:0)

看起来回购http://download.crashlytics.com/maven没有依赖关系org.codehaus.jackson:jackson-mapper-asl:1.9.0。解决此问题的一种方法是添加mavenCentral() repo。或者,您可以使用Groovy的ConfigSlurper类解析JSON。