Android Studio 3.1.3  + gradle 4.4 + build:gradle:3.1.3

示例代码已上传github https://github.com/weixx/gradle-package

配置差别化渠道包

app/build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

def releaseTime() {

return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))

}

android {

// 签名文件

signingConfigs {

config {

keyAlias '111111'

keyPassword '111111'

storeFile file('C:\\Users\\Administrator\\Desktop\\11.jks')

storePassword '111111'

}

}

compileSdkVersion 28

defaultConfig {

// applicationId "com.xx"

minSdkVersion 18

targetSdkVersion 28

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

signingConfig signingConfigs.config

}

flavorDimensions "version"

// 多渠道/多环境 的不同配置

productFlavors {

dev {

// 每个环境包名不同

applicationId "com.xx.dev"

// 动态添加 string.xml 字段;

// 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!

resValue "string", "s1", "dev"

// 动态修改 常量 字段

buildConfigField "String", "ENVIRONMENT", '"dev"'

// 修改 AndroidManifest.xml 里的变量

manifestPlaceholders = [UMENG_CHANNEL_VALUE:"dev",

app_icon:"@mipmap/ic_launcher",

app_name:"dev_myapp"]

dimension "version"

}

stage {

applicationId "com.xx.stage"

resValue "string", "s1", "stage"

buildConfigField "String", "ENVIRONMENT", '"stage"'

manifestPlaceholders = [UMENG_CHANNEL_VALUE:"stage",

app_icon:"@mipmap/ic_launcher",

app_name:"stage_myapp"]

dimension "version"

}

prod {

applicationId "com.xx.prod"

resValue "string", "s1", "prod"

buildConfigField "String", "ENVIRONMENT", '"prod"'

manifestPlaceholders = [UMENG_CHANNEL_VALUE:"prod",

app_icon:"@mipmap/ic_launcher",

app_name:"prod_myapp"]

dimension "version"

}

}

//移除lint检测的error

lintOptions {

abortOnError false

}

buildTypes {

debug {

buildConfigField("boolean", "LOG_DEBUG", "true")

signingConfig signingConfigs.config

}

release {

buildConfigField("boolean", "LOG_DEBUG", "false")

zipAlignEnabled true

shrinkResources true

minifyEnabled true

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

signingConfig signingConfigs.config

// 批量打包,使用 .\gradlew assemble 命令编译打包所有配置的包

android.applicationVariants.all {

variant ->

variant.outputs.all {

outputFileName = rootProject.getName()+"_" + productFlavors[0].name + "_" + buildType.name + "_v" + defaultConfig.versionName + "-${releaseTime()}.apk"

}

}

}

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

AndroidManifest.xml

android:icon="${app_icon}"

android:label="${app_name}"

...>

android:name="UMENG_CHANNEL"

android:value="${UMENG_CHANNEL_VALUE}" />

创建差别化资源代码文件夹,为差别化APP指定特定的行为

main/res/values/colors.xml

#1B1A2A

#1B1A2A

#FF4081

dev/res/values/colors.xml

#1B1A2A

#888888

#FF4081

9fd55b166f134720332593965e1513f4.png

以上是为dev渠道的创建了一个特定的colors资源,在打包dev时所有的引用都将指向该文件,string等都类似与该道理,也可以再创建一个stage/res/values/colors.xml为stage指定特定的资源。

参考

https://blog..net/aiynmimi/article/details/68944830

https://www.jianshu.com/p/533240d222d3

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐