Skip to content

Commit

Permalink
build: 添加AarToJarPlugin
Browse files Browse the repository at this point in the history
消除大量重复定义的jarPackage构建脚本。
  • Loading branch information
shifujun committed Oct 28, 2021
1 parent 87ba969 commit bf33970
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 232 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:$build_gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.tencent.shadow.coding:aar-to-jar-plugin'
}
}
apply from: 'buildScripts/gradle/common.gradle'
Expand Down
25 changes: 1 addition & 24 deletions projects/sample/source/sample-host-lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

android {
compileSdkVersion project.COMPILE_SDK_VERSION
Expand All @@ -23,27 +24,3 @@ android {
}

}

//下面的jarPackage和afterEvaluate负责让这个aar再生成一个输出jar包的任务
def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}
1 change: 1 addition & 0 deletions projects/sdk/coding/aar-to-jar-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
22 changes: 22 additions & 0 deletions projects/sdk/coding/aar-to-jar-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apply plugin: 'java-gradle-plugin'

apply plugin: 'kotlin'

group 'com.tencent.shadow.coding'

gradlePlugin {
plugins {
shadow {
id = "com.tencent.shadow.internal.aar-to-jar"
implementationClass = "com.tencent.shadow.coding.aar_to_jar_plugin.AarToJarPlugin"
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.tools.build:gradle:$build_gradle_version"
testImplementation 'junit:junit:4.12'
testImplementation gradleTestKit()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Tencent is pleased to support the open source community by making Tencent Shadow available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.tencent.shadow.coding.aar_to_jar_plugin

import com.android.build.gradle.BaseExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.Copy
import java.util.*

class AarToJarPlugin : Plugin<Project> {

override fun apply(project: Project) {
project.afterEvaluate {
val android = it.extensions.getByName("android") as BaseExtension
android.buildTypes.forEach { buildType ->
createJarPackageTask(project, buildType.name)
}
}
}

private fun createJarPackageTask(project: Project, buildType: String): Task {
val taskName = "jar${buildType.capitalize(Locale.getDefault())}Package"
return project.tasks.create(taskName, Copy::class.java) {
fun buildDirFile(relativePath: String) =
project.file(project.buildDir.path + relativePath)

val aarFileName = "${project.name}-${buildType}"
val aarFile = buildDirFile("/outputs/aar/${aarFileName}.aar")
val outputDir = buildDirFile("/outputs/jar")

it.from(project.zipTree(aarFile))
it.into(outputDir)
it.include("classes.jar")
it.rename("classes.jar", "${aarFileName}.jar")
it.group = "build"
it.description = "生成jar包"
}.dependsOn(
project.getTasksByName(
"assemble${buildType.capitalize(Locale.getDefault())}",
false
).first()
)
}
}
3 changes: 2 additions & 1 deletion projects/sdk/coding/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include 'checks', 'lint'
include 'code-generator'
include 'code-generator'
include 'aar-to-jar-plugin'
24 changes: 1 addition & 23 deletions projects/sdk/core/activity-container/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.core'

Expand Down Expand Up @@ -33,27 +34,4 @@ dependencies{
implementation 'com.tencent.shadow.coding:lint'
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}

preBuild.dependsOn(project(":generate-delegate-code").getTasksByName("generateDelegateCode", false).first())
1 change: 1 addition & 0 deletions projects/sdk/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:$build_gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.tencent.shadow.coding:aar-to-jar-plugin'
}
}
apply from: '../../../buildScripts/gradle/common.gradle'
Expand Down
24 changes: 1 addition & 23 deletions projects/sdk/core/common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.core'

Expand Down Expand Up @@ -27,26 +28,3 @@ android {
dependencies{
implementation 'com.tencent.shadow.coding:lint'
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}
24 changes: 1 addition & 23 deletions projects/sdk/core/loader/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

apply plugin: 'kotlin-android'

Expand Down Expand Up @@ -64,27 +65,4 @@ dependencies {
implementation 'com.tencent.shadow.coding:lint'
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}

preBuild.dependsOn(project(":generate-delegate-code").getTasksByName("generateDelegateCode", false).first())
24 changes: 1 addition & 23 deletions projects/sdk/core/runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.core'

Expand Down Expand Up @@ -43,27 +44,4 @@ dependencies{
compileOnly project(':activity-container')
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}

preBuild.dependsOn(project(":generate-delegate-code").getTasksByName("generateDelegateCode", false).first())
1 change: 1 addition & 0 deletions projects/sdk/dynamic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:$build_gradle_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.tencent.shadow.coding:aar-to-jar-plugin'
}
}
apply from: '../../../buildScripts/gradle/common.gradle'
Expand Down
24 changes: 1 addition & 23 deletions projects/sdk/dynamic/dynamic-apk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.dynamic'

Expand Down Expand Up @@ -36,26 +37,3 @@ dependencies {
implementation 'com.tencent.shadow.core:utils'
compileOnly 'com.tencent.shadow.core:common'
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}
24 changes: 1 addition & 23 deletions projects/sdk/dynamic/dynamic-host-multi-loader-ext/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.dynamic'

Expand Down Expand Up @@ -39,26 +40,3 @@ dependencies {
compileOnly 'com.tencent.shadow.core:common'
api project(':dynamic-host')
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}
24 changes: 1 addition & 23 deletions projects/sdk/dynamic/dynamic-host/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
apply plugin: 'com.tencent.shadow.internal.aar-to-jar'

group 'com.tencent.shadow.dynamic'

Expand Down Expand Up @@ -37,26 +38,3 @@ dependencies {
compileOnly 'com.tencent.shadow.core:common'
api project(':dynamic-apk')
}

def jarPackage(buildType) {
return tasks.create("jar${buildType.capitalize()}Package", Copy) {
def aarFile = file(project.buildDir.path + "/outputs/aar/${project.name}-${buildType}.aar")
def outputDir = file(project.buildDir.path + "/outputs/jar")

from zipTree(aarFile)
into outputDir
include 'classes.jar'
rename 'classes.jar', "${project.name}-${buildType}.jar"
group = 'build'
description = '生成jar包'
}.dependsOn(project.getTasksByName("assemble${buildType.capitalize()}", false).first())
}

afterEvaluate {
android {
buildTypes.findAll().each { buildType ->
def buildTypeName = buildType.getName()
jarPackage(buildTypeName)
}
}
}
Loading

0 comments on commit bf33970

Please sign in to comment.