2016-09-21 9 views
0

私はgradle.buildファイルに適用できるカスタムプラグインを作成しようとしています。これを行うにはいくつかの方法がありますが、私はbuildSrcフォルダを使用してそれをやっています。これは、プラグインが私のビルドに縛られ、移植可能ではないことを意味します。あなたはrootProjectDir/BUILDSRC/srcに/メイン/グルーヴィーディレクトリ内のタスククラスのソースを置くことができる アンドロイドスタジオのCustom Gradle Pluginのクラスを読み込めません

BUILDSRCプロジェクト: docsからBUILDSRCフォルダには、次の特性を有するべきです。 Gradleはタスククラスのコンパイルとテストを行い、ビルドスクリプトのクラスパス上で利用できるようにします。タスククラスは、ビルドで使用されるすべてのビルドスクリプトで表示されます。しかし、それはビルド外では見えないので、定義されているビルド外でタスククラスを再利用することはできません。buildSrcプロジェクトアプローチを使用すると、タスクの宣言 - つまりタスクが何をすべきか - つまり、どのようにタスクがそれをしますか。

しかし、いずれにせよ、私の問題は以下の通りです:

すでに私は、ルートプロジェクトのパスにBUILDSRCディレクトリを作成し、IDEによって認識されているグルーヴィーなフォルダを作成しました。以下

私の折り畳み構造であると私は取得していますエラー:

RenameAppVersionNameTask.groovy:

enter image description here

を今ここには、タスクやプラグイン自体である

package com.myplugins 
import org.gradle.api.DefaultTask 
import org.gradle.api.tasks.TaskAction 

class RenameAppVersionNameTask extends DefaultTask { 

    @TaskAction 
    def run() { 
     project.configure(project) { 
      // Check if plugin works on an Android module 
      if (it.hasProperty("android")) { 
       // Iterate over app build variants (build types + flavors) 
       project.android.applicationVariants.all { variant -> 
        // Only change debug build type variants 
        if (variant.buildType.name == project.android.buildTypes.debug.name) { 
         // Rename versionName 
         def customVersionName = variant.mergedFlavor.versionName 
         variant.mergedFlavor.versionName = customVersionName + " custom" 
        } 
       } 
      } 
     } 
    } 

    RenameAppVersionNameTask() { 
     group = 'customPlugin' 
     description = 'Renames versionName of the app depends on the current git branch name' 

    } 
} 

CustomPlugin.groovy:

package com.myplugins 
import org.gradle.api.Plugin 
import org.gradle.api.Project 

class CustomPlugin implements Plugin<Project> { 

    @Override 
    void apply(Project project) { 
     project.task('renameAppVersionName', type: RenameAppVersionNameTask) 
     project.tasks.getByName('preBuild').dependsOn('renameAppVersionName') 
    } 

} 

私はGradleの同期で受け付けておりますエラーは次のとおりです。

Failed to sync Gradle project 'My GradlePluginApplication' 
Error:Unable to load class 'CustomPlugin'. 
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) 
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. 
<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes. 

も私はしても、プラグインを適用カント気づい:

enter image description here

私はアンドロイドのスタジオはGradleのラッパーを使用して確認しました。 また、もし必要gradle-wrapper.properties以下である:

distributionBase = GRADLE_USER_HOME distributionPath =ラッパー/ distsディレクトリ zipStoreBase = GRADLE_USER_HOME zipStorePath =ラッパー/ distsディレクトリ distributionUrl = HTTPS://services.gradle.org/distributions /gradle-2.10-all.zip

答えて

2

ディレクトリ内に存在するbuild.gradleファイルが見つからないようです。これは、buildSrcプロジェクトにどのプラグインを使用するか、どの依存関係を引き込むかを指示します。それは、この

apply plugin: 'groovy' 

repositories { 
    jcenter() 
} 

dependencies { 
    compile localGroovy() 
    compile gradleApi() 
} 

に似た何かを見てしまうと、私はまた、カスタムプラグインを作成する必要がわからないbuildSrc

の根に住んでいるだろうし、代わりにあなたのタスクを使用することができ

あなたのプロジェクトのgradleファイルで。

+1

私はデモに従っています。それで私はそれをこのように作りました。明らかに、それはただ一つのタスクで行うことができます。私はbuild.gradleファイルが必要であるとは考えていませんでした。それは私の問題を解決しました。ありがとう。私の場合、 – j2emanue

+0

私はjcenter()を失った "jcenter()"リポジトリを失い、 "mavenCentral"しか持っていなかったので、jcenterを追加し直してもらえました(もちろんこれはbuildSrcディレクトリ内のbuild.gradleです) – xiaoyee

関連する問題