raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.18k stars 795 forks source link

AndroidClassLoadingStrategy Injecting problem in Android SDK 33 #1675

Open Dev4Mod opened 1 month ago

Dev4Mod commented 1 month ago

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();
raphw commented 1 month ago

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.

Dev4Mod commented 1 month ago

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);

Dev4Mod commented 1 month ago

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.