mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-25 15:37:38 +00:00
5ffb9eaa5b
In the effort of preventing apps from crawling APK contents across the whole installed app list to detect Magisk Manager, the solution here is to NOT install the actual APK into the system, but instead dynamically load the full app at runtime by a stub app. The full APK will be stored in the application's private internal data where non-root processes cannot read or scan. The basis of this implementation is the class "AppComponentFactory" that is introduced in API 28. If assigned, the system framework will delegate app component instantiation to our custom implementation, which allows us to do all sorts of crazy stuffs, in our case dynamically load classes and create objects that does not exist in our APK. There are a few challenges to achieve our goal though. First, Java ClassLoaders follow the "delegation pattern", which means class loading resolution will first be delegated to the parent loader before we get a chance to do anything. This includes DexClassLoader, which is what we will be using to load DEX files at runtime. This is a problem because our stub app and full app share quite a lot of class names. A custom ClassLoader, DynamicClassLoader, is created to overcome this issue: it will always load classes in its current dex path before delegating it to the parent. Second, all app components (with the exception of runtime BroadcastReceivers) are required to be declared in AndroidManifest.xml. The full Magisk Manager has quite a lot of components (including those from WorkManager and Room). The solution is to copy the complete AndroidManifest.xml from the full app to the stub, and our AppComponentFactory is responsible to construct the proper objects or return dummy implementations in case the full APK isn't downloaded yet. Third, other than classes, all resources required to run the full app are also not bundled with the stub APK. We have to call an internal API `AssetManager.addAssetPath(String)` to add our downloaded full APK into AssetManager in order to access resources within our full app. That internal API has existed forever, and is whitelisted from restricted API access on modern Android versions, so it is pretty safe to use. Fourth, on the subject of resources, some resources are not just being used by our app at runtime. Resources such as the app icon, app label, launch theme, basically everything referred in AndroidManifest.xml, are used by the system to display the app properly. The system get these resources via resource IDs and direct loading from the installed APK. This subset of resources would have to be copied into the stub to make the app work properly. Fifth, resource IDs are used all over the place in XMLs and Java code. The resource IDs in the stub and full app cannot missmatch, or somewhere, either it be the system or AssetManager, will refer to the incorrect resource. The full app will have to include all resources in the stub, and all of them have to be assigned to the exact same IDs in both APKs. To achieve this, we use AAPT2's "--emit-ids" option to dump the resource ID mapping when building the stub, and "--stable-ids" when building the full APK to make sure all overlapping resources in full and stub are always assigned to the same ID. Finally, both stub and full app have to work properly independently. On 9.0+, the stub will have to first launch an Activity to download the full APK before it can relaunch into the full app. On pre-9.0, the stub should behave as it always did: download and prompt installation to upgrade itself to full Magisk Manager. In the full app, the goal is to introduce minimal intrusion to the code base to make sure this whole thing is maintainable in the future. Fortunately, the solution ends up pretty slick: all ContextWrappers in the app will be injected with custom Contexts. The custom Contexts will return our patched Resources object and the ClassLoader that loads itself, which will be DynamicClassLoader in the case of running as a delegate app. By directly patching the base Context of ContextWrappers (which covers tons of app components) and in the Koin DI, the effect propagates deep into every aspect of the code, making this change basically fully transparent to almost every piece of code in full Magisk Manager. After this commit, the stub app is able to properly download and launch the full app, with most basic functionalities working just fine. Do not expect Magisk Manager upgrades and hiding (repackaging) to work properly, and some other minor issues might pop up. This feature is still in the early WIP stages.
100 lines
3.2 KiB
Groovy
100 lines
3.2 KiB
Groovy
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
|
|
|
def props = new Properties()
|
|
def configPath = project.hasProperty('configPath') ?
|
|
new File(project.configPath) : rootProject.file('config.prop')
|
|
if (configPath.exists())
|
|
configPath.withInputStream { is -> props.load(is) }
|
|
|
|
buildscript {
|
|
ext.vKotlin = '1.3.50'
|
|
|
|
repositories {
|
|
google()
|
|
jcenter()
|
|
maven { url 'http://storage.googleapis.com/r8-releases/raw' }
|
|
maven { url 'https://kotlin.bintray.com/kotlinx' }
|
|
}
|
|
dependencies {
|
|
classpath 'com.android.tools.build:gradle:3.5.1'
|
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${vKotlin}"
|
|
|
|
|
|
// NOTE: Do not place your application dependencies here; they belong
|
|
// in the individual module build.gradle files
|
|
}
|
|
}
|
|
|
|
task clean(type: Delete) {
|
|
delete rootProject.buildDir
|
|
}
|
|
|
|
ext.props = props
|
|
|
|
subprojects {
|
|
repositories {
|
|
google()
|
|
jcenter()
|
|
maven { url "https://jitpack.io" }
|
|
maven { url "http://oss.sonatype.org/content/repositories/snapshots" }
|
|
}
|
|
afterEvaluate { module ->
|
|
if (getPlugins().hasPlugin('com.android.library') ||
|
|
getPlugins().hasPlugin('com.android.application')) {
|
|
android {
|
|
compileSdkVersion 29
|
|
buildToolsVersion '29.0.2'
|
|
|
|
defaultConfig {
|
|
if (minSdkVersion == null)
|
|
minSdkVersion 17
|
|
targetSdkVersion 28
|
|
}
|
|
|
|
compileOptions {
|
|
sourceCompatibility JavaVersion.VERSION_1_8
|
|
targetCompatibility JavaVersion.VERSION_1_8
|
|
}
|
|
}
|
|
}
|
|
|
|
if (module.name == 'app' || module.name == 'stub') {
|
|
android {
|
|
signingConfigs {
|
|
config {
|
|
storeFile rootProject.file('release-key.jks')
|
|
storePassword props['keyStorePass']
|
|
keyAlias props['keyAlias']
|
|
keyPassword props['keyPass']
|
|
}
|
|
}
|
|
|
|
buildTypes {
|
|
debug {
|
|
// If keystore exists, sign the APK with custom signature
|
|
if (signingConfigs.config.storeFile.exists())
|
|
signingConfig signingConfigs.config
|
|
}
|
|
release {
|
|
signingConfig signingConfigs.config
|
|
}
|
|
}
|
|
|
|
lintOptions {
|
|
disable 'MissingTranslation'
|
|
}
|
|
|
|
aaptOptions {
|
|
// Preserve stub resource IDs
|
|
File publicTxt = rootProject.file('stub-public.txt')
|
|
if (publicTxt.exists()) {
|
|
additionalParameters "--stable-ids", "${publicTxt.absolutePath}"
|
|
} else if (module.name == 'stub') {
|
|
additionalParameters "--emit-ids", "${publicTxt.absolutePath}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|