Android基础 Gradle脚本使用
2019-12-18 本文已影响0人
LiuJP
shell 中的参数怎么传递
shell中
./gradlew collectSDK -Pflavor=gionee
gradle脚本
def flavor = project.hasProperty('flavor') ? flavor : "common"
全局配置文件怎么传递
全局build.gradle
包括:
1、Properties的读取
2、中文字符串处理
Properties properties = new Properties()
def setting0 = project.properties.get('setting')
if (setting0 == null || setting0.isEmpty()) {
InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream()
if (inputStream.readBoolean()) {
properties.load(inputStream)
setting0 = properties.getProperty('setting')
}
}
if (setting0 != null) {
properties.load(new File(rootDir.getAbsolutePath() + "/properties/" + setting0 + ".properties").newDataInputStream())
} else {
properties.load(new File(rootDir.getAbsolutePath() + "/sdk/base.properties").newDataInputStream())
}
def name = new String(properties.getProperty("name", "").getBytes("ISO-8859-1"), "utf-8")
ext {
config = [
applicationId: properties.getProperty("applicationId").toString(),
NAME : name,
TARGET0 : properties.getProperty("TARGET0").toString(),
VERSION0 : properties.getProperty("VERSION0").toString(),
SUPPORT0 : properties.getProperty("SUPPORT0").toString(),
target : properties.getProperty("target").toString(),
so : properties.getProperty("so").toString(),
versionCode : properties.getProperty("versionCode").toString(),
SupportEngine: '150'
]
dependencies = [
setting: setting0,
ui : properties.getProperty("ui").toString()
]
android = [
compileSdkVersion: 26,
buildToolsVersion: "26.0.2",
minSdkVersion : 16,
targetSdkVersion : 22
]
}
app中的build.gradle
包括:
1、manifest中的${}的对象
2、BuildConfig中对象
3、全局gradle中的对象
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
applicationId applicationId0
manifestPlaceholders = [
icon : "@mipmap/ic_launcher",
NAME : NAME,
TARGET0 : TARGET0,
VERSION0 : VERSION0,
SUPPORT0 : SUPPORT0,
SupportEngine: SupportEngine
]
buildConfigField "String", "TARGET0", ('"' + TARGET0 + '"')
buildConfigField "String", "so", ('"' + so + '"')
versionCode Integer.valueOf(versionCode0)
versionName "1.0." + versionCode
ndk {
abiFilter "armeabi"
}
}
文件目录指向
还包括:
- assets
- jni
- jniLibs
- aidl
sourceSets {
main {
if (isAddShell) {
java.srcDirs = ['src/']
manifest.srcFile 'src/shell/AndroidManifest.xml'
} else if (isRewardAd) {
java.srcDirs = ['src/reward/java']
res.srcDirs = ['src/reward/res']
resources.srcDirs = ['src/reward/res']
manifest.srcFile 'src/reward/AndroidManifest.xml'
} else if (isIPlugin) {
java.srcDirs = ['src/plugin/java']
res.srcDirs = ['src/plugin/res']
resources.srcDirs = ['src/plugin/res']
manifest.srcFile 'src/plugin/AndroidManifest.xml'
}
}
}
gradle任务Task
- exec
- copy
//在执行以后
project.afterEvaluate {
tasks.forEach() {
println ' -----> it.name:' + it.name
if (it.name.equalsIgnoreCase('assembleRelease')) {
it.doLast {
println ' -----> all task:====================================Release'
println ' outputApkFileName' + outputApkFileName
def apkPath = project.buildDir.absolutePath + '/outputs/apk/release/' + outputApkFileName
println apkPath
project.exec {
executable = 'java'
args += '-jar'
args += shelltoolspaht
args += apkPath
args += shellapkapth
out
println("args = " + args)
}
def intoFile = project.rootDir.getAbsolutePath() + "/release/"
def fromFile = project.buildDir.getAbsolutePath() + "/outputs/apk/release/"
File file1 = new File(intoFile);
if (!file1.exists())
file1.mkdir()
def files = file(fromFile).listFiles().sort()
files.each { File file ->
if (file.isDirectory()) {
def files_1 = file.listFiles().sort()
files_1.each { File files_2 ->
if (files_2.getName().contentEquals(outputApkFileName)) {
// files_2.renameTo(intoFile+chName+outputFileName)
copy {
from files_2
into intoFile
println(' --> cpy apk ' + files_2);
}
}
}
}
}
println("=====================buildMyListFile.doLast success.=========================")
}
}
if (it.name.equalsIgnoreCase('assembleDebug')) {
it.doLast {
println ' -----> all task:====================================Debug'
println ' outputApkFileName' + outputApkFileName
def apkPath = project.buildDir.absolutePath + '/outputs/apk/debug/' + outputApkFileName
println apkPath
project.exec {
executable = 'java'
args += '-jar'
args += shelltoolspaht
args += apkPath
args += shellapkapth
out
println("args = " + args)
}
}
}
}
}