byte-buddy
byte-buddy copied to clipboard
AndroidClassLoadingStrategy Injecting problem in Android SDK 33
https://github.com/raphw/byte-buddy/blob/12baac491ae4e103e641aca17981435b03484919/byte-buddy-android/src/main/java/net/bytebuddy/android/AndroidClassLoadingStrategy.java#L894
This is causing the error "java.lang.SecurityException: Can't exempt class, process is not debuggable." in applications that do not have the debuggable flag.
Exemple code:
var strategy = new AndroidClassLoadingStrategy.Injecting(context.getDir(
"generated",
Context.MODE_PRIVATE));
return new ByteBuddy()
.subclass(myAbstractClass)
.method(ElementMatchers.isAbstract()).intercept(MethodDelegation.to(new HookInterceptor()))
.make()
.load(classLoader, strategy)
.getLoaded();
This seems to be a long standing issue: https://github.com/mockito/mockito/issues/2302
Not sure if this can be avoided as it seems to be a security setting. Could you experiment a bit? I am not much into Android these days, but I am happy to merge a solution.
I used trusted as false because it is not necessary for the dex in classloader to have access to restricted APIs and this solved my problem without any
addDexPath.invoke(classLoader, jar.getAbsolutePath(), false);
In the excerpt about @UnsupportedAppUsage
/*
* This annotation indicates that you may be able to access the member at runtime, but that
* this access is discouraged and not supported by Android. If there is a value
* for {@link #maxTargetSdk()} on the annotation, access will be restricted based
* on the {@code targetSdkVersion} value set in your manifest.
*/
As addDexPath only has the annotation without maxTargetSdk there will be no problems using it.