超实用!打包、配置项目必备的Gradle实战技巧!-创新互联

官方参考

成都网站制作、成都做网站的开发,更需要了解用户,从用户角度来建设网站,获得较好的用户体验。成都创新互联公司多年互联网经验,见的多,沟通容易、能帮助客户提出的运营建议。作为成都一家网络公司,打造的就是网站建设产品直销的概念。选择成都创新互联公司,不只是建站,我们把建站作为产品,不断的更新、完善,让每位来访用户感受到浩方产品的价值服务。

https://developer.android.com/studio/build/gradle-tips.html

以下都是经过实战总结、多次调整的gradle配置方案,欢迎采用,提意见

一 自动命名打包的不同版本

applicationVariants.all { variant ->
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
if ("debug" == variant.buildType.name) {
def apkFile = new File(
output.outputFile.getParent(),
"XXX_App_${variant.versionName}_${buildTime()}_debug.apk")
output.outputFile = apkFile
} else if ("release" == variant.buildType.name) {
def apkFile = new File(
output.outputFile.getParent(),
"XXX_App_${variant.versionName}_release.apk")
output.outputFile = apkFile
}
}
}

if (variant.buildType.name == "debug") {
variant.mergedFlavor.versionName = variant.mergedFlavor.versionName + "." + buildTime();
}

}
def buildTime() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMdd')
    return formattedDate
}

二 统一全module的配置

在build.gradle中改写引用方式

 compileSdkVersion rootProject.ext.android["compileSdkVersion"]

在config.gradle中配置如下

ext {
android = [
compileSdkVersion: 23,
buildToolsVersion: "25.0.0",
minSdkVersion : 15,
targetSdkVersion : 22,
minifyEnabled : false,
minifyFile : rootProject.file('proguard-rules.pro'),
]

version = [
"supportVersion" : "25.0.0"
]

dependencies = [
"support_v4": "com.android.support:support-v4:${version["supportVersion"]}",
"appcompat-v7" : "com.android.support:appcompat-v7:${version["supportVersion"]}",
"recycleview" : "com.android.support:recyclerview-v7:${version["supportVersion"]}",
"multidex" : "com.android.support:multidex:1.0.1",
"junit" : "junit:junit:4.12",

//Baidu location
"baidu" : "libs/BaiduLBS_Android.jar"
] }

三 混合使用productFlavors 和 meta

manifestPlaceholders 可以在清单文件中配置好,格式如下

    
            android:name="appid"
            android:value="12334"/>

同时在build.gradle中引用

productFlavors {

        normal {
            applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

        productone {
 applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

        producttwo {
       
 applicationId "com.xxx"
            manifestPlaceholders = [xxx: "0", appid: "xxx" , host:"xxx"];
        }

    }

配置不同product的资源和jni来源

sourceSets{
        normal {
            jniLibs.srcDirs = ['src/normalIn/jniLibsIn/']
            assets.srcDirs = ['src/normalIn/assetsIn']
        }

        productone {
            jniLibs.srcDirs = ['src/normalIn/jniLibsIn/']
            assets.srcDirs = ['src/normalIn/assetsIn']
        }
    }

代码中调用方法:

  try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (info != null && info.metaData != null) {
                meta = info.metaData.getInt("appid");
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


四 flavor版本判断

// build.gradle in application module

android {
    productFlavors {
        googlePlay {
        }
        wandoujia {
        }
    }
}
if (getGradle().getStartParameter().getTaskRequests().toString().contains("GooglePlay")) {
    // Google Play 版本才应用该插件
    apply plugin: 'com.google.gms.google-services'
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:超实用!打包、配置项目必备的Gradle实战技巧!-创新互联
浏览路径:http://csdahua.cn/article/djeehi.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流