Android Studio工程配置

1.gradle android 版本统一管理
1)在工程目录下build.gradle里面声明

ext {
    config = [
            minSdkVersion        : 19,
            targetSdkVersion     : 24,
            compileSdkVersion    : 26,
            buildToolsVersion    : "26.0.2",
            versionName              : "5.5.1",
            versionCode              : 551,
            supportLibraryVersion: "26.0.0",
            develop_version_code :  551,
            develop_version_name : "5.5.1.1",
            publish_version_code :  551,
            publish_version_name : "5.5.1"
    ]
}

2)在module下面的build.gradle里面编写

defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion config.minSdkVersion
        targetSdkVersion config.targetSdkVersion
        versionCode config.versionCode
        versionName config.versionName
        flavorDimensions "versionCode"
    }
dependencies {
     compile "com.android.support:recyclerview-v7:$config.supportLibraryVersion"
}

2.signingConfigs签名配置

 signingConfigs {
        sign {
            keyAlias 'xxx'
            keyPassword 'xxx'
            storeFile file('key/xxx.keystore')
            storePassword 'xxx'
        }
    }

注意事项:需要放置在引用到之前
3.productFlavors 多版本、多环境

productFlavors {
        develop {
            applicationId "xxx.xxx.xxx_dev"
            versionCode config.develop_version_code
            versionName config.develop_version_name
            buildConfigField("String", "API_HOST", "\"www.local.xxx.com/api/\"")
        }
        publish {
            applicationId "xxx.xxx.xxx"
            versionCode config.publish_version_code
            versionName config.publish_version_name
            buildConfigField("String", "API_HOST", "\"www.xxx.com/api/\"")
        }
    }

java代码

import com.xxx.xxx.BuildConfig;
public static String SERVER_DOMAIN = BuildConfig.API_HOST;

各个环境app name修改
src目录下分别创建各flavor name文件夹,然后创建res->values->string.xml
4.manifest文件中使用占位符

android:authorities="${applicationId}.DataProvider"

5.多版本打包

android.applicationVariants.all { variant ->
    variant.outputs.all { output ->
        variant.productFlavors.each { flavor ->
            def project = "xxx"
            def separator = "_"
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def versionName = variant.versionName
            def versionCode = variant.versionCode
            def date = new Date()
            def formattedDate = date.format('yyyyMMdd')

            def apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + ".apk"
            if (buildType == "release") {
                apkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + flavor.name + separator + formattedDate + ".apk"
            }
            outputFileName = apkName
        }
    }
}

运行 gradlew assembleRelease

你可能感兴趣的:(Android Studio工程配置)