Android Studio gradle的基本用法

来源:互联网 发布:win7 桌面 网络 编辑:程序博客网 时间:2024/06/09 18:08

通过gradle进行版本控制非常方便。尤其是对debug和release的切换及多渠道打包。

defaultConfig {        applicationId "xxxx"        minSdkVersion 14        targetSdkVersion 19        versionCode 1        versionName "1.0.0"        // 设置默认开发渠道        manifestPlaceholders = [UMENG_CHANNEL_VALUE:"default_channel"]        // 设置默认非debug模式        buildConfigField("boolean", "IS_DEBUG", "false")}buildTypes {    debug {      // 定义一个新的常量,使用BuildConfig.API_URL引用      buildConfigField "String", "API_URL", "http://api.dev.com/"      //使用BuildConfig.IS_DEBUG引用      buildConfigField "boolean", "IS_DEBUG", "true"      // net_type_debug字段写在gradle.properties中      buildConfigField "int", "RELEASE_TYPE", "${net_type_debug}"      // 定义一个新的res资源,string要小写,使用R.String.name引用      resValues "string", "name", "vaule"      resValues "boolean", "name", "value"      // 替换Manifest中的字段${UMENG_APP_KEY},${QQ_APP_ID}      manifestPlaceholders = [UMENG_APP_KEY:"xxxxxxx", QQ_APP_ID:"xxxxxx"]    }    release {      // 移除未使用的资源      shrinkResources true      // 是否混淆代码      minifyEnabled false      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'      buildConfigField "String", "API_URL", "http://api.prod.com/"      buildConfigField "boolean", "IS_DEBUG", "false"      buildConfigField "int", "RELEASE_TYPE", "${net_type_release}"      resValues "string", "name", "vaule"      resValues "boolean", "name", "value"      manifestPlaceholders = [UMENG_APP_KEY:"xxxxxxx", QQ_APP_ID:"xxxxxx"]    }}// 多渠道打包productFlavprs{  // 在此声明渠道名  C00_umeng{}  C01_BaiDu{}  ...  productFlavors.all { flavor ->        // 其中UMENG_CHANNEL_VALUE字段在Manifest中定义为${UMENG_CHANNEL_VALUE}        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]    }}// debug keystoresigningConfigs {    debug {        storeFile file('key/debug.keystore')    }}// 这里你可以进行 Java 的版本配置,以便使用对应版本的一些新特性。compileOptions {    sourceCompatibility JavaVersion.VERSION_1_7    targetCompatibility JavaVersion.VERSION_1_7}//执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。lintOptions {    abortOnError false}// 自动解析so文件task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {    destinationDir file("$projectDir/libs")    baseName "Native_Libs2"    extension "jar"    from fileTree(dir: "libs", include: "**/*.so")    into "lib"}tasks.withType(JavaCompile) {    compileTask -> compileTask.dependsOn(nativeLibsToJar)}

buildConfigField定义的常量存在于BuildConfig类中。BuildConfig为final类。存在于build文件夹下的buildConfig
resValues定义的res资源存在于generated.xml中。位在于build文件夹下的resValues

1 0