Skip to content

Commit 9f18768

Browse files
committed
feat: Add ability to globally configure extension projects
1 parent c0ef614 commit 9f18768

File tree

6 files changed

+96
-32
lines changed

6 files changed

+96
-32
lines changed

Diff for: README.md

+37-17
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ ReVanced Patches Gradle plugin configures a project to develop ReVanced Patches.
7272
For that, the plugin provides:
7373

7474
- The [settings plugin](src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt):
75-
Applied to the `settings.gradle.kts` file, configures the project repositories and subprojects
75+
Applied to the `settings.gradle.kts` file, configures the project repositories and subprojects
7676
- The [patches plugin](src/main/kotlin/app/revanced/patches/gradle/PatchesPlugin.kt):
77-
Applied to the patches subproject by the settings plugin
77+
Applied to the patches subproject by the settings plugin
7878
- The [extension plugin](src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt):
79-
Applied to extension subprojects by the settings plugin
79+
Applied to extension subprojects by the settings plugin
8080

8181
> [!CAUTION]
8282
> This plugin is not stable yet and likely to change due to lacking experience with Gradle plugins.
@@ -97,18 +97,36 @@ pluginManagement {
9797
gradlePluginPortal()
9898
google()
9999
maven {
100-
name = "GitHubPackages"
101-
url = uri("https://maven.pkg.github.com/revanced/registry")
102-
credentials {
103-
username = providers.gradleProperty("gpr.user")
104-
password = providers.gradleProperty("gpr.key")
105-
}
100+
name = "GitHubPackages"
101+
url = uri("https://maven.pkg.github.com/revanced/registry")
102+
credentials {
103+
username = providers.gradleProperty("gpr.user")
104+
password = providers.gradleProperty("gpr.key")
105+
}
106106
}
107107
}
108108
}
109109

110110
plugins {
111-
id("app.revanced.patches") version "<version>"
111+
id("app.revanced.patches") version "<version>"
112+
}
113+
114+
// This block is optional and can be used to configure the patches and extension projects.
115+
settings {
116+
// "patches" is the default.
117+
patchesProjectPath = "patches"
118+
119+
extensions {
120+
// The path containing the extension projects. "extensions" is the default.
121+
projectsPath = "extensions"
122+
123+
// A default namespace for extension projects. null is the default.
124+
defaultNamespace = "app.revanced.extension"
125+
126+
// Proguard files relative to the extension project.
127+
// By default, isMinifyEnabled is false, unless a ProGuard file is added.
128+
proguardFiles("../proguard-rules.pro")
129+
}
112130
}
113131
```
114132

@@ -132,15 +150,15 @@ patches {
132150
> [!NOTE]
133151
> By default, the plugin expects the patches project to be in the `patches` directory.
134152
135-
Create the extension project and configure the `build.gradle.kts` file:
153+
Create the extension project and add an empty `build.gradle.kts` file.
154+
Unless the `build.gradle.kts` file is empty, the plugin will not recognize the extension project.
155+
By default, the extension name will be inferred from the relative path to the extension project.
156+
For example, the extension name for the `extensions/extension` project will be `extensions/extension.rve`.
157+
To set an extension name explicitly, add the following to the `build.gradle.kts` file:
136158

137159
```kotlin
138160
extension {
139-
name = "extensions/extension.rve"
140-
}
141-
142-
android {
143-
namespace = "app.revanced.extension"
161+
name = "extensions/extension.rve"
144162
}
145163
```
146164

@@ -162,7 +180,9 @@ To build ReVanced Patches Gradle plugin, follow these steps:
162180
## 📜 Licence
163181

164182
ReVanced Patches Gradle plugin is licensed under the GPLv3 license.
165-
Please see the [license file](LICENSE) for more information. [tl;dr](https://www.tldrlegal.com/license/gnu-general-public-license-v3-gpl-3) you may copy, distribute and modify
183+
Please see the [license file](LICENSE) for more
184+
information. [tl;dr](https://www.tldrlegal.com/license/gnu-general-public-license-v3-gpl-3) you may copy, distribute and
185+
modify
166186
ReVanced Patches Gradle plugin as long as you track changes/dates in source files.
167187
Any modifications to ReVanced Patches Gradle plugin must also be made available under the GPL,
168188
along with build & install instructions.

Diff for: gradle.properties

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
org.gradle.parallel = true
2-
org.gradle.caching = true
3-
kotlin.code.style = official
4-
version = 1.0.0-dev.6
1+
org.gradle.parallel=true
2+
org.gradle.caching=true
3+
kotlin.code.style=official
4+
version=1.0.0-dev.6

Diff for: src/main/kotlin/app/revanced/patches/gradle/ExtensionPlugin.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ abstract class ExtensionPlugin : Plugin<Project> {
1919
override fun apply(project: Project) {
2020
val extension = project.extensions.create("extension", ExtensionExtension::class.java)
2121

22-
project.configureAndroid()
22+
val settingsExtensionProvider = project.gradle.sharedServices.registrations
23+
.findByName("settingsExtensionProvider")!!.service.get() as SettingsExtensionProvider
24+
25+
project.configureAndroid(settingsExtensionProvider)
2326
project.configureArtifactSharing(extension)
2427
}
2528

@@ -64,15 +67,16 @@ abstract class ExtensionPlugin : Plugin<Project> {
6467
/**
6568
* Set up the Android plugin for the extension project.
6669
*/
67-
private fun Project.configureAndroid() {
70+
private fun Project.configureAndroid(settingsExtensionProvider: SettingsExtensionProvider) {
6871
pluginManager.apply {
6972
apply(AppPlugin::class.java)
7073
apply(KotlinAndroidPluginWrapper::class.java)
7174
}
7275

7376
extensions.configure(BaseAppModuleExtension::class.java) {
7477
it.apply {
75-
compileSdk = 33
78+
compileSdk = 34
79+
namespace = settingsExtensionProvider.parameters.defaultNamespace
7680

7781
defaultConfig {
7882
minSdk = 23
@@ -81,13 +85,11 @@ abstract class ExtensionPlugin : Plugin<Project> {
8185

8286
buildTypes {
8387
release {
84-
// If this were true by default, and no proguard files would be present,
85-
// no dex file would be generated.
86-
isMinifyEnabled = false
88+
isMinifyEnabled = settingsExtensionProvider.parameters.proguardFiles.isNotEmpty()
8789

8890
proguardFiles(
8991
getDefaultProguardFile("proguard-android-optimize.txt"),
90-
"proguard-rules.pro",
92+
*settingsExtensionProvider.parameters.proguardFiles.toTypedArray(),
9193
)
9294
}
9395
}

Diff for: src/main/kotlin/app/revanced/patches/gradle/SettingsExtension.kt

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,30 @@ open class SettingsExtension {
1111
val extensions = ExtensionsExtension()
1212

1313
fun extensions(block: ExtensionsExtension.() -> Unit) {
14-
ExtensionsExtension().apply(block)
14+
extensions.apply(block)
1515
}
1616

1717
class ExtensionsExtension {
1818
/**
19-
* The path to the project containing the extension projects.
19+
* The path containing the extension projects.
2020
*/
21-
var projectPath: String? = "extensions"
21+
var projectsPath: String? = "extensions"
22+
23+
/**
24+
* The default namespace for the extension projects.
25+
*/
26+
var defaultNamespace: String? = null
27+
28+
internal val proguardFiles = mutableSetOf<String>()
29+
30+
/**
31+
* Add proguard files to the extension projects relative to the project root.
32+
* Minification will be enabled if at least one file is provided.
33+
*
34+
* @param files The proguard files to add.
35+
*/
36+
fun proguardFiles(vararg files: String) {
37+
proguardFiles += files
38+
}
2239
}
2340
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package app.revanced.patches.gradle
2+
3+
import org.gradle.api.services.BuildService
4+
import org.gradle.api.services.BuildServiceParameters
5+
6+
abstract class SettingsExtensionProvider :
7+
BuildService<SettingsExtensionProvider.Params>,
8+
BuildServiceParameters {
9+
interface Params : BuildServiceParameters {
10+
var defaultNamespace: String?
11+
var proguardFiles: Set<String>
12+
}
13+
}

Diff for: src/main/kotlin/app/revanced/patches/gradle/SettingsPlugin.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ abstract class SettingsPlugin @Inject constructor(
1414
override fun apply(settings: Settings) {
1515
val extension = settings.extensions.create("settings", SettingsExtension::class.java)
1616

17+
settings.gradle.settingsEvaluated {
18+
settings.gradle.sharedServices.registerIfAbsent(
19+
"settingsExtensionProvider",
20+
SettingsExtensionProvider::class.java,
21+
) {
22+
it.parameters.apply {
23+
defaultNamespace = extension.extensions.defaultNamespace
24+
proguardFiles = extension.extensions.proguardFiles
25+
}
26+
}
27+
}
28+
1729
settings.configureDependencies()
1830
settings.configureProjects(extension)
1931
}
@@ -43,7 +55,7 @@ abstract class SettingsPlugin @Inject constructor(
4355
private fun Settings.configureProjects(extension: SettingsExtension) {
4456
// region Include the projects
4557

46-
val extensionsProjectPath = extension.extensions.projectPath ?: return
58+
val extensionsProjectPath = extension.extensions.projectsPath ?: return
4759

4860
objectFactory.fileTree().from(rootDir.resolve(extensionsProjectPath)).matching {
4961
it.include("**/build.gradle.kts")

0 commit comments

Comments
 (0)