I am attempting to develop a \"Dynamic\" Android application.
Dynamic in the sense that I have an activity listed in the manifest that is \"built\" at runtime.
I have managed to call a dynamically instantiated Activity and set the required layout content using ByteBuddy.
Heres how
final DynamicType.Unloaded extends AppCompatActivity> dynamicType = new ByteBuddy(ClassFileVersion.JAVA_V8)
.subclass(AppCompatActivity.class)
.name(CLASS_NAME)
.method(named("onCreate").and(takesArguments(1)))
.intercept(MethodDelegation.to(TargetActivity.class).andThen(SuperMethodCall.INSTANCE))
.make();
final Class extends AppCompatActivity> dynamicTypeClass = dynamicType.load(getClassLoader(), new AndroidClassLoadingStrategy.Injecting(this.getDir("dexgen", Context.MODE_PRIVATE))).getLoaded();
final Intent intent = new Intent(this, dynamicTypeClass);
startActivity(intent);
The method delegation class
public class TargetActivity {
public static void intercept(Bundle savedInstanceState, @This AppCompatActivity thiz) {
thiz.setContentView(R.layout.activity_fourth);
}
}
Even though this gives the desired result it still has issues as the super.onCreate(savedInstanceState)
call is made after I have called setContent
(I think).
Using the excellent ByteBuddy library is a much better approach IMHO than working with DEX manipulation.