-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
build.gradle.kts
364 lines (328 loc) · 14 KB
/
build.gradle.kts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.ClassContext
import com.android.build.api.instrumentation.ClassData
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import com.github.jk1.license.LicenseReportExtension
import com.github.jk1.license.render.ReportRenderer
import com.github.jk1.license.render.TextReportRenderer
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.CHECKCAST
import org.objectweb.asm.Opcodes.INVOKESTATIC
buildscript {
dependencies {
classpath(libs.android.gradlePlugin)
classpath(libs.kotlin.gradlePlugin)
}
}
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.dependencyLicenseReport)
alias(libs.plugins.compose.compiler)
}
val licenseResDir = File("$projectDir/build/dependency-license-res")
fun String.runCommand(
workingDir: File = File("."),
timeoutAmount: Long = 60,
timeoutUnit: TimeUnit = TimeUnit.SECONDS
): String = ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply { waitFor(timeoutAmount, timeoutUnit) }
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isNotEmpty()) {
throw Exception(error)
}
inputStream.bufferedReader().readText().trim()
}
android {
namespace = "org.kde.kdeconnect_tp"
compileSdk = 35
defaultConfig {
minSdk = 21
targetSdk = 35
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
buildFeatures {
viewBinding = true
compose = true
buildConfig = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_9
targetCompatibility = JavaVersion.VERSION_1_9
// Flag to enable support for the new language APIs
isCoreLibraryDesugaringEnabled = true
}
kotlinOptions {
jvmTarget = "9"
}
androidResources {
generateLocaleConfig = true
}
sourceSets {
getByName("main") {
manifest.srcFile("AndroidManifest.xml")
java.setSrcDirs(listOf("src"))
resources.setSrcDirs(listOf("resources"))
res.setSrcDirs(listOf(licenseResDir, "res"))
assets.setSrcDirs(listOf("assets"))
}
getByName("test") {
java.setSrcDirs(listOf("tests"))
}
}
packaging {
resources {
merges += listOf("META-INF/DEPENDENCIES", "META-INF/LICENSE", "META-INF/NOTICE")
}
}
signingConfigs {
getByName("debug") {
storeFile = file("debug.keystore")
storePassword = "android"
keyAlias = "androiddebugkey"
keyPassword = "android"
}
}
buildTypes {
getByName("debug") {
isMinifyEnabled = false
isShrinkResources = false
signingConfig = signingConfigs.getByName("debug")
}
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
}
}
lint {
abortOnError = false
checkReleaseBuilds = false
}
applicationVariants.all {
val variant = this
logger.quiet("Found a variant called ${variant.name}")
if (variant.buildType.isDebuggable) {
variant.outputs.all {
val output = this as com.android.build.gradle.internal.api.BaseVariantOutputImpl
if (output.outputFile.name.endsWith(".apk")) {
// Default output filename is "${project.name}-${v.name}.apk". We want
// the Git commit short-hash to be added onto that default filename.
try {
val hash = "git rev-parse --short HEAD".runCommand(workingDir = rootDir)
val newName = "${project.name}-${variant.name}-${hash}.apk"
logger.quiet(" Found an output file ${output.outputFile.name}, renaming to $newName")
output.outputFileName = newName
} catch (ignored: Exception) {
logger.warn("Could not make use of the 'git' command-line tool. Output filenames will not be customized.")
}
}
}
}
}
}
/**
* Fix PosixFilePermission class type check issue.
*
* It fixed the class cast exception when lib desugar enabled and minSdk < 26.
*/
abstract class FixPosixFilePermissionClassVisitorFactory :
AsmClassVisitorFactory<FixPosixFilePermissionClassVisitorFactory.Params> {
override fun createClassVisitor(
classContext: ClassContext,
nextClassVisitor: ClassVisitor
): ClassVisitor {
return object : ClassVisitor(instrumentationContext.apiVersion.get(), nextClassVisitor) {
override fun visitMethod(
access: Int,
name: String?,
descriptor: String?,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
if (name == "attributesToPermissions") { // org.apache.sshd.sftp.common.SftpHelper.attributesToPermissions
return object : MethodVisitor(
instrumentationContext.apiVersion.get(),
super.visitMethod(access, name, descriptor, signature, exceptions)
) {
override fun visitTypeInsn(opcode: Int, type: String?) {
// We need to prevent Android Desugar modifying the `PosixFilePermission` classname.
//
// Android Desugar will replace `CHECKCAST java/nio/file/attribute/PosixFilePermission`
// to `CHECKCAST j$/nio/file/attribute/PosixFilePermission`.
// We need to replace it with `CHECKCAST java/lang/Enum` to prevent Android Desugar from modifying it.
if (opcode == CHECKCAST && type == "java/nio/file/attribute/PosixFilePermission") {
println("Bypass PosixFilePermission type check success.")
// `Enum` is the superclass of `PosixFilePermission`.
// Due to `Object` is not the superclass of `Enum`, we need to use `Enum` instead of `Object`.
super.visitTypeInsn(opcode, "java/lang/Enum")
} else {
super.visitTypeInsn(opcode, type)
}
}
}
}
return super.visitMethod(access, name, descriptor, signature, exceptions)
}
}
}
override fun isInstrumentable(classData: ClassData): Boolean {
return (classData.className == "org.apache.sshd.sftp.common.SftpHelper").also {
if (it) println("SftpHelper Found! Instrumenting...")
}
}
interface Params : InstrumentationParameters
}
/**
* Collections.unmodifiableXXX is not exist when Android API level is lower than 26.
* So we replace the call to Collections.unmodifiableXXX with the original collection by removing the call.
*/
abstract class FixCollectionsClassVisitorFactory :
AsmClassVisitorFactory<FixCollectionsClassVisitorFactory.Params> {
override fun createClassVisitor(
classContext: ClassContext,
nextClassVisitor: ClassVisitor
): ClassVisitor {
return object : ClassVisitor(instrumentationContext.apiVersion.get(), nextClassVisitor) {
override fun visitMethod(
access: Int,
name: String?,
descriptor: String?,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
return object : MethodVisitor(
instrumentationContext.apiVersion.get(),
super.visitMethod(access, name, descriptor, signature, exceptions)
) {
override fun visitMethodInsn(
opcode: Int,
type: String?,
name: String?,
descriptor: String?,
isInterface: Boolean
) {
val backportClass = "org/kde/kdeconnect/Helpers/CollectionsBackport"
if (opcode == INVOKESTATIC && type == "java/util/Collections") {
val replaceRules = mapOf(
"unmodifiableNavigableSet" to "(Ljava/util/NavigableSet;)Ljava/util/NavigableSet;",
"unmodifiableSet" to "(Ljava/util/Set;)Ljava/util/Set;",
"unmodifiableNavigableMap" to "(Ljava/util/NavigableMap;)Ljava/util/NavigableMap;",
"emptyNavigableMap" to "()Ljava/util/NavigableMap;")
if (name in replaceRules && descriptor == replaceRules[name]) {
super.visitMethodInsn(opcode, backportClass, name, descriptor, isInterface)
val calleeClass = classContext.currentClassData.className
println("Replace Collections.$name call with CollectionsBackport.$name from $calleeClass success.")
return
}
}
super.visitMethodInsn(opcode, type, name, descriptor, isInterface)
}
}
}
}
}
override fun isInstrumentable(classData: ClassData): Boolean {
return classData.className.startsWith("org.apache.sshd") // We only need to fix the Apache SSHD library
}
interface Params : InstrumentationParameters
}
androidComponents {
onVariants { variant ->
variant.instrumentation.transformClassesWith(
FixPosixFilePermissionClassVisitorFactory::class.java,
InstrumentationScope.ALL
) { }
variant.instrumentation.transformClassesWith(
FixCollectionsClassVisitorFactory::class.java,
InstrumentationScope.ALL
) { }
}
}
dependencies {
// It has a bug that causes a crash when using PosixFilePermission and minSdk < 26.
// It has been used in SSHD Core.
// We have taken a workaround to fix it.
// See `FixPosixFilePermissionClassVisitorFactory` for more details.
coreLibraryDesugaring(libs.android.desugarJdkLibsNio)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.constraintlayout.compose)
implementation(libs.androidx.compose.ui.tooling.preview)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.androidx.media)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.preference.ktx)
implementation(libs.androidx.recyclerview)
implementation(libs.androidx.swiperefreshlayout)
implementation(libs.androidx.documentfile)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.extensions)
implementation(libs.androidx.lifecycle.common.java8)
implementation(libs.androidx.gridlayout)
implementation(libs.material)
implementation(libs.disklrucache) //For caching album art bitmaps
implementation(libs.slf4j.handroid)
implementation(libs.apache.sshd.core)
implementation(libs.apache.sshd.sftp)
implementation(libs.apache.sshd.scp)
implementation(libs.apache.sshd.mina)
implementation(libs.apache.mina.core)
//implementation("com.github.bright:slf4android:0.1.6") { transitive = true } // For org.apache.sshd debugging
implementation(libs.bcpkix.jdk15on) //For SSL certificate generation
implementation(libs.classindex)
kapt(libs.classindex)
// The android-smsmms library is the only way I know to handle MMS in Android
// (Shouldn't a phone OS make phone things easy?)
// This library was originally authored as com.klinkerapps at https://github.com/klinker41/android-smsmms.
// However, that version is under-loved. I have therefore made "some fixes" and published it.
// Please see https://invent.kde.org/sredman/android-smsmms/-/tree/master
implementation(libs.android.smsmms)
implementation(libs.logger)
implementation(libs.commons.io)
implementation(libs.commons.collections4)
implementation(libs.commons.lang3)
implementation(libs.univocity.parsers)
// Kotlin
implementation(libs.kotlin.stdlib.jdk8)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)
// Testing
testImplementation(libs.junit)
testImplementation(libs.mockito.core)
testImplementation(libs.jsonassert)
// For device controls
implementation(libs.reactive.streams)
implementation(libs.rxjava)
}
licenseReport {
configurations = LicenseReportExtension.ALL
renderers = arrayOf<ReportRenderer>(TextReportRenderer())
}
tasks.named("generateLicenseReport") {
doLast {
val target = File(licenseResDir, "raw/license")
target.parentFile.mkdirs()
target.writeText(
files(
layout.projectDirectory.file("COPYING"),
layout.buildDirectory.file("reports/dependency-license/THIRD-PARTY-NOTICES.txt")
).joinToString(separator = "\n") {
it.readText()
}
)
}
}
tasks.named("preBuild") {
dependsOn("generateLicenseReport")
}