blob: d855df7dc82ce678b6d64dea8171e83daaf895ff [file] [log] [blame]
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.android.build.api.apiTest
import com.google.common.truth.Truth
import org.junit.Test
import kotlin.test.assertNotNull
class ManifestPlaceholderApiTests: VariantApiBaseTest(TestType.Script) {
private val testingElements= TestingElements(scriptingLanguage)
@Test
fun addCustomManifestPlaceholder() {
given {
tasksToInvoke.add("debugManifestReader")
addModule(":app") {
manifest =
// language=xml
"""<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.build.example.minimal">
<application android:label="Minimal">
<activity android:name="${"$"}{MyName}">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
""".trimIndent()
@Suppress("RemoveExplicitTypeArguments")
buildFile =
// language=kotlin
"""
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
}
import com.android.build.api.artifact.ArtifactType
abstract class ManifestReaderTask: DefaultTask() {
@get:InputFile
abstract val mergedManifest: RegularFileProperty
@TaskAction
fun taskAction() {
val manifest = mergedManifest.asFile.get().readText()
// ensure that merged manifest contains the right activity name.
if (!manifest.contains("activity android:name=\"com.android.build.example.minimal.MyRealName\""))
throw RuntimeException("Manifest Placeholder not replaced successfully")
}
}
android {
${testingElements.addCommonAndroidBuildLogic()}
onVariantProperties {
val manifestReader = tasks.register<ManifestReaderTask>("${'$'}{name}ManifestReader") {
mergedManifest.set(artifacts.get(ArtifactType.MERGED_MANIFEST))
}
manifestPlaceholders.put("MyName", "MyRealName")
}
}
""".trimIndent()
}
}
withDocs {
index =
// language=markdown
"""
# Adding a manifest file placeholder in Kotlin.
See [manifest placeholder documentation](https://developer.android.com/studio/build/manifest-build-variables) for details
This sample show how to add a manifest placeholder value through the variant API. The value is
known at configuration time.
""".trimIndent()
}
check {
assertNotNull(this)
Truth.assertThat(output).contains("BUILD SUCCESSFUL")
}
}
@Test
fun addManifestPlaceholderFromTask() {
given {
tasksToInvoke.add("debugManifestReader")
addModule(":app") {
manifest =
// language=xml
"""<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.build.example.minimal">
<application android:label="Minimal">
<activity android:name="${"$"}{MyName}">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
""".trimIndent()
@Suppress("RemoveExplicitTypeArguments")
buildFile =
// language=kotlin
"""
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
}
import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import com.android.build.api.artifact.ArtifactType
${testingElements.getGitVersionTask()}
val gitVersionProvider = tasks.register<GitVersionTask>("gitVersionProvider") {
File(project.buildDir, "intermediates/gitVersionProvider/output").also {
it.parentFile.mkdirs()
gitVersionOutputFile.set(it)
}
outputs.upToDateWhen { false }
}
abstract class ManifestReaderTask: DefaultTask() {
@get:InputFile
abstract val mergedManifest: RegularFileProperty
@TaskAction
fun taskAction() {
val manifest = mergedManifest.asFile.get().readText()
// ensure that merged manifest contains the right activity name.
if (!manifest.contains("activity android:name=\"com.android.build.example.minimal.NameWithGit"))
throw RuntimeException("Manifest Placeholder not replaced successfully")
}
}
android {
${testingElements.addCommonAndroidBuildLogic()}
onVariantProperties {
val manifestReader = tasks.register<ManifestReaderTask>("${'$'}{name}ManifestReader") {
mergedManifest.set(artifacts.get(ArtifactType.MERGED_MANIFEST))
}
manifestPlaceholders.put("MyName", gitVersionProvider.map { task ->
"NameWithGit" + task.gitVersionOutputFile.get().asFile.readText(Charsets.UTF_8)
})
}
}""".trimIndent()
}
}
withDocs {
index =
// language=markdown
"""
# Adding a BuildConfig field in Kotlin
This sample show how to add a field in the BuildConfig class for which the value is not known at
configuration time.
The added field is used in the MainActivity.kt file.
""".trimIndent()
}
check {
assertNotNull(this)
Truth.assertThat(output).contains("BUILD SUCCESSFUL")
}
}
}