Androd Studio用 gradle 构建项目
项目依赖关系如下:
一个公共Jar工程WhaleySystem,需要先编译出来WhaleySystem.jar,才能编译主App和无屏App_K.
1. settings.gradle文件
include ':eYourChat'
include ':WhaleySystem'
include ':eYourChat_K'
eYourChat 和 eYourChat_K 是(application)App,WhaleySystem 是 jar (library)工程。
2. build.gradle (Project: eYourChat_as )
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
此文件是整个Project的build.gradle,即根目录中的build.gradle
3. build.gradle (Module: eYourChat)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "26.0.3"
defaultConfig {
applicationId "com.xxx"
minSdkVersion 14
targetSdkVersion 22
versionCode 47
versionName 'eChat2.08.10.T10.05'
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${defaultConfig.versionName}_${releaseTime()}_${variant.name}.apk"
}
}
//multiDexEnabled = true
}
signingConfigs {
eChat {
keyAlias 'mcptt'
keyPassword 'Gotaxxxx'
storeFile file('eChat.keystore')
storePassword 'Gotaxxxx'
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
signingConfig signingConfigs.eChat
}
debug {
minifyEnabled false
signingConfig signingConfigs.eChat
}
}
sourceSets {
main{
jniLibs.srcDirs = ['libs']
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/baidumapapi_base_v4_1_1.jar')
compile files('libs/baidumapapi_map_v4_1_1.jar')
compile files('libs/baidumapapi_search_v4_1_1.jar')
compile files('libs/baidumapapi_util_v4_1_1.jar')
compile files('libs/Gotasystem.jar')
compile files('libs/greendao-1.3.7.jar')
compile files('libs/gson-2.2.1.jar')
compile files('libs/isoviewer-1.0-RC-27.jar')
compile files('libs/locSDK_6.13.jar')
compile files('libs/okhttp-3.6.0.jar')
compile files('libs/okio-1.11.0.jar')
compile files('libs/picasso-2.4.0.jar')
compile files('libs/systembartint-1.0.4.jar')
// compile files('libs/multidex-1.0.2.jar')
//compile project(':gotaSystem')
}
def releaseTime() {
return new Date().format("yyyyMMdd-hhmmss")
}
此文件是Module eYourChat 有屏APP的 build.gradle文件
4. build.gradle (Module: eYourChat_K)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "26.0.3"
defaultConfig {
applicationId "com.ztegota.e180xx"
minSdkVersion 8
targetSdkVersion 17
versionCode 36
versionName 'eChat2.08.10.K9'
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${defaultConfig.versionName}_${releaseTime()}_${variant.name}.apk"
}
}
}
signingConfigs {
eChat {
keyAlias 'mcptte180'
keyPassword 'gotaxxxx'
storeFile file('e180Mcpttkeystore')
storePassword 'gotaxxxx'
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
signingConfig signingConfigs.eChat
}
debug {
minifyEnabled false
signingConfig signingConfigs.eChat
}
}
sourceSets {
main{
jniLibs.srcDirs = ['libs']
}
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/Gotasystem.jar')
compile files('libs/greendao-1.3.7.jar')
compile files('libs/gson-2.2.1.jar')
compile files('libs/locSDK_6.13.jar')
compile files('libs/okhttp-3.6.0.jar')
compile files('libs/okio-1.11.0.jar')
}
def releaseTime() {
return new Date().format("yyyyMMdd-hhmmss")
}
此文件是Module eYourChat_K 无屏APP的 build.gradle文件
5. build.gradle (Module: WhaleySystem)
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "26.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/aXMLRPC.jar')
compile files('libs/fastjson-1.1.23.jar')
compile files('libs/gcm.jar')
compile files('libs/goso_framework.jar')
compile files('libs/greendao-1.3.7.jar')
compile files('libs/gson-2.2.1.jar')
compile files('libs/locSDK_6.13.jar')
compile files('libs/LteRilLib.jar')
compile files('libs/okhttp-3.6.0.jar')
compile files('libs/okio-1.11.0.jar')
compile files('libs/pinyin4j-2.5.0.jar')
}
task buildJar(dependsOn: ['build'], type: org.gradle.jvm.tasks.Jar) {
appendix = 'demo'
baseName = 'androidJar'
version = '1.0.0'
classifier = 'release'
extension = 'jar'
archiveName = 'Gotasystem.jar'
destinationDir = file('../eChat/libs/')
def srcClassDir = [project.buildDir.absolutePath + '/intermediates/classes/release'];
from srcClassDir
exclude 'com/ztegota/BuildConfig.class'
//exclude '**/R.class'
//exclude '**/R\$*.class'
}
task buildJar_K(dependsOn: ['build'], type: org.gradle.jvm.tasks.Jar) {
appendix = 'demo'
baseName = 'androidJar'
version = '1.0.0'
classifier = 'release'
extension = 'jar'
archiveName = 'Gotasystem.jar'
destinationDir = file('../eChat_K/libs/')
def srcClassDir = [project.buildDir.absolutePath + '/intermediates/classes/release'];
from srcClassDir
exclude 'com/ztegota/BuildConfig.class'
//exclude '**/R.class'
//exclude '**/R\$*.class'
}
此文件是 Module WhaleySystem 公共jar工程文件的build.gradle。自定义了两个Task: buildJar /(buildJar_K) 用于拷贝编译好的jar到相应的lib目录下。
先编译jar包,在Android Studio 面板右上角有个Gradle图标,进入WhaleySystem,选择Tasks –> other –> buildJar / buildJar_K,可自动生成jar文件到有屏App / 无屏App目录下。
然后再执行 Build –> Rebuild Project 编译命令。
………………………
以上仅为经验之谈,仅供参考。