Skip to content

Commit

Permalink
Fix for android 11 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed May 30, 2021
1 parent e7ec749 commit ab39f4b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 36 deletions.
6 changes: 6 additions & 0 deletions XamarinPosed.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XamarinPosed", "XamarinPose
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Posed.Demo", "Xamarin.Posed.Demo\Xamarin.Posed.Demo.csproj", "{2C0F056B-77B6-4135-9637-06CE89355226}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F6CCFE68-4609-44AB-930E-7D5B4C27D5CC}"
ProjectSection(SolutionItems) = preProject
java\UlyssesFullLoader.java = java\UlyssesFullLoader.java
java\UlyssesLoader.java = java\UlyssesLoader.java
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
2 changes: 2 additions & 0 deletions XamarinPosed/Loader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using Android.Util;
using Android.Views;
using Android.Widget;
using DE.Robv.Android.Xposed;
Expand Down Expand Up @@ -35,6 +36,7 @@ public Loader(string baseApkPath, string packageName)
public void HandleLoadPackage(XC_LoadPackage.LoadPackageParam param)
{
DetectAndFixXamarinApp(param); //This is required for Xamarin app compatibility
Log.Info("XamarinPosed", "XamarinPosed HandleLoadPackage: " + param.PackageName);
XposedBridge.Log("XamarinPosed HandleLoadPackage: " + param.PackageName);
//This is a demo, remove it
HookMyself(param);
Expand Down
2 changes: 2 additions & 0 deletions XamarinPosed/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="28" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
1 change: 1 addition & 0 deletions XamarinPosed/XamarinPosed.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
JavaCodePostProcess;
</AfterGenerateAndroidManifest>
<PostProcessArgProjectDir>"$(ProjectDir)"</PostProcessArgProjectDir>
<AndroidSupportedAbis>armeabi-v7a;x86;arm64-v8a</AndroidSupportedAbis>
</PropertyGroup>
<Target Name="JavaCodePostProcess">
<Exec Command="&quot;$(SolutionDir)JavaCodePostProcessor\bin\$(ConfigurationName)\net5.0\JavaCodePostProcessor.exe&quot; $(ConfigurationName) $(PostProcessArgProjectDir) $(DefineConstants)" ConsoleToMSBuild="true">
Expand Down
74 changes: 56 additions & 18 deletions java/UlyssesFullLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,43 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
Locale locale = Locale.getDefault();
String localeStr = locale.getLanguage() + "-" + locale.getCountry();

String f1 = modulePath.replaceFirst("/app/", "/user/0/");
String filesDir = f1.substring(0, f1.lastIndexOf("/"));
Path currentModulePath = Paths.get(modulePath);
String parent = currentModulePath.getParent().getFileName().toString();
String packageName = parent;
int subPos = parent.lastIndexOf("-");
if (subPos > 0)
{
packageName = parent.substring(0, subPos);
}
Log.i("XamarinPosed", "packageName: " + packageName);

File externalStorageDirectory = Environment.getExternalStorageDirectory();
File filesDirFile = new File(externalStorageDirectory, "Android/data/" + packageName + "/files");
File cachesDirFile = new File(externalStorageDirectory, "Android/data/" + packageName + "/files/cache");
cachesDirFile.mkdirs();
String filesDir = filesDirFile.getAbsolutePath();
Log.i("XamarinPosed", "filesDir: " + filesDir);
//String filesDir = context.getFilesDir().getAbsolutePath(); // /data/user/0/io.va.xposed/virtual/data/user/0/{package}/
String packageName = filesDir.substring(filesDir.lastIndexOf("/") + 1);

String cacheDir = filesDir + "/" + "cache";
String cacheDir = cachesDirFile.getAbsolutePath();
//String cacheDir = context.getCacheDir().getAbsolutePath(); // filesDir + "cache"
String dataAppDir = modulePath.substring(0, modulePath.lastIndexOf("/"));
String nativeLibraryPath = dataAppDir + "/lib"; // getNativeLibraryPath(context);
//tring nativeLibraryPath = getNativeLibraryPath(context); //{baseApkDir}/../lib

File nativeLibraryPathFile = new File(nativeLibraryPath);
for (File f : nativeLibraryPathFile.listFiles())
{
if(f.isDirectory())
{
nativeLibraryPath = f.getAbsolutePath();
break;
}
}

ClassLoader classLoader = this.getClass().getClassLoader();
//TODO: hook context.getClassLoader() and replaced to this classLoader
//ClassLoader classLoader = de.robv.android.xposed.XposedBridge.BOOTCLASSLOADER;
File externalStorageDirectory = Environment.getExternalStorageDirectory();

String externalOverrridePath = new File(externalStorageDirectory, "Android/data/" + packageName + "/files/.__override__").getAbsolutePath();
String externalOverrridePathLegacy = new File(externalStorageDirectory, "../legacy/Android/data/" + packageName + "/files/.__override__").getAbsolutePath();

Expand All @@ -74,31 +95,48 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
String[] initParams = {filesDir, cacheDir, nativeLibraryPath};
String[] externalOverrrideParams = {externalOverrridePath, externalOverrridePathLegacy}; //deprecated

if (BuildConfig.Debug)
Log.i("XamarinPosed", "nativeLibraryPath: " + nativeLibraryPath2);
String nativeLibraryPath3 = nativeLibraryPath + "/";
try
{
System.loadLibrary("xamarin-debug-app-helper");
//DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams, externalOverrrideParams);
DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams);
}
else
if (BuildConfig.Debug)
{
System.load(nativeLibraryPath3 + "libxamarin-debug-app-helper.so");
//DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams, externalOverrrideParams);
DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams);
}
else
{
System.load(nativeLibraryPath3 + "libmonosgen-2.0.so");
}
}
catch (UnsatisfiedLinkError e)
{
System.loadLibrary("monosgen-2.0");
Log.e("XamarinPosed", "Failed to load mono lib, could be architecture mismatch (64bit module vs 32bit app or vice versa)", e);
isInited = false;
return;
}
System.loadLibrary("xamarin-app");

System.load(nativeLibraryPath3 + "libxamarin-app.so");
try
{
System.loadLibrary("mono-native");
System.load(nativeLibraryPath3 + "libmono-native.so");
}
catch (UnsatisfiedLinkError e)
{
Log.i("monodroid", "Failed to preload libmono-native.so (may not exist), ignoring", e);
}

System.loadLibrary("monodroid");
System.load(nativeLibraryPath3 + "libmonodroid.so");
System.load(nativeLibraryPath3 + "libmono-btls-shared.so");
System.load(nativeLibraryPath3 + "libxa-internal-api.so");
Log.i("XamarinPosed", "load lib done");
//Runtime.initInternal(localeStr, sourceDirs, nativeLibraryPath2, initParams, classLoader, externalOverrrideParams, MonoPackageManager_Resources.Assemblies, Build.VERSION.SDK_INT, isEmulator());
Runtime.initInternal(localeStr, sourceDirs, nativeLibraryPath2, initParams, classLoader, MonoPackageManager_Resources.Assemblies, Build.VERSION.SDK_INT, isEmulator());
ApplicationRegistration.registerApplications();

Log.i("XamarinPosed", "init internal done");

// /data/data/com.my.app/files
_loader = new xamarin.posed.Main_Loader();
//_loader = new xamarin.posed.Main_Loader(modulePath, packageName);
isInited = true;
Expand All @@ -107,7 +145,7 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
if (isInited && _loader != null)
{
_loader.initZygote (p0);
}
}
}

public void handleInitPackageResources (de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam p0)
Expand Down
75 changes: 57 additions & 18 deletions java/UlyssesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import java.util.Locale;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import mono.android.BuildConfig;
import mono.android.DebugRuntime;
import mono.android.Runtime;
Expand Down Expand Up @@ -47,22 +49,43 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
Locale locale = Locale.getDefault();
String localeStr = locale.getLanguage() + "-" + locale.getCountry();

String f1 = modulePath.replaceFirst("/app/", "/user/0/");
String filesDir = f1.substring(0, f1.lastIndexOf("/"));
Path currentModulePath = Paths.get(modulePath);
String parent = currentModulePath.getParent().getFileName().toString();
String packageName = parent;
int subPos = parent.lastIndexOf("-");
if (subPos > 0)
{
packageName = parent.substring(0, subPos);
}
Log.i("XamarinPosed", "packageName: " + packageName);

File externalStorageDirectory = Environment.getExternalStorageDirectory();
File filesDirFile = new File(externalStorageDirectory, "Android/data/" + packageName + "/files");
File cachesDirFile = new File(externalStorageDirectory, "Android/data/" + packageName + "/files/cache");
cachesDirFile.mkdirs();
String filesDir = filesDirFile.getAbsolutePath();
Log.i("XamarinPosed", "filesDir: " + filesDir);
//String filesDir = context.getFilesDir().getAbsolutePath(); // /data/user/0/io.va.xposed/virtual/data/user/0/{package}/
String packageName = filesDir.substring(filesDir.lastIndexOf("/") + 1);

String cacheDir = filesDir + "/" + "cache";
String cacheDir = cachesDirFile.getAbsolutePath();
//String cacheDir = context.getCacheDir().getAbsolutePath(); // filesDir + "cache"
String dataAppDir = modulePath.substring(0, modulePath.lastIndexOf("/"));
String nativeLibraryPath = dataAppDir + "/lib"; // getNativeLibraryPath(context);
//tring nativeLibraryPath = getNativeLibraryPath(context); //{baseApkDir}/../lib

File nativeLibraryPathFile = new File(nativeLibraryPath);
for (File f : nativeLibraryPathFile.listFiles())
{
if(f.isDirectory())
{
nativeLibraryPath = f.getAbsolutePath();
break;
}
}

ClassLoader classLoader = this.getClass().getClassLoader();
//TODO: hook context.getClassLoader() and replaced to this classLoader
//ClassLoader classLoader = de.robv.android.xposed.XposedBridge.BOOTCLASSLOADER;
File externalStorageDirectory = Environment.getExternalStorageDirectory();

String externalOverrridePath = new File(externalStorageDirectory, "Android/data/" + packageName + "/files/.__override__").getAbsolutePath();
String externalOverrridePathLegacy = new File(externalStorageDirectory, "../legacy/Android/data/" + packageName + "/files/.__override__").getAbsolutePath();

Expand All @@ -74,31 +97,47 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
String[] initParams = {filesDir, cacheDir, nativeLibraryPath};
String[] externalOverrrideParams = {externalOverrridePath, externalOverrridePathLegacy}; //deprecated

if (BuildConfig.Debug)
Log.i("XamarinPosed", "nativeLibraryPath: " + nativeLibraryPath2);
String nativeLibraryPath3 = nativeLibraryPath + "/";
try
{
System.loadLibrary("xamarin-debug-app-helper");
//DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams, externalOverrrideParams);
DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams);
}
else
if (BuildConfig.Debug)
{
System.load(nativeLibraryPath3 + "libxamarin-debug-app-helper.so");
//DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams, externalOverrrideParams);
DebugRuntime.init(sourceDirs, nativeLibraryPath2, initParams);
}
else
{
System.load(nativeLibraryPath3 + "libmonosgen-2.0.so");
}
}
catch (UnsatisfiedLinkError e)
{
System.loadLibrary("monosgen-2.0");
Log.e("XamarinPosed", "Failed to load mono lib, could be architecture mismatch (64bit module vs 32bit app or vice versa)", e);
isInited = false;
return;
}
System.loadLibrary("xamarin-app");

System.load(nativeLibraryPath3 + "libxamarin-app.so");
try
{
System.loadLibrary("mono-native");
System.load(nativeLibraryPath3 + "libmono-native.so");
}
catch (UnsatisfiedLinkError e)
{
Log.i("monodroid", "Failed to preload libmono-native.so (may not exist), ignoring", e);
}

System.loadLibrary("monodroid");
System.load(nativeLibraryPath3 + "libmonodroid.so");
System.load(nativeLibraryPath3 + "libmono-btls-shared.so");
System.load(nativeLibraryPath3 + "libxa-internal-api.so");
Log.i("XamarinPosed", "load lib done");
//Runtime.initInternal(localeStr, sourceDirs, nativeLibraryPath2, initParams, classLoader, externalOverrrideParams, MonoPackageManager_Resources.Assemblies, Build.VERSION.SDK_INT, isEmulator());
Runtime.initInternal(localeStr, sourceDirs, nativeLibraryPath2, initParams, classLoader, MonoPackageManager_Resources.Assemblies, Build.VERSION.SDK_INT, isEmulator());
ApplicationRegistration.registerApplications();

Log.i("XamarinPosed", "init internal done");

// /data/data/com.my.app/files
_loader = new xamarin.posed.Main_Loader();
//_loader = new xamarin.posed.Main_Loader(modulePath, packageName);
Expand All @@ -108,7 +147,7 @@ public void initZygote (de.robv.android.xposed.IXposedHookZygoteInit.StartupPara
if (isInited && _loader != null)
{
_loader.initZygote (p0);
}
}
}

public void handleInitPackageResources (de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam p0)
Expand Down

0 comments on commit ab39f4b

Please sign in to comment.