Skip to content

Commit

Permalink
Separate core into bridge and loader (LSPosed#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-TSNG authored Mar 17, 2022
1 parent 6b368b0 commit e0de4ca
Show file tree
Hide file tree
Showing 51 changed files with 828 additions and 636 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
with:
path: |
~/.ccache
core/build/.lto-cache
magisk-loader/build/.lto-cache
daemon/build/.lto-cache
key: native-cache-${{ github.sha }}
restore-keys: native-cache-
Expand Down Expand Up @@ -83,14 +83,14 @@ jobs:
if: success()
id: prepareArtifact
run: |
riruReleaseName=`ls core/release/LSPosed-v*-riru-release.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=riruReleaseName::$riruReleaseName"
riruDebugName=`ls core/release/LSPosed-v*-riru-debug.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=riruDebugName::$riruDebugName"
zygiskReleaseName=`ls core/release/LSPosed-v*-zygisk-release.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=zygiskReleaseName::$zygiskReleaseName"
zygiskDebugName=`ls core/release/LSPosed-v*-zygisk-debug.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=zygiskDebugName::$zygiskDebugName"
unzip core/release/LSPosed-v*-riru-release.zip -d LSPosed-riru-release
unzip core/release/LSPosed-v*-riru-debug.zip -d LSPosed-riru-debug
unzip core/release/LSPosed-v*-zygisk-release.zip -d LSPosed-zygisk-release
unzip core/release/LSPosed-v*-zygisk-debug.zip -d LSPosed-zygisk-debug
riruReleaseName=`ls magisk-loader/release/LSPosed-v*-riru-release.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=riruReleaseName::$riruReleaseName"
riruDebugName=`ls magisk-loader/release/LSPosed-v*-riru-debug.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=riruDebugName::$riruDebugName"
zygiskReleaseName=`ls magisk-loader/release/LSPosed-v*-zygisk-release.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=zygiskReleaseName::$zygiskReleaseName"
zygiskDebugName=`ls magisk-loader/release/LSPosed-v*-zygisk-debug.zip | awk -F '(/|.zip)' '{print $3}'` && echo "::set-output name=zygiskDebugName::$zygiskDebugName"
unzip magisk-loader/release/LSPosed-v*-riru-release.zip -d LSPosed-riru-release
unzip magisk-loader/release/LSPosed-v*-riru-debug.zip -d LSPosed-riru-debug
unzip magisk-loader/release/LSPosed-v*-zygisk-release.zip -d LSPosed-zygisk-release
unzip magisk-loader/release/LSPosed-v*-zygisk-debug.zip -d LSPosed-zygisk-debug
- name: Upload riru release
uses: actions/upload-artifact@v2
with:
Expand All @@ -116,14 +116,14 @@ jobs:
with:
name: mappings
path: |
core/build/outputs/mapping
magisk-loader/build/outputs/mapping
app/build/outputs/mapping
- name: Upload symbols
uses: actions/upload-artifact@v2
with:
name: symbols
path: |
core/build/symbols
magisk-loader/build/symbols
daemon/build/symbols
- name: Post to channel
if: ${{ github.event_name != 'pull_request' && success() && github.ref == 'refs/heads/master' }}
Expand All @@ -134,7 +134,7 @@ jobs:
COMMIT_URL: ${{ github.event.head_commit.url }}
run: |
if [ ! -z "${{ secrets.BOT_TOKEN }}" ]; then
OUTPUT="core/release/"
OUTPUT="magisk-loader/release/"
export riruRelease=$(find $OUTPUT -name "LSPosed-v*-riru-release.zip")
export riruDebug=$(find $OUTPUT -name "LSPosed-v*-riru-debug.zip")
export zygiskRelease=$(find $OUTPUT -name "LSPosed-v*-zygisk-release.zip")
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2021 LSPosed Contributors
* Copyright (C) 2021 - 2022 LSPosed Contributors
*/

import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.gradle.BaseExtension
Expand Down Expand Up @@ -77,6 +78,7 @@ fun Project.configureBaseExtension() {
externalNativeBuild {
cmake {
arguments += "-DEXTERNAL_ROOT=${File(rootDir.absolutePath, "external")}"
arguments += "-DCORE_ROOT=${File(rootDir.absolutePath, "core/src/main/jni")}"
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
val flags = arrayOf(
"-Wall",
Expand Down
2 changes: 0 additions & 2 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
/build
/release
/src/main/cpp/api/config.cpp
/.cxx
283 changes: 7 additions & 276 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,26 @@
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2021 LSPosed Contributors
* Copyright (C) 2021 - 2022 LSPosed Contributors
*/

import org.apache.commons.codec.binary.Hex
import org.apache.tools.ant.filters.FixCrLfFilter
import org.apache.tools.ant.filters.ReplaceTokens
import java.io.ByteArrayOutputStream
import java.security.MessageDigest
import java.util.*
val apiCode: Int by rootProject.extra

plugins {
id("com.android.application")
id("com.android.library")
}

val moduleName = "LSPosed"
val moduleBaseId = "lsposed"
val authors = "LSPosed Developers"

val riruModuleId = "lsposed"
val moduleMinRiruApiVersion = 25
val moduleMinRiruVersionName = "25.0.1"
val moduleMaxRiruApiVersion = 25

val injectedPackageName: String by rootProject.extra
val injectedPackageUid: Int by rootProject.extra

val defaultManagerPackageName: String by rootProject.extra
val apiCode: Int by rootProject.extra
val verCode: Int by rootProject.extra
val verName: String by rootProject.extra

android {
flavorDimensions += "api"
namespace = "org.lsposed.lspd.core"

buildFeatures {
prefab = true
androidResources = false
}

defaultConfig {
applicationId = "org.lsposed.lspd"
multiDexEnabled = false
consumerProguardFiles("proguard-rules.pro")

buildConfigField("int", "API_CODE", "$apiCode")
buildConfigField(
"String",
"DEFAULT_MANAGER_PACKAGE_NAME",
""""$defaultManagerPackageName""""
)
buildConfigField("String", "MANAGER_INJECTED_PKG_NAME", """"$injectedPackageName"""")
buildConfigField("int", "MANAGER_INJECTED_UID", """$injectedPackageUid""")
}

buildTypes {
Expand All @@ -72,253 +42,14 @@ android {
proguardFiles("proguard-rules.pro")
}
}

externalNativeBuild {
cmake {
path("src/main/jni/CMakeLists.txt")
}
}

productFlavors {
all {
externalNativeBuild {
cmake {
arguments += "-DMODULE_NAME=${name.toLowerCase()}_$moduleBaseId"
arguments += "-DAPI=${name.toLowerCase()}"
}
}
buildConfigField("String", "API", """"$name"""")
}

create("Riru") {
dimension = "api"
externalNativeBuild {
cmake {
arguments += "-DAPI_VERSION=$moduleMaxRiruApiVersion"
}
}
}

create("Zygisk") {
dimension = "api"
externalNativeBuild {
cmake {
arguments += "-DAPI_VERSION=1"
}
}
}
}

}


dependencies {
implementation("org.apache.commons:commons-lang3:3.12.0")
implementation("de.upb.cs.swt:axml:2.1.2")
compileOnly("androidx.annotation:annotation:1.3.0")
compileOnly(projects.hiddenapi.stubs)
implementation(projects.hiddenapi.bridge)
implementation(projects.services.managerService)
implementation(projects.services.daemonService)
implementation(projects.services.managerService)
}

val zipAll = task("zipAll") {

}

val apkDir: String
get() = if (rootProject.extra.properties["android.injected.invoked.from.ide"] == "true") "intermediates" else "outputs"

fun afterEval() = android.applicationVariants.forEach { variant ->
val variantCapped = variant.name.capitalize(Locale.ROOT)
val variantLowered = variant.name.toLowerCase(Locale.ROOT)
val buildTypeCapped = variant.buildType.name.capitalize(Locale.ROOT)
val buildTypeLowered = variant.buildType.name.toLowerCase(Locale.ROOT)
val flavorCapped = variant.flavorName!!.capitalize(Locale.ROOT)
val flavorLowered = variant.flavorName!!.toLowerCase(Locale.ROOT)

val magiskDir = "$buildDir/magisk/$variantLowered"

val moduleId = "${flavorLowered}_$moduleBaseId"
val zipFileName = "$moduleName-v$verName-$verCode-${flavorLowered}-$buildTypeLowered.zip"

val prepareMagiskFilesTask = task<Sync>("prepareMagiskFiles$variantCapped") {
dependsOn(
"assemble$variantCapped",
":app:package$buildTypeCapped",
":daemon:package$buildTypeCapped"
)
into(magiskDir)
from("${rootProject.projectDir}/README.md")
from("$projectDir/magisk_module") {
exclude("riru.sh", "module.prop", "customize.sh", "sepolicy.rule", "post-fs-data.sh")
}
from("$projectDir/magisk_module") {
include("module.prop")
expand(
"moduleId" to moduleId,
"versionName" to "v$verName",
"versionCode" to verCode,
"authorList" to authors,
"updateJson" to "https://lsposed.github.io/LSPosed/release/${flavorLowered}.json",
"requirement" to when (flavorLowered) {
"riru" -> "Requires Riru $moduleMinRiruVersionName or above installed"
"zygisk" -> "Requires Magisk 24.0+ and Zygisk enabled"
else -> "No further requirements"
},
"api" to flavorCapped
)
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
}
from("$projectDir/magisk_module") {
include("customize.sh", "post-fs-data.sh")
val tokens = mapOf("FLAVOR" to flavorLowered)
filter<ReplaceTokens>("tokens" to tokens)
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
}
if (flavorLowered == "riru") {
from("${projectDir}/magisk_module") {
include("riru.sh", "sepolicy.rule")
val tokens = mapOf(
"RIRU_MODULE_LIB_NAME" to "lspd",
"RIRU_MODULE_API_VERSION" to moduleMaxRiruApiVersion.toString(),
"RIRU_MODULE_MIN_API_VERSION" to moduleMinRiruApiVersion.toString(),
"RIRU_MODULE_MIN_RIRU_VERSION_NAME" to moduleMinRiruVersionName,
"RIRU_MODULE_DEBUG" to if (buildTypeLowered == "debug") "true" else "false",
)
filter<ReplaceTokens>("tokens" to tokens)
filter<FixCrLfFilter>("eol" to FixCrLfFilter.CrLf.newInstance("lf"))
}
}
from(project(":app").tasks.getByName("package$buildTypeCapped").outputs) {
include("*.apk")
rename(".*\\.apk", "manager.apk")
}
from(project(":daemon").tasks.getByName("package$buildTypeCapped").outputs) {
include("*.apk")
rename(".*\\.apk", "daemon.apk")
}
into("lib") {
from("${buildDir}/intermediates/cmake/$variantCapped/obj") {
include("**/liblspd.so")
}
from("${project(":daemon").buildDir}/intermediates/cmake/$buildTypeLowered/obj") {
include("**/libdaemon.so")
}
}
val dexOutPath = if (buildTypeLowered == "release")
"$buildDir/intermediates/dex/$variantCapped/minify${variantCapped}WithR8" else
"$buildDir/intermediates/dex/$variantCapped/mergeDex$variantCapped"
into("framework") {
from(dexOutPath)
rename("classes.dex", "lspd.dex")
}
doLast {
fileTree(magiskDir).visit {
if (isDirectory) return@visit
val md = MessageDigest.getInstance("SHA-256")
file.forEachBlock(4096) { bytes, size ->
md.update(bytes, 0, size)
}
file(file.path + ".sha256").writeText(Hex.encodeHexString(md.digest()))
}
}
}

val zipTask = task<Zip>("zip${variantCapped}") {
dependsOn(prepareMagiskFilesTask)
archiveFileName.set(zipFileName)
destinationDirectory.set(file("$projectDir/release"))
from(magiskDir)
}

zipAll.dependsOn(zipTask)

val adb: String = androidComponents.sdkComponents.adb.get().asFile.absolutePath
val pushTask = task<Exec>("push${variantCapped}") {
dependsOn(zipTask)
workingDir("${projectDir}/release")
commandLine(adb, "push", zipFileName, "/data/local/tmp/")
}
val flashTask = task<Exec>("flash${variantCapped}") {
dependsOn(pushTask)
commandLine(
adb, "shell", "su", "-c",
"magisk --install-module /data/local/tmp/${zipFileName}"
)
}
task<Exec>("flashAndReboot${variantCapped}") {
dependsOn(flashTask)
commandLine(adb, "shell", "reboot")
}
}

afterEvaluate {
afterEval()
}

val adb: String = androidComponents.sdkComponents.adb.get().asFile.absolutePath
val killLspd = task<Exec>("killLspd") {
commandLine(adb, "shell", "su", "-c", "killall", "lspd")
isIgnoreExitValue = true
}
val pushDaemon = task<Exec>("pushDaemon") {
dependsOn(":daemon:assembleDebug")
workingDir("${project(":daemon").buildDir}/$apkDir/apk/debug")
commandLine(adb, "push", "daemon-debug.apk", "/data/local/tmp/daemon.apk")
}
val pushDaemonNative = task<Exec>("pushDaemonNative") {
dependsOn(":daemon:assembleDebug")
doFirst {
val abi: String = ByteArrayOutputStream().use { outputStream ->
exec {
commandLine(adb, "shell", "getprop", "ro.product.cpu.abi")
standardOutput = outputStream
}
outputStream.toString().trim()
}
workingDir("${project(":daemon").buildDir}/intermediates/ndkBuild/debug/obj/local/$abi")
}
commandLine(adb, "push", "libdaemon.so", "/data/local/tmp/libdaemon.so")
}
val reRunDaemon = task<Exec>("reRunDaemon") {
dependsOn(pushDaemon, pushDaemonNative, killLspd)
// tricky to pass a minus number to avoid the injection warning
commandLine(
adb,
"shell",
"ASH_STANDALONE=1",
"su",
"-pc",
"/data/adb/magisk/busybox sh /data/adb/modules/*_lsposed/service.sh --system-server-max-retry=-1&"
)
isIgnoreExitValue = true
}
val tmpApk = "/data/local/tmp/lsp.apk"
val pushApk = task<Exec>("pushApk") {
dependsOn(":app:assembleDebug")
workingDir("${project(":app").buildDir}/$apkDir/apk/debug")
commandLine(adb, "push", "app-debug.apk", tmpApk)
}
val openApp = task<Exec>("openApp") {
commandLine(
adb,
"shell",
"am",
"start",
"-a",
"android.intent.action.MAIN",
"-c",
"org.lsposed.manager.LAUNCH_MANAGER",
"com.android.shell/.BugreportWarningActivity"
)
}
task<Exec>("reRunApp") {
dependsOn(pushApk)
commandLine(adb, "shell", "su", "-c", "mv -f $tmpApk /data/adb/lspd/manager.apk")
isIgnoreExitValue = true
finalizedBy(reRunDaemon)
}

evaluationDependsOn(":app")
evaluationDependsOn(":daemon")
Loading

0 comments on commit e0de4ca

Please sign in to comment.