Skip to content

Commit

Permalink
Ndk build (LSPosed#753)
Browse files Browse the repository at this point in the history
* Update dependencies

* Use prefab dobby

* NDK build

* Add ccache

* Fix library copy

* rm CMakeLists.txt
  • Loading branch information
vvb2060 authored Jun 20, 2021
1 parent 2b593be commit 74f93db
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 173 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
# Denote all files that are truly binary and should not be modified.
*.so binary
*.dex binary
*.jar binary
*.png binary
4 changes: 3 additions & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
key: gradle-builds-core-${{ github.sha }}
restore-keys: |
gradle-builds
- name: Cache Cmake
- name: Cache Ccache
uses: actions/cache@v2
with:
path: ~/.ccache
Expand All @@ -64,6 +64,8 @@ jobs:
ccache -o max_size=2G
ccache -o hash_dir=false
- name: Build with Gradle
env:
NDK_CCACHE: ccache
run: |
[ $(du -s ~/.gradle/wrapper | awk '{ print $1 }') -gt 250000 ] && rm -rf ~/.gradle/wrapper/* || true
find ~/.gradle/caches -exec touch -d "2 days ago" {} + || true
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
[submodule "core/src/main/cpp/external/DexBuilder"]
path = core/src/main/cpp/external/DexBuilder
url = https://github.com/LSPosed/DexBuilder.git
[submodule "core/src/main/cpp/external/Dobby"]
path = core/src/main/cpp/external/Dobby
url = https://github.com/LSPosed/Dobby.git
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ plugins {
id("org.gradle.idea")
id("com.android.application")
id("androidx.navigation.safeargs")
kotlin("android")
}

// workaround for AS.
Expand Down Expand Up @@ -210,7 +209,7 @@ dependencies {
implementation("androidx.fragment:fragment:1.3.4")
implementation("androidx.navigation:navigation-fragment:$navVersion")
implementation("androidx.navigation:navigation-ui:$navVersion")
implementation("androidx.recyclerview:recyclerview:1.2.0")
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation("com.caverock:androidsvg-aar:1.4")
implementation("com.github.bumptech.glide:glide:$glideVersion")
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ buildscript {
classpath("com.android.tools.build:gradle:7.0.0-beta04")
classpath("org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5")
classpath(kotlin("gradle-plugin", version = "1.4.32"))
}
}

Expand Down
1 change: 1 addition & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/release
*.iml
/.cxx
src/main/cpp/main/src/config.cpp
89 changes: 18 additions & 71 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* Copyright (C) 2021 LSPosed Contributors
*/

import com.android.build.api.variant.impl.ApplicationVariantImpl
import com.android.build.api.component.analytics.AnalyticsEnabledApplicationVariant
import com.android.build.api.variant.impl.ApplicationVariantImpl
import com.android.build.gradle.BaseExtension
import com.android.ide.common.signing.KeystoreHelper
import org.apache.tools.ant.filters.FixCrLfFilter
Expand All @@ -29,7 +29,6 @@ import java.security.MessageDigest

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

val moduleName = "LSPosed"
Expand Down Expand Up @@ -59,6 +58,7 @@ val verName: String by rootProject.extra
dependencies {
implementation("dev.rikka.ndk:riru:${moduleMinRiruVersionName}")
implementation("dev.rikka.ndk.thirdparty:cxx:1.1.0")
implementation("io.github.vvb2060.ndk:dobby:1.0")
implementation("com.android.tools.build:apksig:7.0.0-beta03")
implementation("org.apache.commons:commons-lang3:3.12.0")
implementation("de.upb.cs.swt:axml:2.1.1")
Expand Down Expand Up @@ -87,29 +87,9 @@ android {
multiDexEnabled = false

externalNativeBuild {
cmake {
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
val flags = arrayOf(
"-ffixed-x18",
"-Qunused-arguments",
"-fno-rtti", "-fno-exceptions",
"-fno-stack-protector",
"-fomit-frame-pointer",
"-Wno-builtin-macro-redefined",
"-Wl,--exclude-libs,ALL",
"-D__FILE__=__FILE_NAME__",
"-DRIRU_MODULE",
"-DRIRU_MODULE_API_VERSION=$moduleMaxRiruApiVersion",
"""-DMODULE_NAME=\"$riruModuleId\"""",
)
cppFlags("-std=c++20", *flags)
cFlags("-std=c18", *flags)
arguments(
"-DANDROID_STL=none",
"-DVERSION_CODE=$verCode",
"-DVERSION_NAME=$verName",
)
targets("lspd")
ndkBuild {
arguments += "RIRU_MODULE_API_VERSION=$moduleMaxRiruApiVersion"
arguments += "MODULE_NAME=$riruModuleId"
}
}

Expand All @@ -123,56 +103,14 @@ android {
}

buildTypes {
debug {
externalNativeBuild {
cmake {
arguments.addAll(
arrayOf(
"-DCMAKE_CXX_FLAGS_DEBUG=-Og",
"-DCMAKE_C_FLAGS_DEBUG=-Og"
)
)
}
}
}
release {
isMinifyEnabled = true
proguardFiles("proguard-rules.pro")

externalNativeBuild {
cmake {
val flags = arrayOf(
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
"-Wno-unused-value",
"-ffunction-sections",
"-fdata-sections",
"-Wl,--gc-sections",
"-Wl,--strip-all",
"-fno-unwind-tables",
"-fno-asynchronous-unwind-tables"
)
cppFlags.addAll(flags)
cFlags.addAll(flags)
val configFlags = arrayOf(
"-Oz",
"-DNDEBUG"
).joinToString(" ")
arguments.addAll(
arrayOf(
"-DCMAKE_CXX_FLAGS_RELEASE=$configFlags",
"-DCMAKE_CXX_FLAGS_RELWITHDEBINFO=$configFlags",
"-DCMAKE_C_FLAGS_RELEASE=$configFlags",
"-DCMAKE_C_FLAGS_RELWITHDEBINFO=$configFlags"
)
)
}
}
}
}
externalNativeBuild {
cmake {
path("src/main/cpp/CMakeLists.txt")
ndkBuild {
path("src/main/cpp/Android.mk")
}
}

Expand Down Expand Up @@ -278,8 +216,7 @@ androidComponents.onVariants { v ->
rename(".*\\.apk", "manager.apk")
}
into("lib") {
from("${buildDir}/intermediates/cmake/$variantLowered/obj")
exclude("**/*.txt")
from("${buildDir}/intermediates/stripped_native_libs/$variantLowered/out/lib")
}
val dexOutPath = if (variantLowered == "release")
"$buildDir/intermediates/dex/$variantLowered/minify${variantCapped}WithR8" else
Expand Down Expand Up @@ -342,3 +279,13 @@ androidComponents.onVariants { v ->
}
}
}

val generateVersion = task("generateVersion", Copy::class) {
inputs.property("VERSION_CODE", verCode)
inputs.property("VERSION_NAME", verName)
from("${projectDir}/src/main/cpp/main/template")
include("config.cpp")
expand("VERSION_CODE" to verCode, "VERSION_NAME" to verName)
into("${projectDir}/src/main/cpp/main/src")
}
tasks.getByName("preBuild").dependsOn(generateVersion)
7 changes: 7 additions & 0 deletions core/src/main/cpp/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include src/main/cpp/external/DexBuilder/Android.mk
include src/main/cpp/external/yahfa/Android.mk
include src/main/cpp/main/Android.mk

$(call import-module,prefab/cxx)
$(call import-module,prefab/riru)
$(call import-module,prefab/dobby)
14 changes: 14 additions & 0 deletions core/src/main/cpp/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
APP_CFLAGS := -Wall -Wextra
APP_CFLAGS += -fno-stack-protector -fomit-frame-pointer
APP_CFLAGS += -Wno-builtin-macro-redefined -D__FILE__=__FILE_NAME__
APP_CPPFLAGS := -std=c++20
APP_CONLYFLAGS := -std=c18
APP_LDFLAGS := -Wl,--exclude-libs,ALL
APP_STL := none

ifneq ($(NDK_DEBUG),1)
APP_CFLAGS += -Oz -Wno-unused -Wno-unused-parameter -Werror
APP_CFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
APP_CFLAGS += -fno-unwind-tables -fno-asynchronous-unwind-tables
APP_LDFLAGS += -Wl,--gc-sections -Wl,--strip-all
endif
35 changes: 0 additions & 35 deletions core/src/main/cpp/CMakeLists.txt

This file was deleted.

17 changes: 0 additions & 17 deletions core/src/main/cpp/external/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/main/cpp/external/DexBuilder
Submodule DexBuilder updated 2 files
+12 −0 Android.mk
+3 −3 slicer/writer.h
1 change: 0 additions & 1 deletion core/src/main/cpp/external/Dobby
Submodule Dobby deleted from 2da2c2
10 changes: 10 additions & 0 deletions core/src/main/cpp/external/yahfa/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := yahfa
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := src/HookMain.cpp src/trampoline.c
LOCAL_EXPORT_LDLIBS := -llog
LOCAL_STATIC_LIBRARIES := libcxx
include $(BUILD_STATIC_LIBRARY)
15 changes: 15 additions & 0 deletions core/src/main/cpp/main/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LOCAL_PATH := $(call my-dir)
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef

include $(CLEAR_VARS)
LOCAL_MODULE := lspd
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/src
FILE_LIST := $(filter %.cpp, $(call walk, $(LOCAL_PATH)/src))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_STATIC_LIBRARIES := cxx yahfa riru dobby dex_builder
LOCAL_CFLAGS := -DRIRU_MODULE -DRIRU_MODULE_API_VERSION=${RIRU_MODULE_API_VERSION}
LOCAL_CFLAGS += -DMODULE_NAME=${MODULE_NAME}
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
36 changes: 0 additions & 36 deletions core/src/main/cpp/main/CMakeLists.txt

This file was deleted.

3 changes: 1 addition & 2 deletions core/src/main/cpp/main/src/elf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ ElfW(Addr) ElfImg::LinearLookup(std::string_view name) const {
if (symtabs_.empty()) {
symtabs_.reserve(symtab_count);
if (symtab_start != nullptr && symstr_offset_for_symtab != 0) {
for (int i = 0; i < symtab_count; i++) {
for (ElfW(Off) i = 0; i < symtab_count; i++) {
unsigned int st_type = ELF_ST_TYPE(symtab_start[i].st_info);
const char *st_name = offsetOf<const char *>(header, symstr_offset_for_symtab +
symtab_start[i].st_name);
Expand Down Expand Up @@ -260,4 +260,3 @@ bool ElfImg::findModuleBase() {
base = reinterpret_cast<void *>(load_addr);
return true;
}

2 changes: 1 addition & 1 deletion core/src/main/cpp/main/src/jni/resources_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace lspd {
env, class_clazz, "accessFlags", "I");
jint access_flags = env->GetIntField(target_class, java_lang_Class_accessFlags);
env->SetIntField(target_class, java_lang_Class_accessFlags, access_flags & ~kAccFinal);
for (auto i = 0u; i < env->GetArrayLength(constructors); ++i) {
for (auto i = 0; i < env->GetArrayLength(constructors); ++i) {
auto constructor = env->GetObjectArrayElement(constructors, i);
void *method = yahfa::getArtMethod(env, constructor);
uint32_t flags = yahfa::getAccessFlags(method);
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/cpp/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ namespace lspd {
};
}

#define str(s) #s

RIRU_EXPORT RiruVersionedModuleInfo *init(Riru *riru) {
LOGD("using riru %d", riru->riruApiVersion);
LOGD("module path: %s", riru->magiskModulePath);
lspd::magiskPath = riru->magiskModulePath;
if (!lspd::isDebug && lspd::magiskPath.find(MODULE_NAME) == std::string::npos) {
if (!lspd::isDebug && lspd::magiskPath.find(str(MODULE_NAME)) == std::string::npos) {
LOGE("who am i");
return nullptr;
}
Expand Down
1 change: 0 additions & 1 deletion manager-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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

val androidTargetSdkVersion: Int by rootProject.extra
Expand Down

0 comments on commit 74f93db

Please sign in to comment.