如何让 Cordova 3.4.0 在构建时设置 Android 版本名称/代码?
题
我目前正在使用 Cordova 3.4.0 和 CLI 来构建我的项目以使用以下命令来定位 android:
cordova build android
或者
cordova build android --release
我的 config.xml 指定了以下内容:
<widget id="com.example.myapp"
version="0.0.3"
versionCode="2"
...
>
</widget>
生成的 AndroidManifest.xml 中 myapp/平台/android 不会使用 config.xml 中指定的版本和版本代码进行更新。它保持默认:
<manifest
android:versionCode="1"
android:versionName="1.0"
...
</manifest>
这是一个错误吗?或者我应该通过其他方式在 config.xml 中指定 Android 的 versionCode 和 versionName 吗?甚至也许,是否有一个钩子可以在构建之前使用正确的版本更新 AndroidManifest.xml ?
我宁愿不必每次我希望它更改时手动更新它:
- 它容易出错且繁琐
- 对于不熟悉的开发人员来说,改变并不是一件显而易见的事情
- 没有任何平台文件夹存储在源代码管理中
由于这些原因,我希望在 config.xml 中设置版本号。有任何想法吗?
谢谢
解决方案
最后与 Cordova 3.5.0-0.2.4
我将此属性添加到 <widget>
节点 config.xml
android-versionCode="10"
和 AndroidManifest.xml
已正确更新为
android:versionCode="10"
其他提示
追随 michaeloryl 我使用cordova 3.4和使用bash工具 xmlstarlet
基本上它将VersionCode属性的值从AndroidManifest.xml中的config.xml复制了
而不是运行这个:
cordova build android --release
.
运行[应该成为脚本]:
cordova prepare android
# grab version code from config.xml
VERSION_CODE=$(xmlstarlet sel -t -v "//@versionCode" "config.xml")
# write correct version code in manifest
xmlstarlet ed -L -u "/manifest/@android:versionCode" -v "$VERSION_CODE" "platform/android/AndroidManifest.xml"
cordova compile android --release
.
通过这种方式,当我升级到Cordova 3.5时,我可以重用通常的'Cordova Build Android --Release' 在此处不应该是问题,因为它将从config.xml复制到平台/ android中的androidmanifest.xml文件(至少在Cordova 3.4.0上)。那部分我没有问题。
虽然,获取版本代码递增,但这是一项任务。实际上,一系列任务,我加入了咕噜声的鲁弗菲莱斯。这是 string-replace 任务。 “ grunt string-replace:版本码”将递增存储在package.json中的版本码。 “ grunt string-replace:androidversioncode ”将拍摄该值并将其放入平台/ android / androidmanifest.xml文件:
// STRING-REPLACE
'string-replace': {
versionCode: { // update the version code stored in package.json
options: {
replacements: [
{
pattern: /['"]androidVersionCode['"]:.*?['"](\d*)['"]/ig,
replacement: function (match, p1, offset, string) {
return '"androidVersionCode": "' + (parseInt(p1) + 1) + '"';
}
}
]
},
files: {
'package.json': 'package.json'
}
},
androidVersionCode: { // update the version code stored in AndroidManifest.xml
options: {
replacements: [
{
pattern: /android:versionCode=['"](\d*)['"]/ig,
replacement: function (match, p1, offset, string) {
var pkg = grunt.file.readJSON('package.json');
grunt.log.writeln("pkg.androidVersionCode: " + pkg.androidVersionCode);
grunt.log.writeln('Returning: ' + 'android:versionCode="' + pkg.androidVersionCode + '"');
return 'android:versionCode="' + pkg.androidVersionCode + '"';
}
}
]
},
files: {
'platforms/android/AndroidManifest.xml': 'platforms/android/AndroidManifest.xml'
}
}
}
.
它需要从GruntFile.js文件中调用此在使用前调用,当然(以及NPM安装grunt-string-replace):
grunt.loadNpmTasks('grunt-string-replace');
.
您需要添加一个行,例如以下内容为此全部工作:
"androidVersionCode": "13", // or whatever value is current for you
.
它将通过上面的 string-replace:fluepscode 任务递增。我将行Package.json放在以“版本”开头的行之后:
一个重要的技巧让这个工作是为了确保,不要调用“cordova build android”,你调用“cordova准备android”,然后“grunt replove-string:androidversioncode”,和然后“Cordova编译Android”。 Build只是调用“准备”,然后“编译”,并且在这两个任务之间是必须修改AndroidManifest.xml文件的速度,以防止它被覆盖,似乎。
我的构建过程更复杂,因为我实际上是递增package.json中的版本值,其中 grunt-bump grunt,然后使用 xmlpoke 用于使用 string-replace 的Grunt和我的页面。我使用 grunt-shell 实际上致电所有Cordova构建步骤以将所有内容复制到它的位置并按正确的顺序运行。
如果有这种帮助,请告诉我。
我使用Cordova-Custom-Config插件修复了此问题,然后在
中的config.xml中<platform name="android">
.
放入
<preference name="android-manifest/@android:versionCode" value="108" />
.
将值更改为想要的版本代码...
它适用于我。
编辑AndroidManifest.xml中的版本代码(/platforms/android/androidmanifest.xml)
<manifest android:hardwareAccelerated="true" android:versionCode="2"android:versionName="0.0.2" package="com.ionicframework.zinescreen841178" xmlns:android="http://schemas.android.com/apk/res/android">
.
更改此处的版本代码不在config.xml中,它不会更新。然后当您拍摄发布构建时,它将为您生成新的版本代码。
<widget id="com.example.yourapp"
android-versionCode="1112"
version="1.1.3"
xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
.
然后运行它以上'它(例如,在您的释放中使用 - 脚本):
VersionCode=`cat config.xml | grep android-versionCode | sed -e 's/.*android-versionCode=\"\(.*\)\"/\1/'`
VersionCode=$((VersionCode+1))
sed -i "s/.*android-versionCode=\"\(.*\)\"/ android-versionCode=\"$VersionCode\"/" config.xml
.
呀,脏但有效:)
您无需更改androidmanifest.xml,因为它是Cordova本身生成的,因此Cordova的所有清单更改都会恢复
和它的所有负责是build.gradle位于平台文件夹中
文件 - > platform / build.gradle
搜索
如果(minsdkversion>= 20){defaultconfig.versionCode += 9} else if(minsdkversion= 14){defaultconfig.versioncode += 8}
随着您的版本代码从这里更新,您可以更改它或硬代码