admin管理员组文章数量:1532282
前言
为什么用到多渠道,多包名?很多外包公司给客户出售互联网产品,这个时候就有了一劳永逸,千篇一律的好东西,那就是app框架,代码,业务逻辑基本一样,唯一不同的就是图片啊、名称啊、颜色啊、logo啊等一些让用户看上去比较自定义滴东西,这样外包公司可以不用给每一个客户都开发一遍app,只需要换一波资源文件,做一些特定的配置,很快就能出一个全新的APP出来,极大的降低了开发成本。
Gradle 配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
flavorDimensions "tommy"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
manifestPlaceholders = [JPUSH_APPKEY: "@string/string_jpush_app_key", JPUSH_CHANNEL: "@string/string_jpush_channel"]
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
zipAlignEnabled true
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
zipAlignEnabled false
shrinkResources false
minifyEnabled false
}
}
//多渠道配置
productFlavors {
app1 {
applicationId "com.tommy.app1"
versionName "1.0.0"
manifestPlaceholders = [JPUSH_PKGNAME: applicationId]
}
app2 {
applicationId "com.tommy.app2"
versionName "1.0.0"
manifestPlaceholders = [JPUSH_PKGNAME: applicationId]
}
}
//jenckins 自动化多渠道打包配置
applicationVariants.all { variant ->
variant.outputs.all { output ->
def newName
newName = "${applicationId}-" + variant.buildType.name + ".apk"
outputFileName = new File(newName)
println "outputPath:" + variant.getPackageApplication().outputDirectory
}
variant.assemble.doLast {
println "outputPath:" + variant.getPackageApplication().outputDirectory + "/output.json"
delete "${variant.getPackageApplication().outputDirectory}/output.json"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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'
}
注意事项:
1.单个应用的包名是在 defaultConfig {} 里面,现在要删除掉。
2.flavorDimensions 字段得加上 至于后面字符串写什么,大家随意。
3.prodectFlavors {} 里面定义你的包名,版本号,以及一些第三方的特殊信息(如果极光、微信、友盟等配置)
Manifest.xml 配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android/apk/res/android"
package="com.tommy.tommy">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 比如说要用到极光 这些第三方插件 多渠道的配置如下 -->
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL}"/>
<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" /> <!-- </>值来自开发者平台取得的AppKey-->
</application>
</manifest>
注意事项:
1.package="com.tommy.tommy" 已经不用管了,因为gradle里面配置了多渠道的包名,如果你要在manifest用包名要这么用${applicationId}
<!-- Required SDK核心功能-->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="${applicationId}" />
</intent-filter>
</activity>
2.同样的 下面的极光配置也都是动态的 因为不同包名,极光的对应的KEY都不一样滴,极光的KEY我放在values/string 里面,不同的包可以定义自己是string字符串,当然也有默认的请看如下截图:
说明一下:main这个文件夹下面的所有资源为整个项目的默认资源,编译的时候查找资源的顺序一定要弄清楚,拿 com.tommy.app1 来举例:先到 src下的app1去查找相应的资源,如果app1里面有资源就读取app1下面的资源,如果没有就去读取main下面res资源下的资源,就比如说 ic_launcher.png ,如果app1里面没有放这个资源,编译的时候找不到就会去main下面找就如最下面这张图 小机器人logo
打包出来 是两个app包里的logo以及 包里面定义的string里 app_name,注意观察APP名称不一样,因为他们对应资源包下values/strings 里的 app mane 不一样,具体可以自行测试,或者看我在文章结尾附上的源码下载地址。
打包的好处
1.开发的时候可以自由的选择任一包,进行运行
需要注意的是:渠道越多,gradle刷新就越多,电脑就越顶不住,这个时候,如果你电脑卡了,你可以把 productFlavors 下面的包都注释掉,只留一个你要开发的就行,这样会好很多。但,打包的时候就得全部开出来了,所以为了打包的时候不卡,推荐购买一台高配置滴服务器,安装jenckins,用全自动化的方式进行打包,把压力都丢给服务器,自己开发的电脑不卡就OK。
2.打包可以一口气打包,或者选择几个打包,不用传统的一个一个打了
完整代码下载地址
版权声明:本文标题:Android 多渠道 + 多包名 打包配置 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1725844791a1045282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论