2017-08-21 3 views
0

Java(Groovyではなく)を使用してカスタムGradleプラグインを開発しようとしています。JavaでCustom Gradle Pluginを開発する方法

私はガイドとしての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アプリケーション/ 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を使用して以下をどのように達成できますか?

「バリアント」を識別しようとしています。 applicationVariants/libraryVariants。

if (hasApp) { 
      variants = project.android.applicationVariants 
     } else { 
      variants = project.android.libraryVariants 
     } 
+1

を行うことができることを置き換えるよあなたはプロジェクトを試してみました。 android.getApplicationVariants() –

+0

私はこの "final Object obj = project.android.getApplicationVariants();"を試してみます。私のIDEは "アンドロイド"のようにシンボルを解決できないと言っています – Hector

+1

'project.getAndroid()。getApplicationVariants()' –

答えて

1

免責事項:私はGradleのに慣れてる、私はandroidプロパティは拡張オブジェクトであることを推測している

をAndroidのはありません。あなたは拡張オブジェクトが何であるかの種類がわかりますplugin docsExtensionContainer

あなたはアンドロイドのビルドにこののprintlnを置く場合

println "${project.android.class.name}" 

参照してください。そして、あなたは私がここにAndroidModelを使用してい

AndroidModel aModel = getProject().getExtensions().getByType(AndroidModel.class) 

を行うことができ、あなたは

上のprintlnで印刷されたものは何でもします。また、

Object aModel = getProject().getExtensions().getByName("android"); 
System.out.println(aModel.getClass().getName()); 
+0

printlnは" com.android.build.gradle.AppExtension; import "を使用したことから" com.android.build.gradle.AppExtension_Decorated "を返します。 AppExtensionを使ってfinalオブジェクトを呼び出すことができます。obj = appExtension.getApplicationVariants(); – Hector