-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
65 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 12 additions & 28 deletions
40
plugin/src/main/kotlin/ru/cian/huawei/publish/utils/BuildFileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |