├── app
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── cpp
│ │ ├── main.hpp
│ │ ├── logging.h
│ │ ├── logging.cpp
│ │ ├── CMakeLists.txt
│ │ ├── main.cpp
│ │ ├── base.hpp
│ │ ├── modules.cpp
│ │ ├── node.hpp
│ │ └── base.cpp
└── build.gradle.kts
├── gradle
├── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── libs.versions.toml
├── .gitignore
├── settings.gradle.kts
├── README.md
├── .github
└── workflows
│ └── build.yml
├── gradle.properties
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | /release
3 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/5ec1cff/MagicMountStandalone/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 | .cxx
10 | local.properties
11 |
--------------------------------------------------------------------------------
/gradle/libs.versions.toml:
--------------------------------------------------------------------------------
1 | [versions]
2 | agp = "8.7.0"
3 |
4 | [libraries]
5 | cxx = { module = "org.lsposed.libcxx:libcxx", version = "27.0.12077973" }
6 |
7 | [plugins]
8 | agp-lib = { id = "com.android.library", version.ref = "agp" }
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/cpp/main.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | std::string get_magisk_tmp();
7 |
8 | void handle_modules();
9 |
10 | void umount_modules(const char *magic);
11 |
12 | extern std::vector partitions;
13 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 23 20:36:09 CST 2024
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google()
4 | mavenCentral()
5 | gradlePluginPortal()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 |
16 | rootProject.name = "MagicMountStandalone"
17 | include(":app")
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Magic Mount Standalone
2 |
3 | The extracted version of [Magisk](https://github.com/topjohnwu/Magisk) Magic Mount
4 |
5 | ## Build
6 |
7 | ./gradlew zipDebug
8 | ./gradlew zipRelease
9 |
10 | Package will be placed at app/release, including executables and debug symbols
11 |
12 | ## Install for test
13 |
14 | ./gradlew installDebug
15 | ./gradlew installRelease
16 |
17 | Binaries will be push to /data/local/tmp/magic_mount
18 |
19 | ## Usage
20 |
21 | ```shell
22 | magic_mount [--work-dir dir] [--magic magic] [--add-partitions /p1,/p2,....]
23 |
24 | mount: do magic mount
25 | umount: umount all magic mounts
26 |
27 | magic: the name of the work dir
28 | work-dir: the path of the work dir
29 | add-partitions: add special partitions to mount
30 | ```
31 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | workflow_dispatch:
5 | push:
6 | branches: [ master ]
7 | tags: [ v* ]
8 | pull_request:
9 | merge_group:
10 |
11 | jobs:
12 | build:
13 | name: Build
14 | runs-on: ubuntu-latest
15 |
16 | steps:
17 | - name: Checkout
18 | uses: actions/checkout@v4
19 | with:
20 | submodules: "recursive"
21 | fetch-depth: 0
22 |
23 | - name: Setup Java
24 | uses: actions/setup-java@v4
25 | with:
26 | distribution: temurin
27 | java-version: 21
28 |
29 | - name: Setup Gradle
30 | uses: gradle/actions/setup-gradle@v4
31 |
32 | - name: Build with Gradle
33 | run: |
34 | ./gradlew zipDebug
35 | ./gradlew zipRelease
36 |
37 | - name: Upload artifacts
38 | uses: actions/upload-artifact@v4
39 | with:
40 | name: "artifacts"
41 | path: "./app/release"
42 | compression-level: 9
43 |
--------------------------------------------------------------------------------
/app/src/main/cpp/logging.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #ifndef LOG_TAG
9 | # define LOG_TAG "MagicMount"
10 | #endif
11 |
12 | #ifndef NDEBUG
13 | #define LOGD(...) logging::log(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
14 | #define LOGV(...) logging::log(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
15 | #else
16 | #define LOGD(...)
17 | #define LOGV(...)
18 | #endif
19 | #define LOGI(...) logging::log(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
20 | #define LOGW(...) logging::log(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
21 | #define LOGE(...) logging::log(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
22 | #define LOGF(...) logging::log(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__)
23 | #define PLOGE(fmt, args...) LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno))
24 |
25 | namespace logging {
26 | void setPrintEnabled(bool print);
27 |
28 | [[gnu::format(printf, 3, 4)]]
29 | void log(int prio, const char *tag, const char *fmt, ...);
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/cpp/logging.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | #include "logging.h"
7 |
8 | namespace logging {
9 | static bool use_print = false;
10 | static char prio_str[] = {
11 | 'V', 'D', 'I', 'W', 'E', 'F'
12 | };
13 |
14 | void setPrintEnabled(bool print) {
15 | use_print = print;
16 | }
17 |
18 | void log(int prio, const char *tag, const char *fmt, ...) {
19 | {
20 | va_list ap;
21 | va_start(ap, fmt);
22 | __android_log_vprint(prio, tag, fmt, ap);
23 | va_end(ap);
24 | }
25 | if (use_print) {
26 | char buf[BUFSIZ];
27 | va_list ap;
28 | va_start(ap, fmt);
29 | vsnprintf(buf, sizeof(buf), fmt, ap);
30 | va_end(ap);
31 | auto prio_char = (prio > ANDROID_LOG_DEFAULT && prio <= ANDROID_LOG_FATAL) ? prio_str[
32 | prio - ANDROID_LOG_VERBOSE] : '?';
33 | printf("[%c][%d:%d][%s]:%s\n", prio_char, getpid(), gettid(), tag, buf);
34 | }
35 | }
36 | }
--------------------------------------------------------------------------------
/app/src/main/cpp/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.22.1)
2 |
3 | project("magic_mount")
4 |
5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAGS}")
6 |
7 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
8 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
9 |
10 | find_package(cxx REQUIRED CONFIG)
11 | link_libraries(cxx::cxx)
12 |
13 | add_executable(${PROJECT_NAME} main.cpp modules.cpp base.cpp logging.cpp)
14 | target_link_libraries(${PROJECT_NAME} cxx::cxx log)
15 |
16 | if (DEFINED DEBUG_SYMBOLS_PATH)
17 | message(STATUS "Debug symbols will be placed at ${DEBUG_SYMBOLS_PATH}")
18 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
19 | COMMAND ${CMAKE_COMMAND} -E make_directory ${DEBUG_SYMBOLS_PATH}/${ANDROID_ABI}
20 | COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $
21 | ${DEBUG_SYMBOLS_PATH}/${ANDROID_ABI}/${PROJECT_NAME}.debug
22 | COMMAND ${CMAKE_STRIP} --strip-all $
23 | COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink ${DEBUG_SYMBOLS_PATH}/${ANDROID_ABI}/${PROJECT_NAME}.debug
24 | $)
25 | endif()
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. For more details, visit
12 | # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Enables namespacing of each library's R class so that its R class includes only the
19 | # resources declared in the library itself and none from the library's dependencies,
20 | # thereby reducing the size of the R class for that library
21 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/app/src/main/cpp/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "main.hpp"
4 | #include "logging.h"
5 |
6 | std::string tmp_path = "/debug_ramdisk";
7 |
8 | using namespace std::string_view_literals;
9 |
10 | std::vector partitions{"/vendor", "/product", "/system_ext"};
11 |
12 | void help() {
13 | LOGE("usage: magic_mount [--work-dir dir] [--magic magic] [--add-partitions /p1,/p2,....]");
14 | }
15 |
16 | int main(int argc, char **argv) {
17 | #ifndef NDEBUG
18 | logging::setPrintEnabled(true);
19 | #endif
20 |
21 | const char *magic = "magic";
22 |
23 | if (argc < 2) {
24 | help();
25 | return 1;
26 | }
27 |
28 | bool do_umount = false;
29 |
30 | if (argv[1] == "umount"sv) {
31 | do_umount = true;
32 | } else if (argv[1] != "mount"sv) {
33 | help();
34 | return 1;
35 | }
36 |
37 | for (int i = 2; i < argc; i++) {
38 | if (argv[i] == "--work-dir"sv && i + 1 < argc) {
39 | tmp_path = argv[i + 1];
40 | } else if (argv[i] == "--magic"sv && i + 1 < argc) {
41 | magic = argv[i + 1];
42 | } else if (argv[i] == "--add-partitions"sv && i + 1 < argc) {
43 | size_t pos = 0;
44 | std::string_view ps{argv[i + 1]};
45 | for (;;) {
46 | auto new_pos = ps.find(',', pos);
47 | if (new_pos != std::string_view::npos) {
48 | partitions.emplace_back(ps.substr(pos, new_pos - pos));
49 | pos = new_pos + 1;
50 | continue;
51 | }
52 | break;
53 | }
54 | partitions.emplace_back(ps.substr(pos));
55 | }
56 | }
57 |
58 | if (do_umount) {
59 | umount_modules(magic);
60 | return 0;
61 | }
62 |
63 | LOGI("magic_mount: work dir %s magic %s", tmp_path.c_str(), magic);
64 | for (auto &s: partitions) {
65 | LOGD("supported partitions: %s", s.c_str());
66 | }
67 |
68 | if (mount(magic, tmp_path.c_str(), "tmpfs", 0, nullptr) == -1) {
69 | PLOGE("mount tmp");
70 | return 1;
71 | }
72 | if (mount(nullptr, tmp_path.c_str(), nullptr, MS_PRIVATE, nullptr) == -1) {
73 | PLOGE("mount tmp private");
74 | return 1;
75 | }
76 | handle_modules();
77 | LOGI("mount done");
78 | if (mount(nullptr, tmp_path.c_str(), nullptr, MS_REMOUNT | MS_RDONLY, nullptr) == -1) {
79 | PLOGE("make ro");
80 | }
81 | if (umount2(tmp_path.c_str(), MNT_DETACH) == -1) {
82 | PLOGE("umount tmp");
83 | }
84 | return 0;
85 | }
86 |
87 | std::string get_magisk_tmp() {
88 | return tmp_path;
89 | }
90 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/cpp/base.hpp:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #include "logging.h"
15 |
16 | // https://github.com/topjohnwu/Magisk/blob/455b13b83c4dde60511e43a634c880317b1ba5fc/native/src/core/include/core.hpp#L31
17 | struct module_info {
18 | std::string name;
19 | };
20 |
21 | // https://github.com/topjohnwu/Magisk/blob/455b13b83c4dde60511e43a634c880317b1ba5fc/native/src/include/consts.hpp#L8
22 |
23 | #define SECURE_DIR "/data/adb"
24 | #define MODULEROOT SECURE_DIR "/modules"
25 |
26 | #define INTLROOT ".magisk"
27 | #define WORKERDIR INTLROOT "/worker"
28 | #define MODULEMNT INTLROOT "/modules"
29 |
30 | struct dirent *xreaddir(DIR *dirp);
31 |
32 | // files
33 |
34 | using sFILE = std::unique_ptr;
35 | using sDIR = std::unique_ptr;
36 |
37 | sDIR make_dir(DIR *dp);
38 |
39 | sFILE make_file(FILE *fp);
40 |
41 | static inline sDIR open_dir(const char *path) {
42 | return make_dir(opendir(path));
43 | }
44 |
45 | static inline sDIR xopen_dir(const char *path) {
46 | return make_dir(opendir(path));
47 | }
48 |
49 | static inline sDIR xopen_dir(int dirfd) {
50 | return make_dir(fdopendir(dirfd));
51 | }
52 |
53 | static inline sFILE open_file(const char *path, const char *mode) {
54 | return make_file(fopen(path, mode));
55 | }
56 |
57 | static inline sFILE xopen_file(const char *path, const char *mode) {
58 | return make_file(fopen(path, mode));
59 | }
60 |
61 | static inline sFILE xopen_file(int fd, const char *mode) {
62 | return make_file(fdopen(fd, mode));
63 | }
64 |
65 | #define DISALLOW_COPY_AND_MOVE(clazz) \
66 | clazz(const clazz&) = delete; \
67 | clazz(clazz &&) = delete;
68 |
69 | template
70 | class run_finally {
71 | DISALLOW_COPY_AND_MOVE(run_finally)
72 |
73 | public:
74 | explicit run_finally(Func &&fn) : fn(std::move(fn)) {}
75 |
76 | ~run_finally() { fn(); }
77 |
78 | private:
79 | Func fn;
80 | };
81 |
82 | void cp_afc(const char *src, const char *dest);
83 |
84 | void clone_attr(const char *src, const char *dest);
85 |
86 | int mkdirs(const char *path, mode_t mode);
87 | int xmkdirs(const char *path, mode_t mode);
88 |
89 | // mount scan
90 |
91 | struct mount_info {
92 | unsigned int id;
93 | unsigned int parent;
94 | dev_t device;
95 | std::string root;
96 | std::string target;
97 | std::string vfs_option;
98 | struct {
99 | unsigned int shared;
100 | unsigned int master;
101 | unsigned int propagate_from;
102 | } optional;
103 | std::string type;
104 | std::string source;
105 | std::string fs_option;
106 | };
107 |
108 | std::vector parse_mount_info(const char *pid);
109 |
110 | // https://github.com/topjohnwu/Magisk/blob/40aab136019f4c1950f0789baf92a0686cd0a29e/native/src/base/xwrap.cpp#L354
111 | // xwrap
112 |
113 | int xmount(const char *source, const char *target,
114 | const char *filesystemtype, unsigned long mountflags,
115 | const void *data);
116 |
117 | int xsymlink(const char *target, const char *linkpath);
118 | int xsymlinkat(const char *target, int newdirfd, const char *linkpath);
119 |
120 | ssize_t xreadlink(const char *pathname, char *buf, size_t bufsiz);
121 | ssize_t xreadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
122 |
123 | ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count);
124 |
125 | int xlstat(const char *pathname, struct stat *buf);
126 | int xfstat(int fd, struct stat *buf);
127 | int xmkdirat(int dirfd, const char *pathname, mode_t mode);
128 | int xmkdir(const char *pathname, mode_t mode);
129 |
130 | int xopen(const char *pathname, int flags);
131 | int xopen(const char *pathname, int flags, mode_t mode);
132 | int xopenat(int dirfd, const char *pathname, int flags);
133 | int xopenat(int dirfd, const char *pathname, int flags, mode_t mode);
134 |
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | import java.io.ByteArrayOutputStream
2 |
3 | plugins {
4 | alias(libs.plugins.agp.lib)
5 | }
6 |
7 | fun String.execute(currentWorkingDir: File = file("./")): String {
8 | val byteOut = ByteArrayOutputStream()
9 | project.exec {
10 | workingDir = currentWorkingDir
11 | commandLine = split("\\s".toRegex())
12 | standardOutput = byteOut
13 | }
14 | return String(byteOut.toByteArray()).trim()
15 | }
16 |
17 | val gitCommitCount = "git rev-list HEAD --count".execute().toInt()
18 | val gitCommitHash = "git rev-parse --verify --short HEAD".execute()
19 |
20 | val defaultCFlags = arrayOf(
21 | "-Wall",
22 | "-Wno-unused", "-Wno-unused-parameter", "-Wno-vla-cxx-extension",
23 | "-fno-rtti", "-fno-exceptions",
24 | "-fno-stack-protector", "-fomit-frame-pointer",
25 | "-Wno-builtin-macro-redefined", "-D__FILE__=__FILE_NAME__",
26 | )
27 |
28 | val releaseFlags = arrayOf(
29 | "-O3", "-flto",
30 | "-fvisibility=hidden", "-fvisibility-inlines-hidden",
31 | "-Wl,--exclude-libs,ALL", "-Wl,--gc-sections",
32 | )
33 |
34 | android {
35 | buildFeatures {
36 | androidResources = false
37 | buildConfig = false
38 | prefab = true
39 | prefabPublishing = true
40 | }
41 | externalNativeBuild.cmake {
42 | path("src/main/cpp/CMakeLists.txt")
43 | }
44 |
45 | defaultConfig {
46 | externalNativeBuild.cmake {
47 | arguments += "-DANDROID_STL=none"
48 | cFlags("-std=c18", *defaultCFlags)
49 | cppFlags("-std=c++20", *defaultCFlags)
50 | }
51 | }
52 |
53 | buildTypes {
54 | forEach {
55 | it.externalNativeBuild.cmake {
56 | arguments += "-DDEBUG_SYMBOLS_PATH=${layout.buildDirectory.dir("symbols/${it.name}").get().asFile.absolutePath}"
57 | }
58 | }
59 | }
60 |
61 | prefab {
62 | register("magic_mount")
63 | }
64 | }
65 |
66 | androidComponents.onVariants { variant ->
67 | val variantLowered = variant.name.lowercase()
68 | val variantCapped = variant.name.replaceFirstChar { it.uppercaseChar() }
69 | afterEvaluate {
70 | task("zip$variantCapped") {
71 | group = "my"
72 | archiveFileName.set("magic_mount-$gitCommitHash-$gitCommitCount-$variantLowered.zip")
73 | destinationDirectory.set(layout.projectDirectory.dir("release").asFile)
74 | dependsOn("externalNativeBuild$variantCapped")
75 |
76 | into("") {
77 | from(layout.buildDirectory.dir("intermediates/cmake/$variantLowered/obj"))
78 | from(layout.buildDirectory.dir("symbols/${variant.buildType}"))
79 | }
80 | }
81 |
82 | val executableName = "magic_mount"
83 |
84 | task("install$variantCapped") {
85 | group = "my"
86 | dependsOn("externalNativeBuild$variantCapped")
87 |
88 | val abiList = listOf("armeabi-v7a", "arm64-v8a", "x86_64", "x86")
89 | doLast {
90 | val primaryArch = "adb shell getprop ro.product.cpu.abi".execute()
91 | val arch = "adb shell getprop ro.product.cpu.abilist".execute()
92 | exec {
93 | commandLine = listOf(
94 | "adb", "shell", "rm /data/local/tmp/$executableName || su -c 'rm /data/local/tmp/$executableName'")
95 | isIgnoreExitValue = true
96 | }
97 | arch.split(",").forEach { abi ->
98 | if (abi !in abiList) {
99 | println("ignore unknown abi $abi")
100 | return@forEach
101 | }
102 | val isPrimary = primaryArch == abi
103 | val devPath =
104 | "/data/local/tmp/${if (isPrimary) executableName else "${executableName}_$abi"}"
105 | exec {
106 | workingDir =
107 | layout.buildDirectory.dir("intermediates/cmake/$variantLowered/obj/$abi").get().asFile
108 | commandLine =
109 | listOf(
110 | "adb",
111 | "push",
112 | executableName,
113 | devPath
114 | )
115 | }
116 | exec {
117 | commandLine = listOf(
118 | "adb",
119 | "shell",
120 | "chmod",
121 | "+x",
122 | devPath
123 | )
124 | }
125 | }
126 | }
127 | }
128 | }
129 | }
130 |
131 | dependencies {
132 | implementation(libs.cxx)
133 | }
134 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/cpp/modules.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include