使用Android插件获取Gradle项目的类路径 [英] Get classpath for gradle project using Android plugin

查看:1434
本文介绍了使用Android插件获取Gradle项目的类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为Gradle多项目构建构建一些任务,并且需要获取项目的类路径。构建脚本包含使用Java插件和使用Android插件的项目的项目。



对于Java项目,我能够使用顶级投票答案在这个问题获得类路径使用 configurations.runtime.asPath ;然而,这不适用于Android项目,因为没有configurations.runtime属性。



如何使用Android插件为Gradle项目生成类路径? p>

解决方案

这是一个生成模块jar的gradle任务,并且还包含所有变体的测试类路径。
它包含库和选定运行库中的android.jar。

我添加了两个命令行执行,用于更新emacs中的一些env var并杀死任何运行beanshell(与以前的类路径)。

 任务类路径(类型:Jar){
from android.sourceSets.main .java.srcDirs,
android.sourceSets.test.java.srcDirs
outputs.upToDateWhen {false}
doLast {
println构建类路径...
def cp2 = [android.getBootClasspath()[0],it.archivePath]
android.applicationVariants.all {v - >
cp2 + = v.getApkLibraries()
}
def classpath = cp2.unique()。join(:)
println正在更新emacs ...
exec {
executablesh
args-c,emacsclient --eval(setenv \CLASSPATH \\+ classpath +\)'
}
exec {
可执行文件sh
args-c,emacsclient --eval'(jdee-bsh-exit)'
}


请注意,我正在使用:加入classpath


I've been building some tasks for a gradle multi-project build and have a need to get the class path for a project. The build script has projects that use the Java plugin and projects that use the Android plugin.

For the Java projects I was able to use the top voted answer in this question to get the class path using configurations.runtime.asPath; however, this is not working for the Android projects because there is no configurations.runtime property.

How can generate a classpath for a gradle project using the Android plugin?

解决方案

Here is a gradle task that generates the module jar and includes also the test classpath for all variants. It is including libraries and the android.jar from selected runtime.

I've added two commandline executions for updating some env var inside emacs and for killing any running beanshell (with previous classpath).

task classpath(type: Jar) {
  from android.sourceSets.main.java.srcDirs,
       android.sourceSets.test.java.srcDirs
  outputs.upToDateWhen { false }
  doLast {
    println "Building classpath..."
    def cp2 = [android.getBootClasspath()[0], it.archivePath]
    android.applicationVariants.all { v ->
      cp2 += v.getApkLibraries()
    }
    def classpath = cp2.unique().join(":")
    println "Updating emacs..."
    exec {
      executable "sh"
      args "-c", "emacsclient --eval '(setenv \"CLASSPATH\" \""+classpath+"\")'"
    }
    exec {
      executable "sh"
      args "-c", "emacsclient --eval '(jdee-bsh-exit)'"
    }
  }
}

Be aware that I'm using ":" for joining the classpath

这篇关于使用Android插件获取Gradle项目的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆