gradle构建脚本优化

# 常量设置

gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
#android.enableJetifier=true

# Project All Config For Gradle
COMPILE_SDK_VERSION=30

MIN_SDK_VERSION=20
TARGET_SDK_VERSION=23

GRADLE_VERSION=7
GRADLE_PLUGIN_VERSION=4.2.0

VERSION_CODE = 20200101
VERSION_NAME = 0.0.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 公共build相关脚本封装

注意:这里的读取签名配置是读取外部的设置处理的;【要点】

build.gradle

def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+8"))
}

android {
    compileSdkVersion 30
    defaultConfig {
       //applicationId "cn.samyz.yessz.xxx"
        minSdkVersion MIN_SDK_VERSION
        targetSdkVersion TARGET_SDK_VERSION
        versionCode VERSION_CODE as int
        versionName VERSION_NAME
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        debug {
        }
        release {
        }
    }

    buildTypes {
        debug {
            zipAlignEnabled true
            shrinkResources false
            minifyEnabled false
            signingConfig signingConfigs.debug
        }
        release {
            buildConfigField "boolean", "LOG_DEBUG", "false"
            debuggable false
            zipAlignEnabled true
            shrinkResources true
            minifyEnabled false
            signingConfig signingConfigs.release
//          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    def fileName = outputFile.name.replace(".apk", "-${VERSION_NAME}-${releaseTime()}.apk")
                    outputFileName = fileName
                }
            }
        }
    }
}

File propFile = file('D:\\samy\\and\\key-cfg\\cfg\\app-signature.properties');
if (propFile.exists()) {
    def Properties props = new Properties()
    props.load(new FileInputStream(propFile))
//    println props
    if (props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
            props.containsKey('KEY_ALIAS') &&
            props.containsKey('KEY_PASSWORD')) {
        android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']

        android.signingConfigs.debug.storeFile = file(props['STORE_FILE'])
        android.signingConfigs.debug.storePassword = props['STORE_PASSWORD']
        android.signingConfigs.debug.keyAlias = props['KEY_ALIAS']
        android.signingConfigs.debug.keyPassword = props['KEY_PASSWORD']
    } else {
        android.buildTypes.release.signingConfig = null
        android.buildTypes.debug.signingConfig = null
    }
} else {
    android.buildTypes.release.signingConfig = null
    android.buildTypes.debug.signingConfig = null
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

# 全局其他设置相关

global.gradle

android {
  sourceSets {
    main {
      jniLibs.srcDirs = ['libs']
    }
  }
}


repositories {
  flatDir {
    dirs 'libs'
  }
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
//  compile(name:'common-release', ext:'aar')
//  compile(name:'net-release', ext:'aar')
//  compile(name:'img-release', ext:'aar')
//  compile(name:'db-release', ext:'aar')
//  compile(name:'ui-release', ext:'aar')
//  compile(name:'frame-release', ext:'aar')
//  compile(name:'ext-release', ext:'aar')
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 构建引入设置

apply plugin: 'com.android.application'
apply from: '../gradle/global.gradle'
apply from: '../gradle/build.gradle'

android {
    defaultConfig {
        applicationId "cn.samyz.yessz.xxx"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
//    implementation 'androidx.appcompat:appcompat:1.0.0'
//    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.annotation:annotation:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# 构建生产后的效果

上次更新: 2022/04/15, 05:41:31
×