Skip to content

Commit

Permalink
Version with AGP 4.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cosic committed Aug 11, 2021
1 parent c6934f2 commit ec2321c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 66 deletions.
7 changes: 4 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libs.appcompat = "androidx.appcompat:appcompat:1.2.0"
libs.kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$versions.kotlin"
libs.kotlinReflect = "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin"
libs.gson = "com.google.code.gson:gson:2.8.2"
libs.gradleTools = "com.android.tools.build:gradle:4.1.1"
libs.gradleTools = "com.android.tools.build:gradle:4.2.1"

def junit = [:]
versions.junitJupiter = "5.7.0"
Expand All @@ -32,7 +32,8 @@ ext.junit = junit
ext.libs = libs

def gradlePlugins = [:]
gradlePlugins.gradle = "com.android.tools.build:gradle:4.1.3"
gradlePlugins.gradle = "com.android.tools.build:gradle:4.2.1"
gradlePlugins.kotlinDsl = "org.gradle.kotlin:gradle-kotlin-dsl-plugins:1.4.9"
gradlePlugins.kotlin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
gradlePlugins.dokka = "org.jetbrains.dokka:dokka-gradle-plugin:0.10.1"
gradlePlugins.maven = "com.github.dcendents:android-maven-gradle-plugin:2.1"
Expand All @@ -42,5 +43,5 @@ gradlePlugins.publish = "com.gradle.publish:plugin-publish-plugin:0.12.0"
ext.gradlePlugins = gradlePlugins

def sample = [:]
sample.huaweiPlugin = "1.2.4-SNAPSHOT"
sample.huaweiPlugin = "1.2.5-SNAPSHOT"
ext.sample = sample
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
kotlin.code.style=official
org.gradle.parallel=false

android.useAndroidX=false
android.useAndroidX=true
android.enableJetifier=false

2 changes: 2 additions & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
dependencies {
classpath gradlePlugins.gradle
classpath gradlePlugins.kotlin
classpath gradlePlugins.kotlinDsl
classpath gradlePlugins.dokka
classpath gradlePlugins.maven
classpath gradlePlugins.bintray
Expand All @@ -31,6 +32,7 @@ allprojects {

apply plugin: "groovy"
apply plugin: 'kotlin'
apply plugin: "org.gradle.kotlin.kotlin-dsl"
apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: "org.jetbrains.dokka"
apply plugin: 'com.jfrog.bintray'
Expand Down
2 changes: 1 addition & 1 deletion plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android.enableJetifier=true
####################################################################################################

GROUP=ru.cian
VERSION_NAME=1.2.4
VERSION_NAME=1.2.5-SNAPSHOT

POM_ARTIFACT_ID=huawei-publish-gradle-plugin
POM_NAME=Huawei Publish Gradle Plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private const val DEFAULT_PUBLISH_TIMEOUT_MS = 10 * 60 * 1000L
private const val DEFAULT_PUBLISH_PERIOD_MS = 15 * 1000L

open class HuaweiPublishExtension(
project: Project
project: Project
) {

val instances = project.container(HuaweiPublishExtensionConfig::class.java) { name ->
Expand All @@ -32,14 +32,14 @@ class HuaweiPublishExtensionConfig(

fun releasePhase(closure: Closure<ReleasePhaseExtension>): ReleasePhaseExtension {
releasePhase = ReleasePhaseExtension()
project.configure(releasePhase, closure)
project.configure(releasePhase!!, closure)
return releasePhase!!
}

/*
For required property use GradleProperty class instance. For example:
var credentialsPath by GradleProperty(project, String::class.java)
*/
/*
For required property use GradleProperty class instance. For example:
var credentialsPath by GradleProperty(project, String::class.java)
*/
var credentialsPath: String? = null
var deployType: DeployType = DeployType.PUBLISH
var publishTimeoutMs: Long = DEFAULT_PUBLISH_TIMEOUT_MS
Expand All @@ -51,16 +51,16 @@ class HuaweiPublishExtensionConfig(

override fun toString(): String {
return "HuaweiPublishExtensionConfig(" +
"name='$name', " +
"credentialsPath='$credentialsPath', " +
"deployType='$deployType', " +
"publishTimeoutMs='$publishTimeoutMs', " +
"publishPeriodMs='$publishPeriodMs', " +
"buildFormat='$buildFormat', " +
"buildFile='$buildFile', " +
"releaseTime='$releaseTime', " +
"releasePhase='$releasePhase'" +
")"
"name='$name', " +
"credentialsPath='$credentialsPath', " +
"deployType='$deployType', " +
"publishTimeoutMs='$publishTimeoutMs', " +
"publishPeriodMs='$publishPeriodMs', " +
"buildFormat='$buildFormat', " +
"buildFile='$buildFile', " +
"releaseTime='$releaseTime', " +
"releasePhase='$releasePhase'" +
")"
}
}

Expand All @@ -70,12 +70,20 @@ open class ReleasePhaseExtension {
var endTime: String? = null
var percent: Double? = null

constructor()

constructor(startTime: String?, endTime: String?, percent: Double?) {
this.startTime = startTime
this.endTime = endTime
this.percent = percent
}

override fun toString(): String {
return "ReleasePhaseConfig(" +
"startTime='$startTime', " +
"endTime='$endTime', " +
"percent='$percent'" +
")"
"startTime='$startTime', " +
"endTime='$endTime', " +
"percent='$percent'" +
")"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ru.cian.huawei.publish

import com.android.build.gradle.AppExtension
import com.android.build.api.extension.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.ApplicationVariant
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType

class HuaweiPublishPlugin : Plugin<Project> {

Expand All @@ -20,28 +22,30 @@ class HuaweiPublishPlugin : Plugin<Project> {
p.plugins.findPlugin(AppPlugin::class.java) != null
}

project.plugins.withType<AppPlugin> {
applyInternal(project)
}

project.afterEvaluate {
if (!findAppPlugin(project)) {
project.logger.warn(
"The Android Gradle Plugin was not applied. Huawei Publish Plugin " +
"will not be configured."
)
}
applyInternal(project)
// applyInternal(project)
}
}

private fun applyInternal(project: Project) {
val androidExtension = project.extensions.getByType(AppExtension::class.java)
androidExtension.applicationVariants.all { variant ->
if (!variant.buildType.isDebuggable) {
createTask(project, variant)
}
val androidExtension = project.extensions.getByType<ApplicationAndroidComponentsExtension>()
androidExtension.onVariants v@{ variant ->
createTask(project, variant)
}
}

@Suppress("DefaultLocale")
private fun createTask(project: Project, variant: BaseVariant) {
private fun createTask(project: Project, variant: ApplicationVariant) {
val variantName = variant.name.capitalize()
val taskName = "${HuaweiPublishTask.NAME}$variantName"
project.tasks.create(taskName, HuaweiPublishTask::class.java, variant)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.cian.huawei.publish

import com.android.build.gradle.api.BaseVariant
import com.android.build.api.variant.ApplicationVariant
import org.gradle.api.DefaultTask
import org.gradle.api.publish.plugins.PublishingPlugin
import org.gradle.api.tasks.Internal
Expand All @@ -21,12 +21,12 @@ import javax.inject.Inject

open class HuaweiPublishTask
@Inject constructor(
private val variant: BaseVariant
private val variant: ApplicationVariant
) : DefaultTask() {

init {
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Upload and publish application build file to Huawei AppGallery Store for ${variant.baseName} buildType"
description = "Upload and publish application build file to Huawei AppGallery Store for ${variant.name} buildType"
}

@get:Internal
Expand Down Expand Up @@ -128,7 +128,7 @@ open class HuaweiPublishTask
val appInfo = huaweiService.getAppID(
clientId = config.credentials.clientId,
token = token,
packageName = variant.applicationId
packageName = variant.applicationId.get()
)

Logger.i("Get Upload Url")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
package ru.cian.huawei.publish.utils

import com.android.build.api.artifact.ArtifactType
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.api.InstallableVariantImpl
import com.android.build.gradle.internal.scope.InternalArtifactType
import org.gradle.api.file.RegularFile
import com.android.build.api.variant.ApplicationVariant
import ru.cian.huawei.publish.BuildFormat
import java.io.File

internal class BuildFileProvider(private val variant: BaseVariant) {
internal class BuildFileProvider(private val variant: ApplicationVariant) {

fun getBuildFile(buildFormat: BuildFormat): File? {
return when(buildFormat) {
BuildFormat.APK -> getFinalApkArtifactCompat(variant)
BuildFormat.APK -> getFinalApkArtifactCompat(variant).singleOrNull()
BuildFormat.AAB -> getFinalBundleArtifactCompat(variant).singleOrNull()
}
}

private fun getFinalApkArtifactCompat(variant: BaseVariant): File {
return variant.outputs.first().outputFile
private fun getFinalApkArtifactCompat(variant: ApplicationVariant): List<File> {
return variant.artifacts.get(ArtifactType.APK).map {
variant.artifacts.getBuiltArtifactsLoader().load(it)
?.elements?.map { element -> File(element.outputFile) } ?: emptyList()
}.getOrElse( emptyList())
}

/**
* That's hack&trick due to https://issuetracker.google.com/issues/109918868
*/
private fun getFinalBundleArtifactCompat(variant: BaseVariant): Set<File> {
val installable = variant as InstallableVariantImpl
return try {
installable.getFinalArtifact(
InternalArtifactType.BUNDLE
).get().files
} catch (e: NoClassDefFoundError) {
val enumMethod =
InternalArtifactType::class.java.getMethod("valueOf", String::class.java)
val artifactType = enumMethod.invoke(null, "BUNDLE") as ArtifactType<RegularFile>
val artifact = installable.javaClass
.getMethod("getFinalArtifact", ArtifactType::class.java)
.invoke(installable, artifactType)
artifact.javaClass.getMethod("getFiles").apply {
isAccessible = true
}.invoke(artifact) as Set<File>
}
private fun getFinalBundleArtifactCompat(variant: ApplicationVariant): List<File> {
return variant.artifacts.get(ArtifactType.BUNDLE).map {
listOf(File(it.asFile.absolutePath))
}.getOrElse( emptyList())
}
}

0 comments on commit ec2321c

Please sign in to comment.