1 /*
<lambda>null2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.build.resources
18 
19 import androidx.build.getSupportRootFolder
20 import com.android.build.api.dsl.KotlinMultiplatformAndroidLibraryTarget
21 import com.android.build.api.variant.LibraryVariant
22 import java.io.File
23 import org.gradle.api.Project
24 import org.gradle.api.tasks.Copy
25 import org.gradle.api.tasks.TaskProvider
26 import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
27 import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
28 
29 fun configurePublicResourcesStub(
30     libraryVariant: LibraryVariant,
31     copyPublicResourcesDirTask: TaskProvider<CopyPublicResourcesDirTask>
32 ) =
33     libraryVariant.sources.res.also {
34         it?.addGeneratedSourceDirectory(
35             copyPublicResourcesDirTask,
36             CopyPublicResourcesDirTask::outputFolder
37         )
38     }
39 
Projectnull40 fun Project.configurePublicResourcesStub(kmpExtension: KotlinMultiplatformExtension) {
41     val targetRes = project.layout.buildDirectory.dir("generated/res/public-stub")
42 
43     val generatePublicResourcesStub =
44         tasks.register("generatePublicResourcesStub", Copy::class.java) { task ->
45             task.from(File(project.getSupportRootFolder(), "buildSrc/res"))
46             task.into(targetRes)
47         }
48     val sourceSet =
49         kmpExtension.targets
50             .withType(KotlinMultiplatformAndroidLibraryTarget::class.java)
51             .single()
52             .compilations
53             .getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
54             .defaultSourceSet
55     sourceSet.resources.srcDir(
56         generatePublicResourcesStub.flatMap { project.provider { it.destinationDir } }
57     )
58 }
59