Android gradle配置

project下的build.gradle文件配置

buildscript {
    repositories {
        maven{
            url "http://.../content/repositories/thirdparty" //maven私服上存放第三方jar包的地址
        }
        maven{
            url "http://.../content/groups/public"//maven私服上存放gradle jar包的地址
        }
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        //此处不同版本的gradle是导包不成功的原因之一,可手动改成自己本地的gradle版本,如1.3.0
    }
}

allprojects {
    repositories {
        maven{
            url "http://.../content/repositories/thirdparty"
        }
        maven{
            url "http://.../content/groups/public"
        }
        mavenCentral()
    }
}

app 下的build.gradle文件配置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "包名"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0.1"
    }
    signingConfigs {
         //打包的数字签名及相关密码
        releaseConfig {
            keyAlias ' '
            keyPassword ' '
            storeFile file('../...jks')
            storePassword ''
        }
    }

    buildTypes {
        debug {
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
            versionNameSuffix "-debug"
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }
        release {
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
            //对打包的应用程序进行优化
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            //是否代码混淆
            minifyEnabled false
            //代码混淆文件的位置
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //数字签名
            signingConfig signingConfigs.releaseConfig
        }
    }

    //java版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    //lint检查开关
    lintOptions {
        abortOnError false
    }

    //打包配置 移除一些文件(百度地图、高德地图的相关文件)
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'assets/lineDashTexture.png'
    }
    //多渠道打包,可以动态替换包名及manifest文件中高德地图的app_key
    //manifest文件中的高德地图appkey由如下value值代替
    //<meta-data
    // android:name="com.amap.api.v2.apikey"
    // android:value="${GAODE_MAP_APPKEY}" />
    productFlavors {
        name1{
            applicationId = "包名一"
            manifestPlaceholders = [GAODE_MAP_APPKEY: "不同包名的高德地图app_key"]
        }
        name2{
            applicationId = "包名二"
            manifestPlaceholders = [GAODE_MAP_APPKEY: "不同包名的高德地图app_key"]
        }
    }

    //修改生成的apk名字
    applicationVariants.all{ variant->
        variant.outputs.each { output->
            def oldFile = output.outputFile
            def newName = ''
            if (variant.productFlavors[0].name.equals('name1')) {
                newName = "newName1"
            } else {
                newName = "newName2"
            }
            if(variant.buildType.name.equals('release')){
                def releaseApkName = newName + '-release.apk'
                def file = output.outputFile
                output.outputFile = new File(file.parent, releaseApkName)
            } else if(variant.buildType.name.equals('debug')){
                def releaseApkName = newName + '-debug.apk'
                def file = output.outputFile
                output.outputFile = new File(file.parent, releaseApkName)
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1+'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'
    //latest.integration获取服务器最新版本
    //依赖自己上传到maven私服上的第三方jar包
    compile 'com.test.yjs:volley:latest.integration'
    compile 'com.jakewharton:butterknife:7.0.1'
}

进入项目根目录,执行gradle assembleRelease 即可一次打包多种不同渠道的apk包

你可能感兴趣的:(android,gradle)