我有一个通过 jenkins 构建的 NativeScript 项目。
截至7月9日,该项目建设没有出现任何问题。在同一天,提交后,构建停止工作。
为了确保错误不是由新代码引起的,我们恢复了更改,但构建仍然无法正常工作。
详细分析日志后发现,
tns
在处理firebase插件时,需要下载gradle。
当它出现时,它会下载 gradle 8.4,并且在该版本中构建不起作用。在工作构建的日志中,同一天,下载的 gradle 版本是 7.6。
似乎引用的组件之一,例如
nativescript-plugin-firebase
,似乎正在尝试使用不兼容的 gradle 版本。
这是工作构建的编译日志的摘录:
Entrypoint bundle = runtime.js vendor.js bundle.js
[../$$_lazy_route_resource lazy recursive] ../$$_lazy_route_resource lazy namespace object 160 bytes {bundle} [built]
[./app.css] 10.8 KiB {bundle} [built]
[./app/app-routing.module.ts] 1.13 KiB {bundle} [built]
[./app/app.component.ts] 13 KiB {bundle} [built]
[./app/app.module.ts] 27.1 KiB {bundle} [built]
[./app/classes/custom-translate-loader.ts] 438 bytes {bundle} [built]
[./app/classes/modal-helper/modal-dialog-parameters.ts] 967 bytes {bundle} [built]
[./app/customPipe/backer-transform.ts] 1.1 KiB {bundle} [built]
[./app/customPipe/budged-order-id.pipe.ts] 1.38 KiB {bundle} [built]
[./app/customPipe/labels-transform.ts] 1.16 KiB {bundle} [built]
[./app/customPipe/url-transform.ts] 1.45 KiB {bundle} [built]
[./app/services/auth/token.interceptor.ts] 6.42 KiB {bundle} [built]
[./app/services/localstorage-budget-order/localstorage-budget-order.service.ts] 10 KiB {bundle} [built]
[./environments/environment.ts] 1.3 KiB {bundle} [built]
[./main.ts] 2.71 KiB {bundle} [built]
+ 1367 hidden modules
Webpack compilation complete.
nativescript-plugin-firebase: /var/lib/jenkins/workspace/ript_Application_Android_develop/platforms/android/.pluginfirebaseinfo not found, forcing prepare!
nativescript-plugin-firebase: running release build or change in environment detected, forcing prepare!
Downloading https://services.gradle.org/distributions/gradle-7.6-bin.zip
这是非工作版本,于 7 月 9 日晚些时候开始发生:
Entrypoint bundle = runtime.js vendor.js bundle.js
[../$$_lazy_route_resource lazy recursive] ../$$_lazy_route_resource lazy namespace object 160 bytes {bundle} [built]
[./app.css] 10.8 KiB {bundle} [built]
[./app/app-routing.module.ts] 1.13 KiB {bundle} [built]
[./app/app.component.ts] 13 KiB {bundle} [built]
[./app/app.module.ts] 27.1 KiB {bundle} [built]
[./app/classes/custom-translate-loader.ts] 438 bytes {bundle} [built]
[./app/classes/modal-helper/modal-dialog-parameters.ts] 967 bytes {bundle} [built]
[./app/customPipe/backer-transform.ts] 1.1 KiB {bundle} [built]
[./app/customPipe/budged-order-id.pipe.ts] 1.38 KiB {bundle} [built]
[./app/customPipe/labels-transform.ts] 1.16 KiB {bundle} [built]
[./app/customPipe/url-transform.ts] 1.45 KiB {bundle} [built]
[./app/services/auth/token.interceptor.ts] 6.42 KiB {bundle} [built]
[./app/services/localstorage-budget-order/localstorage-budget-order.service.ts] 10 KiB {bundle} [built]
[./environments/environment.ts] 1.3 KiB {bundle} [built]
[./main.ts] 2.71 KiB {bundle} [built]
+ 1367 hidden modules
Webpack compilation complete.
nativescript-plugin-firebase: /var/lib/jenkins/workspace/ript_Application_Android_develop/platforms/android/.pluginfirebaseinfo not found, forcing prepare!
nativescript-plugin-firebase: running release build or change in environment detected, forcing prepare!
Downloading https://services.gradle.org/distributions/gradle-8.4-bin.zip
您可以在每个日志的最后一行看到正在下载的 gradle 版本。
如何确保我坚持使用 7.6 版本?
发生此错误时,首先构建失败,因为构建需要 java 17 而不是 11。如果我们安装该版本,则后续构建错误为:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'background_http'.
> Failed to notify project evaluation listener.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
> Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.
If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.
> Could not get unknown property 'generateReleaseBuildConfig' for root project 'background_http' of type org.gradle.api.Project.
我尝试编辑node_modules/nativescript/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties并插入最后一行:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
当我这样做时,我收到此错误:
* What went wrong:
A problem occurred evaluating root project 'background_http'.
> Failed to apply plugin 'com.android.internal.version-check'.
> Minimum supported Gradle version is 8.4. Current version is 7.6. If using the gradle wrapper, try editing the distributionUrl in platforms/tempPlugin/background_http/gradle/wrapper/gradle-wrapper.properties to gradle-8.4-all.zip
谢谢你。
我已经取得了一些进步。如果对大家有帮助的话。
创建一个
patch-gradle.js
文件,其中包含以下内容:
const fs = require('fs');
const path = require('path');
// adjust your paths for nativescript-cli installed globally
const paths = [
'GLOBAL/node_modules/nativescript/vendor/gradle-plugin/gradle/wrapper/gradle-wrapper.properties',
'GLOBAL/node_modules/nativescript/vendor/gradle-plugin/build.gradle',
'node_modules/@nativescript/android/framework/gradle/wrapper/gradle-wrapper.properties',
'node_modules/@nativescript/android/framework/build.gradle'
]
const finds = ["{{runtimeGradleVersion}}", "{{runtimeAndroidPluginVersion}}", '6.4', 'gradle:3.6.4']
// const replaces = ['6.7', '4.0.1'] // NS 7.1
const replaces = ['6.8', '4.1.2', '6.8', 'gradle:4.1.2'] // NS 7.2
// Define the path to the file
paths.forEach((value, key) => {
const filePath = value;
// Define the line to search and the replacement
const lineToFind = finds[key];
const lineToReplace = replaces[key];
// Read the file content
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
return console.error(`Error reading file: ${err}`);
}
// Replace the text in the line if it exists
const updatedData = data.split('\n').map(line => {
return line.includes(lineToFind) ? line.replace(lineToFind, lineToReplace) : line;
}).join('\n');
// Write the updated content back to the file
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
return console.error(`Error writing file: ${err}`);
}
console.log('File successfully updated!');
});
});
});
然后创建平台
tns platform add android
。
然后运行这个脚本node patch-gradle.js
。
然后构建 android tns build android
.
通过这种方式,它可以为模块/插件构建正确的
aar
文件。这是解决问题的一步,但不是完整的解决方案。