上传库至jCenter执行命令报错:无法读取指定Gradle脚本
解决jCenter上传时无法读取远程Gradle脚本的问题
这个问题我之前帮不少开发者解决过——你引用的远程Gradle脚本https://raw.githubusercontent.com/ntpanchal/JCenter/master/installv1.gradle已经被原作者移除或移动了位置,导致构建时拉取不到资源,才会触发这个错误。下面给你几个靠谱的解决方案:
方案一:替换为本地Gradle脚本(最稳妥)
远程脚本依赖容易受原作者操作影响,不如把脚本内容本地化:
- 在你的项目根目录新建一个
scripts文件夹,里面创建installv1.gradle文件。 - 把下面的通用上传配置内容复制进去(记得替换占位信息):
apply plugin: 'maven' apply plugin: 'signing' // 替换成你的库仓库信息 def siteUrl = 'https://github.com/yourusername/your-library' def gitUrl = 'https://github.com/yourusername/your-library.git' install { repositories.mavenInstaller { pom { project { packaging 'aar' name '你的库名称' url siteUrl // 许可证配置 licenses { license { name 'The Apache Software License, Version 2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } // 开发者信息 developers { developer { id '你的开发者ID' name '你的姓名' email '你的邮箱地址' } } // 版本控制信息 scm { connection gitUrl developerConnection gitUrl url siteUrl } } } } } // 生成源码包 task sourcesJar(type: Jar) { from android.sourceSets.main.java.srcDirs classifier 'sources' } // 生成Javadoc包 task javadoc(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task javadocJar(type: Jar, dependsOn: javadoc) { classifier 'javadoc' from javadoc.destinationDir } artifacts { archives javadocJar archives sourcesJar } // 签名配置(如果需要) signing { sign configurations.archives }
- 找到你项目里引用远程脚本的代码(一般在库模块的
build.gradle里),把apply from: 'https://raw.githubusercontent.com/ntpanchal/JCenter/master/installv1.gradle'改成apply from: file('../scripts/installv1.gradle')(路径根据你的实际结构调整)。
方案二:改用官方推荐的Bintray上传插件
jCenter的官方上传方式是用Bintray的Gradle插件,稳定性更高:
- 在项目根目录的
build.gradle里添加插件依赖:
buildscript { dependencies { classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' } }
- 在库模块的
build.gradle里配置插件和上传信息:
apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' // 设置你的库Maven坐标 group = 'com.yourcompany.yourlibrary' version = '1.0.0' // Maven POM配置 install { repositories.mavenInstaller { pom { project { name '你的库名称' description '你的库功能描述' url 'https://github.com/yourusername/your-library' licenses { license { name 'Apache-2.0' url 'http://www.apache.org/licenses/LICENSE-2.0.txt' } } developers { developer { id '你的ID' name '你的姓名' email '你的邮箱' } } scm { connection 'https://github.com/yourusername/your-library.git' developerConnection 'https://github.com/yourusername/your-library.git' url 'https://github.com/yourusername/your-library' } } } } } // Bintray账号和仓库配置 bintray { // 推荐用环境变量存储敏感信息,避免硬编码 user = System.getenv('BINTRAY_USER') key = System.getenv('BINTRAY_KEY') configurations = ['archives'] pkg { repo = 'maven' // 你的Bintray仓库名称 name = 'YourLibrary' // 库在Bintray的名称 desc = '你的库描述' websiteUrl = 'https://github.com/yourusername/your-library' vcsUrl = 'https://github.com/yourusername/your-library.git' licenses = ['Apache-2.0'] publish = true // 自动发布到jCenter } }
- 确保你已经在Bintray创建了账号和Maven仓库,并且获取到了API密钥(在Bintray个人设置的
API Key页面查看)。
完成配置后,重新执行./gradlew clean build install bintrayUpload就能正常上传了。
内容的提问来源于stack exchange,提问作者Nilesh Panchal
相关产品推荐
相关产品推荐

