Home gradle整理
Post
Cancel

gradle整理

1. gradlew 常用命令整理

  • gradlew -v 版本号
  • gradlew clean 清除工程目录下的build文件夹
  • gradlew build 检查依赖并编译打包

这里注意的是 gradlew build 命令把debugrelease环境的包都打出来,如果正式发布只需要打Release的包,该怎么办呢,下面介绍一个很有用的命令 assemble, 如

  • gradlew assembleDebug(缩写: gradlew aD) 编译并打Debug包
  • gradlew assembleRelease(缩写: gradlew aR) 编译并打Release的包

还有打渠道包,如百度 gradlew assembleBaiduRelease 编译并打Release的百度包

除此之外,assemble还可以和productFlavors结合使用,比如定义了 installRelease ,uninstallRelease 两个productFlavors,则可以如下命令:

  • gradlew installRelease(缩写: gradlew iR) Release 模式打包并安装
  • gradlew uninstallRelease 卸载Release模式包

2. gradlew compile优化

  1. gradlew aR –daemon –parallel(交叉依赖不能用这个) -x lint -x test(去掉lint test任务) –offline(使用本地缓存)

  2. 减少module的直接依赖,改用引用aar

  3. uploadArchives

    1. 发布所有module: gradlew uploadArchives

    2. 发布单个module: gradlew :module_name:uploadArchives

    3. 上传配置:

      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
      
      //1. 在项目根目录创建maven_push.gradle文件
      apply plugin: 'maven'
      apply plugin: 'signing'
            
      configurations {
          deployerJars
      }
            
      repositories {
          mavenCentral()
      }
            
      // 判断版本是Release or Snapshots
      def isReleaseBuild() {
          return !VERSION.contains("SNAPSHOT");
      }
            
      // 获取仓库url
      def getRepositoryUrl() {
          return isReleaseBuild() ? RELEASE_URL : SNAPSHOT_URL;
      }
            
      uploadArchives {
          repositories {
              mavenDeployer {
                  beforeDeployment {
                      MavenDeployment deployment -> signing.signPom(deployment)
                  }
            
                  pom.version = VERSION
                  pom.artifactId = ARTIFACT_ID
                  pom.groupId = GROUP_ID
                  pom.packaging = 'aar'
            
                  repository(url: getRepositoryUrl()) {
                      authentication(userName: NAME, password: PASSWORD) // maven授权信息
                  }
              }
          }
      }
            
      // 进行数字签名
      signing {
          // 当 发布版本 & 存在"uploadArchives"任务时,才执行
          required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
          sign configurations.archives
      }
            
      //2. 在gradle.properties中配置常量:
      RELEASE_URL=http://www.example.com/
      NAME=admin
      PASSWORD=admin123
            
      //3. 在对应的module中的build.gradle中加入
      apply from: '../maven_push.gradle'
      

3. gradle 常用配置模板

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def fileProviderName = "com.ejin.example.fileprovider"
def fileProviderNameDebug = "com.ejin.example.fileprovider.debug"

android {
    compileSdkVersion 24
    buildToolsVersion "27.0.1"
    defaultConfig {
        applicationId "com.ejin.example"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        buildConfigField("String", "BASE_URL", "\"http://release.example.com/\"")
        manifestPlaceholders = [
                "FILE_PROVIDER_NAME": fileProviderName,
                "BAIDU_APP_KEY": "***",
        ]
        ndk {
            //jpush --- 选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }
    }

    lintOptions {
        ignoreWarnings true
        disable "MissingTranslation"
        disable "ResourceType"
    }

    signingConfigs {
        releaseConfig {
            storeFile file("./keystore/release.keystore")
            storePassword '1234'
            keyAlias 'key'
            keyPassword '1234'
        }
        debugConfig {
            storeFile file("./keystore/release.keystore")
            storePassword '1234'
            keyAlias 'key'
            keyPassword '1234'
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            //Zipalign优化
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            signingConfig signingConfigs.releaseConfig
            resValue("string","app_name","RELEASE")

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField("boolean", "APPDEBUG", "false")
        }

        debug {
            applicationIdSuffix ".dev"
            signingConfig signingConfigs.debugConfig
            resValue("string","app_name","DEV")

            buildConfigField("boolean", "APPDEBUG", "true")
            buildConfigField("String", "BASE_URL", "\"http://dev.example.com/\"")
            manifestPlaceholders = [
                    "FILE_PROVIDER_NAME": fileProviderNameDebug,
                    "BAIDU_APP_KEY": "***",
            ]
        }
    }

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

    applicationVariants.all {
        variant ->
            variant.outputs.all {
                outputFileName = "name_${variant.buildType.name}_v${variant.versionName}_${buildTime()}.apk"
            }
    }
}

def buildTime() {
    def df = new SimpleDateFormat("yyyyMMddHHmm")
    df.setTimeZone(TimeZone.getDefault())
    return df.format(new Date())
}

dependencies 
    api fileTree(include: ['*.jar'], dir: 'libs')
    api 'com.android.support:appcompat-v7:24.2.1'
}
This post is licensed under CC BY 4.0 by the author.