1- /*
2- * This file is part of LSPosed.
3- *
4- * LSPosed is free software: you can redistribute it and/or modify
5- * it under the terms of the GNU General Public License as published by
6- * the Free Software Foundation, either version 3 of the License, or
7- * (at your option) any later version.
8- *
9- * LSPosed is distributed in the hope that it will be useful,
10- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- * GNU General Public License for more details.
13- *
14- * You should have received a copy of the GNU General Public License
15- * along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
16- *
17- * Copyright (C) 2021 - 2022 LSPosed Contributors
18- */
19-
201import com.android.build.api.dsl.ApplicationDefaultConfig
212import com.android.build.api.dsl.CommonExtension
223import com.android.build.gradle.api.AndroidBasePlugin
234import com.ncorti.ktfmt.gradle.tasks.KtfmtFormatTask
5+ import java.io.ByteArrayOutputStream
6+ import javax.inject.Inject
7+ import org.gradle.api.provider.ValueSource
8+ import org.gradle.api.provider.ValueSourceParameters
9+ import org.gradle.process.ExecOperations
2410
2511plugins {
2612 alias(libs.plugins.lsplugin.cmaker)
@@ -32,43 +18,90 @@ plugins {
3218 alias(libs.plugins.ktfmt)
3319}
3420
21+ /* * A ValueSource that executes 'git rev-list --count' to get the total commit count. */
22+ abstract class GitCommitCountValueSource : ValueSource <String , ValueSourceParameters .None > {
23+ @get:Inject abstract val execOperations: ExecOperations
24+
25+ override fun obtain (): String {
26+ val output = ByteArrayOutputStream ()
27+ val result =
28+ execOperations.exec {
29+ commandLine(" git" , " rev-list" , " --count" , " refs/remotes/origin/master" )
30+ standardOutput = output
31+ isIgnoreExitValue = true
32+ }
33+ // Return the count if successful, otherwise a default of "1".
34+ return if (result.exitValue == 0 && output.toString().isNotBlank()) {
35+ output.toString().trim()
36+ } else {
37+ " 1"
38+ }
39+ }
40+ }
41+
42+ /* * A ValueSource that executes 'git tag' to get the latest version tag. */
43+ abstract class GitLatestTagValueSource : ValueSource <String , ValueSourceParameters .None > {
44+ @get:Inject abstract val execOperations: ExecOperations
45+
46+ override fun obtain (): String {
47+ val output = ByteArrayOutputStream ()
48+ val result =
49+ execOperations.exec {
50+ commandLine(" git" , " tag" , " --list" , " --sort=-v:refname" )
51+ standardOutput = output
52+ isIgnoreExitValue = true
53+ }
54+ // If successful, parse the first line. Provide a default if no tags are found.
55+ return if (result.exitValue == 0 && output.toString().isNotBlank()) {
56+ output.toString().lineSequence().first().removePrefix(" v" )
57+ } else {
58+ " 1.0"
59+ }
60+ }
61+ }
62+
63+ // This defers the execution of the git commands and allows Gradle to cache the results.
64+ val versionCodeProvider by extra(providers.of(GitCommitCountValueSource ::class .java) {})
65+ val versionNameProvider by extra(providers.of(GitLatestTagValueSource ::class .java) {})
66+
67+ val repo = jgit.repo()
68+ val commitCount = (repo?.commitCount(" refs/remotes/origin/master" ) ? : 1 ) + 4200
69+ val latestTag = repo?.latestTag?.removePrefix(" v" ) ? : " 1.0"
70+
71+ val injectedPackageName by extra(" com.android.shell" )
72+ val injectedPackageUid by extra(2000 )
73+
74+ val defaultManagerPackageName by extra(" org.lsposed.manager" )
75+ val verCode by extra(commitCount)
76+ val verName by extra(latestTag)
77+
3578cmaker {
3679 default {
3780 arguments.addAll(
81+ arrayOf(" -DVECTOR_ROOT=${rootDir.absolutePath} " , " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" )
82+ )
83+ val flags =
3884 arrayOf(
39- " -DEXTERNAL_ROOT=${File (rootDir.absolutePath, " external" )} " ,
40- " -DCORE_ROOT=${File (rootDir.absolutePath, " core/src/main/jni" )} " ,
41- " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" ,
85+ " -DINJECTED_UID=$injectedPackageUid " ,
86+ " -DVERSION_CODE=${verCode} " ,
87+ " -DVERSION_NAME='\" ${verName} \" '" ,
88+ " -Wno-gnu-string-literal-operator-template" ,
89+ " -Wno-c++2b-extensions" ,
4290 )
43- )
44- val flags = arrayOf(
45- " -DINJECTED_AID=$injectedPackageUid " ,
46- " -Wno-gnu-string-literal-operator-template" ,
47- " -Wno-c++2b-extensions" ,
48- )
4991 cFlags.addAll(flags)
5092 cppFlags.addAll(flags)
5193 abiFilters(" arm64-v8a" , " armeabi-v7a" , " x86" , " x86_64" )
5294 }
5395 buildTypes {
5496 if (it.name == " release" ) {
55- arguments + = " -DDEBUG_SYMBOLS_PATH=${
97+ arguments + =
98+ " -DDEBUG_SYMBOLS_PATH=${
5699 layout.buildDirectory.dir(" symbols" ).get().asFile.absolutePath
57100 } "
58101 }
59102 }
60103}
61104
62- val repo = jgit.repo()
63- val commitCount = (repo?.commitCount(" refs/remotes/origin/master" ) ? : 1 ) + 4200
64- val latestTag = repo?.latestTag?.removePrefix(" v" ) ? : " 1.0"
65-
66- val injectedPackageName by extra(" com.android.shell" )
67- val injectedPackageUid by extra(2000 )
68-
69- val defaultManagerPackageName by extra(" org.lsposed.manager" )
70- val verCode by extra(commitCount)
71- val verName by extra(latestTag)
72105val androidTargetSdkVersion by extra(36 )
73106val androidMinSdkVersion by extra(27 )
74107val androidBuildToolsVersion by extra(" 36.0.0" )
@@ -77,9 +110,7 @@ val androidCompileNdkVersion by extra("29.0.13113456")
77110val androidSourceCompatibility by extra(JavaVersion .VERSION_21 )
78111val androidTargetCompatibility by extra(JavaVersion .VERSION_21 )
79112
80- tasks.register(" Delete" , Delete ::class ) {
81- delete(rootProject.layout.buildDirectory)
82- }
113+ tasks.register(" Delete" , Delete ::class ) { delete(rootProject.layout.buildDirectory) }
83114
84115subprojects {
85116 plugins.withType(AndroidBasePlugin ::class .java) {
@@ -127,6 +158,7 @@ tasks.register<KtfmtFormatTask>("format") {
127158 source = project.fileTree(rootDir)
128159 include(" *.gradle.kts" , " */build.gradle.kts" )
129160 dependsOn(" :xposed:ktfmtFormat" )
161+ dependsOn(" :zygisk:ktfmtFormat" )
130162}
131163
132164ktfmt { kotlinLangStyle() }
0 commit comments