android gradle is插件,android gradle 插件创建 configuration

写了一个gradle 插件,现在的需求是需要通过

productFlavors

的类型来创建不同的

configuration

目前代码:

private Project project

private Configuration embedConf

private Set<Configuration> embedConf2

private Set<ResolvedArtifact> artifacts

@Override

void apply(Project project) {

this.project = project

checkAndroidPlugin()

createConfiguration()

project.afterEvaluate {

resolveArtifacts()

project.android.libraryVariants.all { variant ->

processVariant(variant)

}

}

}

//检查插件

private void checkAndroidPlugin() {

if (!project.plugins.hasPlugin('com.android.library')) {

throw new ProjectConfigurationException('fat-aar-plugin must be applied in project that' +

' has android library plugin!', null)

}

}

//创建标签插件

private void createConfiguration() {

def listener = new DependencyResolutionListener() {

@Override

void beforeResolve(ResolvableDependencies resolvableDependencies) {

embedConf.dependencies.each { dependency ->

project.dependencies.add('provided', dependency)

//println 'fat-aar-->[dependency] ' + dependency.name

}

embedConf2.each { configuration ->

configuration.dependencies.each { dependency ->

project.dependencies.add('provided', dependency)

//println 'fat-aar-->[dependency] ' + dependency.name

}

}

project.gradle.removeListener(this)

}

@Override

void afterResolve(ResolvableDependencies resolvableDependencies) {}

}

embedConf2 = new  HashSet<Configuration>()

project.android.libraryVariants.all { BaseVariant variant ->

variant.productFlavors.each { flavors ->

//                println 'fat-aar-->[flavors] ' + project.name

//                println 'fat-aar-->[flavors] ' + flavors.name.capitalize()

def cname = flavors.getName() + 'Embed'

if (project.configurations.findByName(cname) == null) {

def configg = project.configurations.create(cname.toString())

configg.visible = false

embedConf2.add(configg)

println 'fat-aar-->[configuration] ' + project.configurations.getByName(cname)

}

}

}

embedConf = project.configurations.create('embed')

println 'fat-aar-->[configuration] ' + project.configurations.getByName('embed')

embedConf.visible = false

project.gradle.addListener(listener)

}

现在遇到问题是 创建的 embed configuration 是可以正常使用,但是通过遍历productFlavors 的报错,看错误日志像是构建的顺序问题

错误日志:

➜ ./gradlew module-oldminirpc:assembleLazadaDebug

Parallel execution is an incubating feature.

fat-aar-->[configuration] configuration ':module-apdid:embed'

Incremental java compilation is an incubating feature.

fat-aar-->[configuration] configuration ':module-apdid:lazadaEmbed'

fat-aar-->[configuration] configuration ':module-apdid:aeEmbed'

fat-aar-->[embed detected][jar] com.alipay.stunsdk:stunclientsdk:1.0.2

fat-aar-->[configuration] configuration ':module-face:embed'

fat-aar-->[configuration] configuration ':module-logger:embed'

fat-aar-->[configuration] configuration ':module-oldminirpc:embed'

FAILURE: Build failed with an exception.

* Where:

Build file '/Users/gnaixx/Documents/AntWork/Code/android-alipaysecuritysdk/module-oldminirpc/build.gradle' line: 41

* What went wrong:

A problem occurred evaluating project ':module-oldminirpc'.

> Could not find method aeEmbed() for arguments [com.alipay.apsecuritysdk.mrpc:mrpc-core:1.0.2:core] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1.033 secs

950-390_%E7%94%BB%E6%9D%BF-1.jpg

### 解决方案 对于初次启动Android Studio时遇到的Gradle配置缓慢问题,有几种方法可以显著提升效率: #### 优化网络连接设置 如果位于中国或其他网络受限地区,建议修改`gradle.properties`文件中的镜像源地址来加快下载速度。具体操作是在项目的根目录下找到或新建`gradle.properties`文件,并加入如下内容[^1]: ```properties systemProp.http.proxyHost=proxy.example.com systemProp.http.proxyPort=8080 systemProp.https.proxyHost=proxy.example.com systemProp.https.proxyPort=8080 org.gradle.daemon=true org.gradle.parallel=true org.gradle.configureondemand=true ``` #### 修改Gradle版本和仓库位置 确保使用的Gradle版本是最新的稳定版之一,在项目级`build.gradle`文件内指定合适的Gradle插件版本以及调整Maven中央库为中国大陆可用的阿里云等国内镜像站点[^2]: ```groovy // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false } pluginManagement { repositories { maven { url 'https://maven.aliyun.com/repository/public/' } google() jcenter() // Warning: this repository is deprecated and will be removed soon } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { maven { url 'https://maven.aliyun.com/repository/google/' } mavenCentral() } } ``` #### 启用离线模式 当开发环境已经具备所需依赖项之后,可以在Settings/Preferences对话框里勾选Offline work选项以减少不必要的在线查询时间消耗。 通过上述措施能够有效改善首次加载大型工程项目时可能出现的速度瓶颈现象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值