一、国内封网,要设置gradle用代理(痛苦至极)
gradle clean时会下载需要的文件,若用代理会导致失败
1.针对单个项目设置gradle代理
修改项目中的gradle.properties文件,增加配置项:
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=10384
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=10384
2.如果想针对所有的gradle项目设置代理
可以在GRADLE_USER_HOME下新建文件gradle.properties,然后设置代理。GRADLE_USER_HOME的路径一般如下:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle\ (Windows)
二、.gradle clean时出现版本不匹配错误
Error:Gradle version 1.10 is required. Current version is 2.9. If using the gradle wrapper, try editing the distributionUrl in /Users/username/apps/android/project-android/gradle/wrapper/gradle-wrapper.properties to gradle-1.10-all.zip.Please fix the project's Gradle settings in Gradle settings
解决方法:
build.gradle文件中的
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}}
修改成
classpath 'com.android.tools.build:gradle:1.1.+'
gradle-wrapper.properties文件中的
istributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip
修改成本机安装的版本
distributionUrl=http\://services.gradle.org/distributions/gradle-2.9-all.zip
三、gradle build中文乱码问题
build.gradle文件中配置(放在最后面)
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
四、build.gradle文件说明
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.+'
}
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 16
buildToolsVersion "22.0.1"
signingConfigs {
**Config{
storeFile file("D:\\My Documents\\Documents\\mykey\\**keystore")
storePassword "***"
keyAlias "***"
keyPassword "***"
}
}
buildTypes{
release {
//混淆开关
minifyEnabled true
//是否zip对齐
zipAlignEnabled true
//是否打开debuggable开关
debuggable false
//是否打开jniDebuggable开关
jniDebuggable false
//混淆配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt')
//签名配置
signingConfig signingConfigs.**Config
}
}
//渠道
productFlavors {
free {
applicationId 'com.clarck.httpclientnew.free'
}
premium {
applicationId 'com.clarck.httpclientnew.premium'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
//批量打包--指定配置文件
//不同的渠道商对应的各自AndroidManifest
//AndroidManifest的application标签只用配置渠道号的值
//其他公共配置都放在公共的AndroidManifest.xml中
selfTest {
manifest.srcFile 'product/AndroidManifest_selfTest.xml'
}
MMstore {
manifest.srcFile 'product/AndroidManifest_MMstore.xml'
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
}
五、Gradle 管理Eclipse web项目时需要将jar包复制到lib目录下
task copyJars(type: Copy) {
from configurations.runtime
into 'lib' // 目标位置
}
来源:oschina
链接:https://my.oschina.net/u/2501904/blog/533926