Gradle copyAAR任务依赖assembleDebug后不执行问题求助
copyAAR Task Hey there, let's break down why your copyAAR task isn't executing and printing that log message you're waiting for. I've seen this exact issue a few times, so let's start with the most common pitfalls:
1. You've Got the Task Dependency Logic Backwards
This is probably the core issue. When you write task copyAAR(dependsOn:'assembleDebug'), you're telling Gradle: "To run copyAAR, first run assembleDebug". That does not mean "run copyAAR after assembleDebug finishes".
If your goal is to automatically trigger copyAAR whenever assembleDebug completes, you need to use finalizedBy instead of dependsOn:
task copyAAR { doLast { println "++++++ AAR COPIED ++++++: " } } // This tells Gradle: "After assembleDebug finishes, run copyAAR" assembleDebug.finalizedBy copyAAR
2. Outdated Gradle Syntax is Causing Issues
The << syntax you used (<< { println ... }) is deprecated in Gradle 5.0 and later. It's replaced with doLast to define the action that runs when the task executes. Even if your Gradle version is older, switching to doLast makes the task definition clearer and avoids silent failures.
3. You're Not Triggering the Task Correctly
If you stick with your original dependsOn setup (where copyAAR relies on assembleDebug), you can't just run ./gradlew :MY_Library:assembleDebug and expect copyAAR to run. You need to explicitly trigger the copyAAR task:
./gradlew :MY_Library:copyAAR
This will first run assembleDebug, then execute your copyAAR action.
4. Ensure Tasks Are Properly Initialized
Since Android Gradle Plugin creates tasks like assembleDebug dynamically during configuration, wrapping your task setup in afterEvaluate can prevent cases where Gradle tries to reference a task that doesn't exist yet. Here's a safe version:
afterEvaluate { task copyAAR { doLast { println "++++++ AAR COPIED ++++++: " } } assembleDebug.finalizedBy copyAAR }
Quick Verification Steps
- Run
./gradlew :MY_Library:tasksand check ifcopyAARappears in the task list. If it doesn't, your task isn't being defined correctly. - If you're using the
finalizedBysetup, run./gradlew :MY_Library:assembleDebug --infoto see detailed logs. You should see a line indicatingcopyAARis being triggered afterassembleDebug.
内容的提问来源于stack exchange,提问作者Jeff Young

