Android——Gradle 心得 、变量、 多渠道、一些坑

前言

新手安卓,也是刚开始接触AS,被gradle,虐的不行,满嘴 F***,真心没有感觉gradle丝毫方便,后来发现gradle的方便之处。被大佬要求去做多渠道。不得不硬着头皮去上了。搜了好多。Google了,看了好多,都是从好多截取下来才找到有用信息,太费时间了。所以自己打算写写,以便有其他新手遇到同样问题不再费事。第一篇博文请轻喷。。~~哈哈

正文

先看我的 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'newlens'
android {
    compileSdkVersion 19
    buildToolsVersion '23.0.0'
    sourceSets { main{
        jniLibs.srcDirs = ['libs'] }
    }
    defaultConfig {
        // 一些默认属性
        applicationId "com.0000.000"  //包名
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 4
        versionName "1.0"
        //strings 中的 @string/app_name 不要   负责这里
        // resValue app_name会无效,而是优先采用 strings中的<pre name="code" class="plain">        resValue "string", "app_name", "0000" //清单配置文件中 app_name 
        buildConfigField "boolean", "AUTO_UPDATES", "true"
        buildConfigField "String" ,"APPURL", "\"http://455557/jf/api/\""
    }
 
  
变量方法一 
这里 是为了 不同渠道,或者不同模式 release或者 debug 显示 不同appname
注意看 有坑啊 
开始我就按搜到的文章设置 发现我的无效,后来在搜一下 发现resValue的时候需要删除
values中的 stirng.xml    app_name   都删除了。如果有国际化也要删除相应的。
删除后并不影响  @string/app_name 使用。
之前设置的 appname 在这里用了。来看一下 mainfests
接着看代码
    
//    优先级 buildTypes >  productFlavors > defaultConfig
    buildTypes {
        // 这里的优先级最高 注意同productFlavors重复设置 问题 我这里 建议只设置IP属性 
   	// 你们可以根据业务进行调整 
        release {
        		//是否混淆
            minifyEnabled true
            //去除无用的 资源
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //正式版  国外
            buildConfigField "String" ,"APPURL", "\"http://45550.107/jf/api/\""
        }
        debug{
            //测试版  国内
//            buildConfigField "String" ,"APPURL", "\"http://115555172/jf/api/\""
//            leaf
//            buildConfigField "String" ,"APPURL", "\"http://1555255/jf/api/\""
//            国外
            buildConfigField "String" ,"APPURL", "\"http://55557/jf/api/\""
//            minifyEnabled true
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
一定要 注意优先级 问题,避免设的属性无效

//    signingConfigs {
//        release {
//            // relase签名
//            keyAlias '123'
//            keyPassword '123456'
//            storeFile file('is.jks')
//            storePassword '123456'
//        }
//    }
 我这里并没有用
signingConfigs 
而是直接用 build


目前用不到signingConfigs,其实是,大佬生成的是 .jks没有生成.keysotre,我不知道 .jks能不能
才这样使用,还有发现用build generate apk省事。就没弄了。
有人说直接把后缀.jks改下改为.keystore就行。
        //another app name
//    android.applicationVariants.all { variant ->
//        // get app_name field from defaultConfig
//        def appName = variant.mergedFlavor.resValues.get('app_name').getValue()
//        // concat new App name with each flavor's name
//        appName = "${appName}"
appname 要这样写 
用gradle的变量都这样写
${****}

继续看 
//        variant.productFlavors.each { flavor ->
//            appName += " ${flavor.name}"
//        }
//        // optionally add buildType name
//        appName += " ${variant.buildType.name}"
//        // your requirement: if buildType == debug, add DEV to App name
//        if(variant.buildType.name == "debug") {
//            appName += " DEV"
//        }
//        // set new resVale
//        variant.resValue 'string', 'app_name', appName
//    }
    productFlavors {
        //这里的参数 如果和 buildTypes冲突,默认优先使用buildTypes
        test {
            resValue "string", "app_name", "DEBUG"
            //包名修改 仅供安装两个app,jpush等会有包名错误,不建议设置
//            applicationId     "com.000.000.test"//包名修改
						//更新 设置 有些 渠道不能自更新 
            buildConfigField "boolean", "AUTO_UPDATES", "true"
        }
        testnoup {
            resValue "string", "app_name", "DEBUG"
            //包名修改 仅供安装两个app,jpush等会有包名错误,applicationid不建议设置
//            applicationId     "com.000.000.test"//包名修改
            buildConfigField "boolean", "AUTO_UPDATES", "false"
        }
        googleplay {
            buildConfigField "boolean", "AUTO_UPDATES", "false"
        }
    }
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [TD_CHANNEL_ID_VALUE: name]
    }
}

 productFlavors
这个是多渠道打包使用的一些参数设置

用友盟。talkingdata统计需要这样设置
<pre name="code" class="plain"> [TD_CHANNEL_ID_VALUE: name]
 
 
参考 详细内容下面链接

Gradle多渠道打包[umeng]----

变量方法二
这里其实就用了 gradle 定义变量的一种方法 
搞过多渠道的都知道,这个是一个简略的写法。 productFlavors.all

来看一下 mainfests 中 我怎么写的

变量方法三

我们再来看一下 代码中的 

 buildConfigField "boolean", "AUTO_UPDATES", "false"

 buildConfigField "boolean", "AUTO_UPDATES", "true"

这是 gradle中定义变量的另外一种方法 

因为有些渠道不能自更新。所以做使用。

看我java代码中操作、、

就这样用就行了 我的 checkOutVersion 是一个更新的方法。我就不再说了。

在其他java代码中 如果引用 ,就先在 build.gradle中写好 然后 

BuildConfig. (调用你命名的变量)

原来有个不错链接还有其中原理都很清楚,重要的是 !!我现在找不太到了,,先凑活看一下 这个,

http://stormzhang.com/android/2015/01/25/gradle-build-field/

三个变量方法

链接给你,看着我的结合这个,就清楚了。

细心看我写的。这个链接没把一些坑写出了。坑了我。

http://drakeet.me/gradle-change-android-resvalue-buildconfig-and-manifest

好了 就这样了。完整的 build.gradle 给你们粘贴一下

apply plugin: 'com.android.application'
apply plugin: 'newlens'
android {
    compileSdkVersion 19
    buildToolsVersion '23.0.0'
    sourceSets { main{
        jniLibs.srcDirs = ['libs'] }
    }
    defaultConfig {
        // 一些默认属性
        applicationId "com.0000.000"  //包名 我改了一下哈哈~~
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 4
        versionName "1.0"
        //strings 中的 @string/app_name 不要   负责这里
        // resValue app_name会无效,而是优先采用 strings中的
        resValue "string", "app_name", "0000" //清单配置文件中 app_name 
        buildConfigField "boolean", "AUTO_UPDATES", "true"
        buildConfigField "String" ,"APPURL", "\"http://455557/jf/api/\""
    }
//    优先级 buildTypes >  productFlavors > defaultConfig
    buildTypes {
        // 这里的优先级最高 注意同productFlavors重复设置 问题 建议只设置IP属性
        release {
        		//是否混淆
            minifyEnabled true
            //去除无用的 资源
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //正式版  国外
            buildConfigField "String" ,"APPURL", "\"http://45550.107/jf/api/\""
        }
        debug{
            //测试版  国内
//            buildConfigField "String" ,"APPURL", "\"http://115555172/jf/api/\""
//            leaf
//            buildConfigField "String" ,"APPURL", "\"http://1555255/jf/api/\""
//            国外
            buildConfigField "String" ,"APPURL", "\"http://55557/jf/api/\""
//            minifyEnabled true
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }
    }
//    signingConfigs {
//        release {
//            // relase签名
//            keyAlias '123'
//            keyPassword '123456'
//            storeFile file('is.jks')
//            storePassword '123456'
//        }
//    }
        //another app name
//    android.applicationVariants.all { variant ->
//        // get app_name field from defaultConfig
//        def appName = variant.mergedFlavor.resValues.get('app_name').getValue()
//        // concat new App name with each flavor's name
//        appName = "${appName}"
//        variant.productFlavors.each { flavor ->
//            appName += " ${flavor.name}"
//        }
//        // optionally add buildType name
//        appName += " ${variant.buildType.name}"
//        // your requirement: if buildType == debug, add DEV to App name
//        if(variant.buildType.name == "debug") {
//            appName += " DEV"
//        }
//        // set new resVale
//        variant.resValue 'string', 'app_name', appName
//    }
    productFlavors {
        //这里的参数 如果和 buildTypes冲突,默认优先使用buildTypes
        inftest {
            resValue "string", "app_name", "DEBUG"
            //包名修改 仅供安装两个app,如果设置jpush等会有包名错误,不建议设置 
//            applicationId     "com.000.000.test"//包名修改
						//更新 设置 有些 渠道不能自更新 
            buildConfigField "boolean", "AUTO_UPDATES", "true"
        }
        inftestnoup {
            resValue "string", "app_name", "DEBUG"
            //包名修改 仅供安装两个app,jpush等会有包名错误,applicationid不建议设置
//            applicationId     "com.000.000.test"//包名修改
            buildConfigField "boolean", "AUTO_UPDATES", "false"
        }
        shenzheninf {
        }
        olala {

        }
        googleplay {
            buildConfigField "boolean", "AUTO_UPDATES", "false"
        }
    }
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [TD_CHANNEL_ID_VALUE: name]
    }
}

// android.applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, defaultConfig.applicationId + "-" + buildType.name + "-v" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk" ); } }

//}
repositories {
    mavenCentral()
}

//apply plugin: 'com.bugtags.library.plugin'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //数据统计
    compile files('libs/TalkingdataAnalytics.jar')
    testCompile 'junit:junit:4.12'
    compile 'com.loopj.android:android-async-http:1.4.6'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    //    <!--listview动画 -->
    compile 'com.nhaarman.listviewanimations:library:2.6.0'
    //听云。性能检测
    compile 'com.networkbench.newlens.agent.android:nbs.newlens.agent:2.3.1'
    //bugtags
//    compile 'com.bugtags.library:bugtags-lib:1.1.0'
    //内存泄露分析
//    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
//    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}


附加

额外的一些坑
gradle,build的时候设置多个包名 (applicationId ) 遇到的自定义view找不到,这时候需要看一下你的 xml 要这样设置 

res-auto"

之前的 


设置不同的

  applicationId 之后要这样写


ps:开始我也是设置不同包名想着debug一个,正式一个。手机上装两个app,后来发现用处不大,就没再用了。如果有人

遇到多包名的 自定义view问题可以参考一下。


一本书 Packt.Gradle.for.Android

这本书 入门 可以 


链接奉上




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值