Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manifest generation refactoring #84

Merged
merged 4 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions application/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import java.util.Properties

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.2")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.4.1")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.5.0")
}
}

val properties = Properties()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be better to move this code block into androidProjectConfiguration? (underhood)
Or, It should lay on top-level gradle build script?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it needs a bit more refinement, next PR will do

val file: File = project.rootProject.file("local.properties")
if (file.exists()) file.inputStream().use { properties.load(it) }

// Enjoy easiest way to configure your Android project
androidProjectConfiguration(
minSdk = 21,
targetSdk = 29,
compileSdk = 29,
kotlinVersion = "1.4.21",
agpVersion = "4.2.0-beta02",
kotlinVersion = properties.getProperty("kotlinVersion", "1.4.21"),
agpVersion = properties.getProperty("agpVersion", "4.2.0-beta02"),
versionCode = 1,
versionName = "1.0",
dataBinding = true
)
)
1 change: 1 addition & 0 deletions plugins/android/src/main/java/androidApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fun Project.androidApp(
testInstrumentationRunner,
consumerMinificationFiles,
manifestPlaceholders,
// AndroidApp should always use custom manifest
generateManifest = false
)
applyFeatures(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tools.forma.android.utils.applyFrom
import tools.forma.validation.Validator
import tools.forma.validation.validator
import java.io.File
import org.gradle.api.Project

class AndroidLibraryFeatureConfiguration(
val packageName: String,
Expand All @@ -29,6 +30,8 @@ fun androidLibraryFeatureDefinition(
featureConfiguration = featureConfiguration,
configuration = { extension, feature, project, formaConfiguration ->
with(extension) {
maybeGenerateManifest(project, feature)

compileSdkVersion(formaConfiguration.compileSdk)

defaultConfig.applyFrom(
Expand All @@ -43,37 +46,43 @@ fun androidLibraryFeatureDefinition(

buildFeatures.dataBinding = feature.dataBinding
buildFeatures.viewBinding = feature.viewBinding

if (feature.generateManifest) {
val path = generateManifest(project.buildDir, feature.packageName)
sourceSets {
/**
* Manifest file resolved during configuration,
* so we need to create file before we get into plugin is configured
*/
getByName("main").manifest.srcFile(path)
}
}
}
}
)

private fun LibraryExtension.maybeGenerateManifest(
project: Project,
feature: AndroidLibraryFeatureConfiguration
) {
val manifestFile = manifestFile(project.buildDir, feature.packageName)
populateManifest(manifestFile, feature.packageName)
sourceSets {
/**
* Manifest file resolved during configuration,
* so we need to create file before we get into plugin is configured
*/
getByName("main").manifest.srcFile(manifestFile.path)
}
}

fun manifestFile(buildDir: File, packageName: String) =
File(buildDir, "tmp/manifest/${packageName}.xml")

/**
* Manifest file generated and added it to sourceSet
* @param buildDir - project's build dir
* @param packageName - will be injected to manifest template
* @return path to generated file
*/
private fun generateManifest(
buildDir: File,
private fun populateManifest(
manifestFile: File,
packageName: String
): String {
/**
* Some naive caching here, file name equals package name
* We create new file only if package is changed or on first build
*/
val tmpManifest = File(buildDir, "tmp/manifest/${packageName}.xml")
with(tmpManifest) {
with(manifestFile) {
if (!exists()) {
parentFile.mkdirs()
createNewFile()
Expand All @@ -84,5 +93,5 @@ private fun generateManifest(
)
}
}
return tmpManifest.path
return manifestFile.path
}