我正在尝试使用Java(而不是Groovy)开发自定义Gradle插件。
我使用这个Groovy插件作为指南
@Override
void apply(Project project) {
def hasApp = project.plugins.withType(AppPlugin)
def hasLib = project.plugins.withType(LibraryPlugin)
if (!hasApp && !hasLib) {
throw new IllegalStateException("'android' or 'android-library' plugin required.")
}
final def log = project.logger
final def variants
if (hasApp) {
variants = project.android.applicationVariants
} else {
variants = project.android.libraryVariants
}
project.dependencies {
debugCompile 'com.jakewharton.hugo:hugo-runtime:1.2.2-SNAPSHOT'
// TODO this should come transitively
debugCompile 'org.aspectj:aspectjrt:1.8.6'
compile 'com.jakewharton.hugo:hugo-annotations:1.2.2-SNAPSHOT'
}
project.extensions.create('hugo', HugoExtension)
variants.all { variant ->
if (!variant.buildType.isDebuggable()) {
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return;
} else if (!project.hugo.enabled) {
log.debug("Hugo is not disabled.")
return;
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = [
"-showWeaveInfo",
"-1.5",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}
}
}
我已经开发了这个用于检查Android App / Android库
final PluginCollection hasAndroidAppPlugin = project.getPlugins().withType(AppPlugin.class);
final PluginCollection hasAndroidLibraryPlugin = project.getPlugins().withType(LibraryPlugin.class);
if (hasAndroidAppPlugin.isEmpty() & hasAndroidLibraryPlugin.isEmpty()) {
throw new IllegalStateException("'android' or 'android-library' plugin required.");
}
这用于设置我的依赖项
final DependencyHandler dependencyHandler = project.getDependencies();
dependencyHandler.add("debugCompile", "com.example.perspective:perspective-runtime:0.0.1");
dependencyHandler.add("debugCompile", "org.aspectj:aspectjrt:1.8.10.3");
如何使用Java实现以下目标?
我很难找到" Variants"例如applicationVariants / libraryVariants。
if (hasApp) {
variants = project.android.applicationVariants
} else {
variants = project.android.libraryVariants
}
答案 0 :(得分:1)
免责声明:我熟悉Gradle,而不是Android
我猜测android
属性是一个扩展对象。请参阅plugin docs和ExtensionContainer
如果你将这个println放入android build
println "${project.android.class.name}"
您将看到扩展对象的类型。然后就可以了
AndroidModel aModel = getProject().getExtensions().getByType(AndroidModel.class)
我在这里使用AndroidModel
,你将用上面的println中打印的内容替换它
您也可以
Object aModel = getProject().getExtensions().getByName("android");
System.out.println(aModel.getClass().getName());