summaryrefslogtreecommitdiff
path: root/extensions/library/build.gradle
blob: e9a02bb5ae8420890f8012fa7cc27efff56d6bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2014 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.
 */
import com.android.build.api.artifact.MultipleArtifact;
import com.android.build.api.artifact.ScopedArtifact;
import com.android.build.api.variant.ScopedArtifacts;
import java.util.jar.*;
import java.util.regex.*;

apply plugin: 'com.android.library'

android {
    namespace "androidx.databinding.library"
    compileSdkVersion dataBindingConfig.compileSdkVersion
    buildToolsVersion dataBindingConfig.buildToolsVersion

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion dataBindingConfig.targetSdkVersion
        versionCode 1
        versionName "1.0"
        consumerProguardFiles 'proguard-rules.txt'
    }
    compileOptions {
        sourceCompatibility dataBindingConfig.javaTargetCompatibility
        targetCompatibility dataBindingConfig.javaSourceCompatibility
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'android/databinding/DataBinderMapperImpl.class'
    }
    buildTypes.all {
        consumerProguardFiles 'proguard-consumer-rules.pro'
    }

    androidComponents {
        onVariants(selector().all(), { variant ->
            TaskProvider<ExcludeShimsTask> taskProvider = project.tasks.register(
                    variant.getName() + "ExcludeShimsTask", ExcludeShimsTask.class
            )

            variant.artifacts
                    .forScope(ScopedArtifacts.Scope.PROJECT)
                    .use(taskProvider)
                    .toTransform(
                        ScopedArtifact.CLASSES.INSTANCE,
                        { it.getAllJars() },
                        { it.getAllDirectories() },
                        { it.getOutput() }
                    )

            taskProvider.configure { task ->
                task.excludes.add("androidx/databinding/DataBindingComponent.*")
                task.excludes.add("androidx/databinding/DataBinderMapperImpl.*")
            }
        })
    }

    publishing {
        singleVariant("release")
    }
}

dependencies {
    implementation rootProject.ext.libs.android_support_collection
    api "androidx.databinding:databinding-common:${dataBindingConfig.version}"
    api project(':viewbinding')
    api rootProject.ext.libs.android_arch_lifecycle_runtime
    compileOnly rootProject.ext.libs.lifecycle_livedata
    compileOnly rootProject.ext.libs.fragments
}

//create jar tasks
android.libraryVariants.all { variant ->
    def name = variant.buildType.name

    if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
        return // Skip debug builds.
    }
    def suffix = name.capitalize()

    def sourcesJarTask = project.tasks.create(name: "sourceJar${suffix}", type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                artifactId = 'databinding-runtime'
                from components.release
                artifact tasks.named("sourceJarRelease")

                pom {
                    licenses {
                        license {
                            name = dataBindingConfig.licenseName
                            url = dataBindingConfig.licenseUrl
                            distribution = dataBindingConfig.licenseDistribution
                        }
                    }
                }
            }
        }
    }
}

/**
 * Remove shim classes that will be generated by the application annotation processor.
 */
abstract class ExcludeShimsTask extends Jar {
    @InputFiles
    abstract ListProperty<RegularFile> getAllJars();

    @InputFiles
    abstract ListProperty<Directory> getAllDirectories();

    @OutputFiles
    abstract RegularFileProperty getOutput();

    @Input
    Set<String> excludes = new HashSet<String>();

    @TaskAction
    void taskAction() {

        OutputStream jarOutput = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(
           output.get().getAsFile()
        )))

        Set<Pattern> patterns = excludes.collect {
            Pattern.compile(it)
        }

        allJars.get().forEach { file ->
            JarFile jarFile = new JarFile(file.asFile)
            for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) {
                JarEntry jarEntry = e.nextElement();
                if (!shouldIgnore(patterns, jarEntry.name)) {
                    jarOutput.putNextEntry(new JarEntry(jarEntry.name))
                    jarFile.getInputStream(jarEntry).withCloseable {
                        jarOutput << it
                    }
                    jarOutput.closeEntry()
                }
            }
            jarFile.close()
       }

        allDirectories.get().forEach { directory ->
            directory.asFile.traverse(type: groovy.io.FileType.FILES) { file ->
                String relativePath = directory.asFile.toURI().relativize(file.toURI()).getPath()
                        .replace(File.separatorChar, '/' as char)

                if (!shouldIgnore(patterns, relativePath)) {
                    jarOutput.putNextEntry(new JarEntry(relativePath))
                    new FileInputStream(file).withCloseable { inputStream ->
                        jarOutput << inputStream
                    }
                    jarOutput.closeEntry()
                }
            }
        }
        jarOutput.close()
    }

    boolean shouldIgnore(Collection<Pattern> patterns, String entryName) {
        for (pattern in patterns) {
            if (pattern.matcher(entryName).matches()) {
                return true
            }
        }
        return false
    }
}

task prebuildAar(type : Copy) {
    dependsOn tasks.named("publish")
    from "$buildDir/outputs/aar/library-release.aar"
    into dataBindingConfig.prebuildFolder
    rename { String fileName ->
        "databinding-library.aar"
    }
}