Skip to content

Commit

Permalink
Refactor: drop kotlin (#3)
Browse files Browse the repository at this point in the history
* Refactor: use java instead

* Chore: use LinkedHashMap

* Fix: fix checksum

* Fix: add gitattributes

* Fix: update .gitattributes

* Fix: fix scripts rendering

* Fix: fix crlf

* Fix: gradle scripts

* Chore: update README.md

* Fix: fix shell scripts
  • Loading branch information
Kr328 authored Nov 22, 2022
1 parent 08eba06 commit 6170c47
Show file tree
Hide file tree
Showing 30 changed files with 758 additions and 709 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto eol=lf
*.bat text eol=crlf
*.jar binary
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Entrypoint {
```kotlin
plugins {
id("com.android.application") // required
id("com.github.kr328.gradle.zygote") version "2.5" // apply plugin
id("com.github.kr328.gradle.zygote") version "2.7" // apply plugin
// ... other plugins
}
```
Expand Down
13 changes: 1 addition & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.20" apply false
id("com.android.library") version "7.3.1" apply false
}

subprojects {
group = "com.github.kr328.gradle.zygote"
version = "2.6"

plugins.withId("org.jetbrains.kotlin.jvm") {
tasks.withType(KotlinCompile::class) {
kotlinOptions {
jvmTarget = "11"
}
}
}
version = "2.7"

plugins.withId("java") {
extensions.configure<JavaPluginExtension> {
Expand Down
5 changes: 1 addition & 4 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
java
`java-gradle-plugin`
`maven-publish`
}
Expand Down Expand Up @@ -38,7 +36,6 @@ task("generateDynamicSources") {
inputs.property("moduleVersion", project.version)
outputs.dir(dynamicSources)
tasks.withType(JavaCompile::class.java).forEach { it.dependsOn(this) }
tasks.withType(KotlinCompile::class.java).forEach { it.dependsOn(this) }
tasks["sourcesJar"].dependsOn(this)

doFirst {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package com.github.kr328.gradle.zygote;

import com.android.build.api.variant.ApplicationVariant;
import com.android.build.api.variant.VariantOutput;
import com.github.kr328.gradle.zygote.tasks.ChecksumTask;
import com.github.kr328.gradle.zygote.tasks.CustomizeTask;
import com.github.kr328.gradle.zygote.tasks.PackagesTask;
import com.github.kr328.gradle.zygote.tasks.PropertiesTask;
import com.github.kr328.gradle.zygote.util.StringUtils;

import org.apache.tools.ant.filters.FixCrLfFilter;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Sync;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Zip;
import org.gradle.api.tasks.bundling.ZipEntryCompression;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import javax.annotation.Nullable;

public final class ZygoteLoaderDecorator {
private final Project project;
private final ZygoteLoaderExtension extension;

public ZygoteLoaderDecorator(Project project, ZygoteLoaderExtension extension) {
this.project = project;
this.extension = extension;
}

public void decorateVariant(final Loader loader, final ApplicationVariant variant) {
final TaskProvider<Task> pack = project.getTasks().named("package" + StringUtils.capitalize(variant.getName()));
final VariantOutput variantOutput = variant.getOutputs().stream().findAny().orElseThrow();

final Map<String, String> moduleProp = new LinkedHashMap<>();

moduleProp.put("version", variantOutput.getVersionName().getOrElse(""));
moduleProp.put("versionCode", variantOutput.getVersionCode().getOrElse(0).toString());
moduleProp.put("minSdkVersion", String.valueOf(variant.getMinSdkVersion().getApiLevel()));
if (variant.getMaxSdkVersion() != null) {
moduleProp.put("maxSdkVersion", String.valueOf(variant.getMaxSdkVersion()));
}

if (loader == Loader.Riru) {
moduleProp.putAll(extension.getRiru());
} else if (loader == Loader.Zygisk) {
moduleProp.putAll(extension.getZygisk());
}

final TaskProvider<PropertiesTask> generateModuleProp = project.getTasks().register(
"generateModuleProp" + StringUtils.capitalize(variant.getName()),
PropertiesTask.class,
task -> {
task.getDestinationFile().set(
project.getLayout().getBuildDirectory()
.dir("generated/properties/" + variant.getName())
.map(p -> p.file("module.prop"))
);
task.getProperties().set(moduleProp);
}
);

final TaskProvider<PackagesTask> generateInitialPackages = project.getTasks().register(
"generateInitialPackages" + StringUtils.capitalize(variant.getName()),
PackagesTask.class,
task -> {
task.getDestinationDirectory().set(
project.getLayout().getBuildDirectory()
.dir("generated/packages/" + variant.getName())
);
task.getPackages().set(extension.getPackages());
}
);

final TaskProvider<Sync> mergeMagisk = project.getTasks().register(
"mergeMagisk" + StringUtils.capitalize(variant.getName()),
Sync.class,
task -> {
task.setDestinationDir(new File(project.getBuildDir(), "intermediates/merged_magisk/" + variant.getName()));

final Provider<File> apkFile = pack.map(p ->
p.getOutputs().getFiles().getFiles().stream()
.flatMap(f -> {
if (f.isDirectory()) {
return Stream.of(Objects.requireNonNull(f.listFiles()));
} else {
return Stream.of(f);
}
})
.filter(f -> f.getName().endsWith(".apk"))
.findAny()
.orElseThrow()
);

final Provider<FileCollection> apk = apkFile.map(project::zipTree);

task.getInputs().file(apkFile);

// module prop
task.from(generateModuleProp);

// initial packages
task.from(generateInitialPackages, sp -> sp.into("packages"));

// classes.dex
task.from(apk, sp -> sp.include("classes.dex"));

// native libraries
task.from(apk, sp -> {
sp.include("lib/*/*.so");
sp.eachFile(file -> {
final String abi = file.getPath().split("/")[1];

if (loader == Loader.Riru) {
file.setPath("riru/" + abi + "/lib" + moduleProp.get("id") + ".so");
} else if (loader == Loader.Zygisk) {
file.setPath("zygisk/" + abi + ".so");
}
});
});

// assets
task.from(apk, sp -> {
sp.include("assets/**");
sp.eachFile(file -> file.setPath(file.getPath().substring("assets/".length())));
});
}
);

final TaskProvider<ChecksumTask> generateChecksum = project.getTasks().register(
"generateChecksum" + StringUtils.capitalize(variant.getName()),
ChecksumTask.class,
task -> {
task.getDestinationFile().set(
project.getLayout().getBuildDirectory()
.dir("generated/checksum/" + variant.getName())
.map(p -> p.file("00-verify-resources.sh"))
);
task.getRootDirectory().set(mergeMagisk.map(m ->
project.getLayout().getProjectDirectory()
.dir(m.getDestinationDir().getAbsolutePath()))
);
}
);

final TaskProvider<CustomizeTask> generateCustomize = project.getTasks().register(
"generateCustomize" + StringUtils.capitalize(variant.getName()),
CustomizeTask.class,
task -> {
task.getDestinationFile().set(
project.getLayout().getBuildDirectory()
.dir("generated/customize/" + variant.getName())
.map(p -> p.file("customize.sh"))
);
task.getMergedDirectory().set(mergeMagisk.map(m ->
project.getLayout().getProjectDirectory()
.dir(m.getDestinationDir().getAbsolutePath()))
);
task.getChecksumFileName().set(generateChecksum.map(c -> c.getDestinationFile().get().getAsFile().getName()));
}
);

final TaskProvider<Zip> zipMagisk = project.getTasks().register(
"zipMagisk" + StringUtils.capitalize(variant.getName()),
Zip.class,
zip -> {
zip.getDestinationDirectory().set(
project.getLayout().getBuildDirectory()
.dir("outputs/magisk/" + variant.getFlavorName() + "/" + variant.getBuildType())
);

zip.getArchiveBaseName().set(project.getName());
if (loader == Loader.Riru && extension.getRiru().containsKey("archiveName")) {
zip.getArchiveBaseName().set(extension.getRiru().get("archiveName"));
} else if (loader == Loader.Zygisk && extension.getZygisk().containsKey("archiveName")) {
zip.getArchiveBaseName().set(extension.getZygisk().get("archiveName"));
}

zip.setIncludeEmptyDirs(false);
zip.setEntryCompression(ZipEntryCompression.DEFLATED);
zip.setPreserveFileTimestamps(false);

zip.from(mergeMagisk);
zip.from(generateChecksum, sp -> sp.into("customize.d"));
zip.from(generateCustomize);
}
);

project.getTasks().named(
"assemble" + StringUtils.capitalize(variant.getName()),
t -> t.dependsOn(zipMagisk)
);
}

enum Loader {
Riru("riru"), Zygisk("zygisk");

final String flavorName;

Loader(String flavorName) {
this.flavorName = flavorName;
}

@Nullable
public static Loader fromFlavorName(final String flavorName) {
return Arrays.stream(Loader.values())
.filter(l -> l.flavorName.equals(flavorName))
.findFirst().orElse(null);
}
}
}
Loading

0 comments on commit 6170c47

Please sign in to comment.