Gradle 的配置

本文介绍如何配置Android项目的构建脚本以实现自动化签名、多渠道打包,并引入第三方库及资源管理。涵盖构建类型的设置、混淆策略及针对不同市场渠道的自定义配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

apply plugin: 'com.android.application'
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
    compileSdkVersion 23

    buildToolsVersion "23.0.3"

   引入第三方so库时需要配置sourceSets

    sourceSets {
        main {                         
        jniLibs.srcDirs = ['libs']                          
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"//编译时内存不足的问题

    }

   配置包名版本等

 defaultConfig {
        applicationId "com.packname"
        minSdkVersion 14
        targetSdkVersion 17
        versionCode 1
        versionName "1.0.0"

        // dex突破65535的限制
        multiDexEnabled true
        // 默认是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }

 自动签名打包

    signingConfigs {
        debug {
            keyAlias '。。。'
            keyPassword 'passwd'
            storePassword 'passwd'
            storeFile file('项目keystore')
        }
        release {
            keyAlias '...'
            keyPassword 'passwd'
            storePassword '1passwd'
            storeFile file('项目keystore.jks')
        }
    }

构建类型,分debug、release

buildTypes {
        debug {
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
            buildConfigField "boolean", "API_ENV", "true"


           buildConfigField "String", "API_SERVER_URL", "\"http://test...\""
           buildConfigField "String", "API_WEB_URL","\"http://test...\""
           buildConfigField "String", "API_3G_URL","\"http://www...\""


            buildConfigField "String", "IMAGE_TYPE", "\".jpg,.png,.gif\""
            versionNameSuffix "-debug"
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
            debuggable true
            
        }
        release {
         
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
            buildConfigField "boolean", "API_ENV", "false"

            buildConfigField "String", "API_SERVER_URL", "\"http://apps...""
            buildConfigField "String", "API_3G_URL","\"http://www...""
            buildConfigField "String", "API_WEB_URL","\"http://apps...\""

            buildConfigField "String", "IMAGE_TYPE", "\".jpg,.png,.gif\""
            debuggable false 
            minifyEnabled //使用混淆
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "v${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

</pre><pre>

  多渠道打包

productFlavors {
       baidu {}
        _360 {}
        wandoujia {}
        yingyongbao{}
        yingyonghui{}
        jifeng{}
        lenovo{}
        yidong{}
        huawei{}
        xiaomi {}
    }
   统一配置
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

第三方库依赖管理

.jar和.aar文件,添加.aar文件还需要配置respositories

repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
   //.aar文件
    compile name: ' 文件name', ext: 'aar'

}


### Gradle配置概述 在开发环境中,Gradle 是一种强大的构建工具,广泛应用于Java、Kotlin和其他编程语言的项目管理。对于Android项目的构建,通常通过`build.gradle`文件来进行详细的设置。 #### 修改Gradle安装路径 为了优化磁盘空间利用效率以及遵循最佳实践,推荐将Gradle及其缓存放置于非系统分区的位置[^1]。 #### 配置依赖库源 当需要从Maven仓库获取JAR包时,在`build.gradle`文件内应指定相应的远程仓库URL作为依赖解析位置。此操作涉及编辑repositories部分的内容,以确保能够正确访问所需的外部资源。 ```groovy repositories { mavenCentral() // 使用中央Maven仓库 } ``` #### 设置Gradle版本 针对特定需求调整所使用的Gradle版本是可行的;这主要取决于具体应用的要求或是团队内部的标准流程。更改位于项目根目录下的`gradle/wrapper/gradle-wrapper.properties`文件里的`distributionUrl`参数即可实现这一点[^2]。 ```properties distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip ``` #### 全局私服Nexus配置 除了单个项目级别的自定义外,还可以为整个环境设定统一的企业级私有存储库(如Nexus)。这类全局性的改动既能在各个单独的应用程序中体现出来,也能集中化管理和维护第三方组件的引入方式[^3]。 ```groovy allprojects { repositories { mavenLocal() maven { url 'http://your-nexus-server/repository/maven-releases/' } jcenter() } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值