2016-09-21 3 views
0

gradle.build 파일에 적용 할 수있는 맞춤 플러그인을 만들려고합니다. 이 작업을 수행하는 데는 몇 가지 방법이 있지만 buildSrc 폴더를 사용하여 수행하고 있습니다. 이것은 플러그인이 내 빌드에 묶여 있고 이식 가능하지 않다는 것을 의미합니다. 는 docs에서 buildSrc 폴더에는 다음과 같은 속성이 있어야합니다 당신은 rootProjectDir/buildSrc/SRC/메인/그루비 디렉토리에서 작업 클래스의 소스를 넣을 수 있습니다 android studio에서 Custom Gradle Plugin 클래스를로드 할 수 없습니다.

buildSrc 프로젝트. Gradle은 작업 클래스를 컴파일하고 테스트하고 빌드 스크립트의 클래스 경로에서 사용할 수 있도록합니다. 태스크 클래스는 빌드에서 사용되는 모든 빌드 스크립트에서 볼 수 있습니다. 그러나 빌드 외부에서 볼 수는 없으므로 정의 된 빌드 외부에서 태스크 클래스를 재사용 할 수 없습니다. buildSrc 프로젝트 접근법을 사용하면 태스크 선언 - 즉 태스크가 수행해야하는 -을 태스크 구현 - 즉, 작업이 그것을 어떻게합니다.

하지만 어쨌든 내 문제는 다음과 같습니다 :

이미 내가 루트 프로젝트 경로에 buildSrc 디렉토리를 생성하고, IDE에 의해 인식되는 멋있는 폴더를 만들었습니다. 이제 여기

enter image description here

작업 및 플러그인 자체 : 아래

내 배 구조와 내가 점점 오전 오류입니다

RenameAppVersionNameTask.groovy :

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을 동기화 수신하고 오류 :

enter image description here

내가 안드로이드 스튜디오가 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. 

도 내가 심지어 플러그인을 적용하지 못할났습니다. gradle-wrapper.properties 필요하다면 은 아래와 같다 :

distributionBase = GRADLE_USER_HOME distributionPath = 래퍼/dists distributionUrl = HTTPS dists zipStoreBase = GRADLE_USER_HOME zipStorePath = 래퍼/ : //services.gradle.org/distributions을 /gradle-2.10-all.zip

답변

2

buildSrc 디렉토리 내에있는 build.gradle 파일이 누락 된 것 같습니다. 그러면 buildSrc 프로젝트에 사용할 플러그인과 가져올 종속성을 알릴 수 있습니다.그것은이

apply plugin: 'groovy' 

repositories { 
    jcenter() 
} 

dependencies { 
    compile localGroovy() 
    compile gradleApi() 
} 

유사 보일 것이며, 나 또한 사용자 정의 플러그인을 만들 필요가 확실하지 오전 buildSrc

의 루트에 살고있는 것이며, 대신 작업을 사용할 수

프로젝트의 gradle 파일에

+1

데모를 팔로우하고 있습니다. 내가 왜 이렇게 만들었는지. 분명히 그것은 단지 하나의 작업으로 수행 될 수 있습니다. 내가 build.gradle 파일을 필요 realizse 않았다. 그게 내 문제를 해결 했어. 감사. – j2emanue

+0

제 경우에는 "jcenter()"저장소를 잃어 버렸고 "mavenCentral"만 가지고 jcenter를 다시 추가했습니다. (물론 buildSrc 디렉토리 안에 build.gradle에 있습니다) – xiaoyee

관련 문제