美文网首页Android开发Android开发经验谈
Android 项目开发必备-建立属于你的build.gradl

Android 项目开发必备-建立属于你的build.gradl

作者: PaperKite | 来源:发表于2017-12-18 10:23 被阅读0次
timg (1).jpg

开发一个Android项目不仅仅需要你会写java/kotlin代码,而且你还要了解各种配置文件。例如。AndroidManifest.xml,混淆文件,build.gradle等。这里面最难理解也是最重要的非build.gradle莫属了,接下来我们就讲一讲一个成熟的项目的build.gradle文件是怎么样的:
首先我们建立一个新工程并打开build.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.ljw.basedemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}

这是AS默认给我们生成的build.gradle,我们需要对它进行一个改造:
如果你不需要单元测试我们可以删掉这些文件:

  // testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    //androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    //    exclude group: 'com.android.support', module: 'support-annotations'
    //})
  // testCompile 'junit:junit:4.12'

ok,删完了以后我们就可愉快的开始我们build.gradle的建立了,
首先,一个项目想要发布,签名是必要的东西,所以我们要配置一下签名文件:

    signingConfigs {
            storeFile file('../key/yourname.jks') //你的jks文件路径
            storePassword "youstorepassword"
            keyAlias "youAlias"
            keyPassword "yourpassword"
    }

如果你的项目够大那这句话是必须的

    // dex突破65535的限制
            multiDexEnabled true

接下来是配置我们的buildTypes,具体解释见注释

  buildTypes {
        debug {
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
            //混淆
            minifyEnabled false
            //Zipalign优化
            zipAlignEnabled false
            // 移除无用的resource文件
            shrinkResources false
            signingConfig null
        }

        release {
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
            minifyEnabled true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为ljw_v1.0__baidu.apk
                        def fileName = "ljw_v${variant.productFlavors[0].versionName}_${variant.name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

为gradle添加UTF-8支持

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

最后就是多渠道打包了

    productFlavors {
        liuliang {//这里你可以给不同渠道的包设置不同的版本号,名称,甚至applicationId,若是你设置不同的applicationId了,那你的不同渠道的包就可以同时存在在一个手机上。 }
        xiaomi { }
        wandoujia {}
        OPPO {}
        Lenovo { }
        Unicom {}
        qh360 { }
        VIVO {  }
        sougou { }
        anzhi { }
        leshi { }
        ppzhushou { }
        _91zhushou { }
        yingyonghui {  }
        baidu {}
        yingyongbao { }
        huawei { }
        meizu {  }
  
}

ok,基本上一个项目的build.gradle文件差不多就配置好了,如果有特殊需要你们在自己加东西。
下一篇文章预告-Android 项目开发必备-为你的项目选择优质框架

相关文章

网友评论

    本文标题:Android 项目开发必备-建立属于你的build.gradl

    本文链接:https://www.haomeiwen.com/subject/mqbdwxtx.html