顯示具有 gradle 標籤的文章。 顯示所有文章
顯示具有 gradle 標籤的文章。 顯示所有文章

2013/07/17

Ask password when release using gradle

總算移轉到 Gradle 來, 但卻出現一個 release sign 的問題.
之前使用 ant 在 sign release 會跳出提示來問密碼, 但是 gradle 卻不知該怎麼做.
Google 一番, 找到以下連結.
assemblerelease-task-dependency-ask-for-keystore-password

但這卻有一個問題, 雖然會提示輸入密碼, 但是最後出來的 apk 一樣沒有 sign.

以下是我的解決方法:
task('readPasswordFromInput') << {
def console = System.console()

ext.keystorePassword = console.readLine('\n\n\n> Enter keystore password: ')

android.signingConfigs.release.storePassword = ext.keystorePassword
android.signingConfigs.release.keyPassword = ext.keystorePassword
}

tasks.whenTaskAdded { task ->
if (task.name.matches("validateReleaseSigning")) {
task.dependsOn('readPasswordFromInput')
}
}

另外在 build type release 那邊加上以下
    signingConfigs {
debug {
storeFile file("my-debug-key.keystore")
}

release {
storeFile file("my-release-key.keystore")
storePassword ""
keyAlias "release_key"
keyPassword ""
}
}

這樣當 release 時就可以輸入密碼也成功的 sign 了.

2013/06/26

Gradle with external dependencies

看一下 google 關於 gradle 的文件, 使用 android-studio 創建立一個 gradle project,
測試了一下 dependencies, 一直出現以下錯誤. 實在搞不懂為什麼.
* What went wrong:
A problem occurred configuring project ':Test'.
> Failed to notify project evaluation listener.
   > Could not resolve all dependencies for configuration ':Test:compile'.
      > Could not find com.github.kevinsawicki:http-request:5.4.
        Required by:
            TestProject:Test:unspecified


後來總算 Google 到了, 才發現自己文件真是沒有看仔細.
在 buildscript 裡面的 mavenCentral 只是單純給 buildscript 裡面的 dependencies 使用,
如果要實際參照到自己的 dependencies 的話, 則要額外再補上 repositories.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.+'
    }
}
apply plugin: 'android'

/* 這個不加上去, dependencies 就會找不到 */
repositories {
   mavenCentral() 
}

dependencies {
    /* compile files('libs/android-support-v4.jar') */
compile fileTree(dir: 'libs', include: '*.jar')

 compile 'com.github.kevinsawicki:http-request:5.4'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
    }
}

參照上面的 build.gradle, 這樣就可以了.
之後大概會把自己所有的 projects 全部轉成 gradle projects 吧.

更新:
不知道在那一版本的 android-studio, 下面這行已經會自動加上去了.
repositories {
   mavenCentral() 
}