依赖管理
1.在项目根目录新建config.gradle文件:
ext {
android = [
compileSdkVersion: 23,
buildToolsVersion: "23.0.2",
minSdkVersion : 14,
targetSdkVersion : 23,
]
dependencies = [
"appcompat-v7" : "com.android.support:appcompat-v7:23.4.0",
"design" : "com.android.support:design:23.4.0",
"retrofit" : "com.squareup.retrofit2:retrofit:2.1.0",
"converter-gson" : "com.squareup.retrofit2:converter-gson:2.1.0",
"adapter-rxjava" : "com.squareup.retrofit2:adapter-rxjava:2.1.0",
"logging-interceptor" : "com.squareup.okhttp3:logging-interceptor:3.3.1",
"rxjava" : "io.reactivex:rxjava:1.1.8",
"rxandroid" : "io.reactivex:rxandroid:1.2.1",
"butterknife" : "com.jakewharton:butterknife:8.2.1",
"butterknife-compiler" : "com.jakewharton:butterknife-compiler:8.2.1",
"glide" : "com.github.bumptech.glide:glide:3.7.0",
"glide-transformations": "jp.wasabeef:glide-transformations:2.0.1"
]
}
2.在Project的build.gradle中引用:
apply from: "config.gradle"
3.在Module的build.gradle中引用:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
applicationId "com.skyzone.netdemomvp"
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode 1
versionName "1.0"
buildConfigField 'String', 'API_SERVER_URL', '"http://my.api/"'
buildConfigField "boolean", "AUTO_UPDATES", "true"
}
signingConfigs {//签名的配置
release {
storeFile file("***.jks")
storePassword '***'
keyAlias '***'
keyPassword '***'
}
}
buildTypes {
debug {
buildConfigField "boolean", "LOG_DEBUG", "true"
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
buildConfigField "boolean", "LOG_DEBUG", "false"
debuggable true
minifyEnabled true
useProguard true
//if need remove unless resource?note:must use shrinkResources with minifyEnabled
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
// resValue "string", "loading", "jax_loading"
applicationVariants.all { variant ->
if (variant.buildType.name.equals('release')) {
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "jax_demo_v${defaultConfig.versionName}_${releaseTime()}_${variant.flavorName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
//针对很多渠道
productFlavors.all { flavor ->
manifestPlaceholders.put("UMENG_CHANNEL_VALUE", name)
}
}
}
productFlavors {//多渠道打包,命令行打包:gradlew assembleRelease
xiaomi {
applicationId 'com.skyzone.netdemomvpxiaomi'
buildConfigField "boolean", "AUTO_UPDATES", "false"
// manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
// manifestPlaceholders.put("UMENG_CHANNEL_VALUE", 'xiaomi')
}
//gradlew assembleGooglePlayRelease
googleplay {
applicationId 'com.skyzone.netdemomvpgoogleplay'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile rootProject.ext.dependencies["appcompat-v7"]
compile rootProject.ext.dependencies["design"]
//net
compile rootProject.ext.dependencies["retrofit"]
compile rootProject.ext.dependencies["converter-gson"]
compile rootProject.ext.dependencies["adapter-rxjava"]
compile rootProject.ext.dependencies["logging-interceptor"]
compile rootProject.ext.dependencies["rxjava"]
compile rootProject.ext.dependencies["rxandroid"]
//inject
compile rootProject.ext.dependencies["butterknife"]
apt rootProject.ext.dependencies["butterknife-compiler"]
//img
compile rootProject.ext.dependencies["glide"]
compile rootProject.ext.dependencies["glide-transformations"]
}
def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))}
签名打包
//打包所有渠道的debug apk
gradlew assembleDebug
//打包所有渠道的release apk
gradlew assembleRelease
//仅打包GooglePlay渠道的release apk
gradlew assembleGooglePlayRelease
//仅打包GooglePlay渠道的debug apk
gradlew assembleGooglePlayDebug
//打包GooglePlay渠道的debug/release apk
gradlew assembleGooglePlay
Note:gradlew assemblerelease could not find tools.jar Solve:设置环境变量JAVA_HOME,重新启动。