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.
Yes you CAN start such an Activity
(assuming you have a dummy manifest Activity
entry).
If you don't like this technique, use Fragments
(they don't need entries in the manifest).
Alternatively use WebView and JavaScript like Apache Cordova et-al (cross platform too !).
ByteBuddy (kudos too @Rafael Winterhalter author of Byte Buddy) looks cool, maybe a learning curve involved. Why not download the linked project and try both techniques.
Here's how to include
ByteBuddy
in your Android Studio Gradle
project (build.gradle
):
android {
compileSdkVersion 25
buildToolsVersion '25'
dependencies {
compile 'com.android.support:appcompat-v7:25'
compile 'net.bytebuddy:byte-buddy:1.7.9'
compile 'net.bytebuddy:byte-buddy-android:1.7.9'
}
}
how I can "find" my dynamically instantiate class at runtime?
See my answer here and follow the links for source code and tutorial (Apache Ant {Eclipse
compatible, build.xml
} and Android Studio Gradle
examples build.gradle
of the same code, you need some custom build steps which these project's provide).
Code snippet:
// Internal storage where the DexClassLoader writes the optimized dex file to.
final File optimizedDexOutputPath = getDir(SECONDARY_DEX_INTERNAL_DIR, Context.MODE_PRIVATE);
// Initialize the class loader with the secondary dex file.
DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(),
optimizedDexOutputPath.getAbsolutePath(),
null,
getClassLoader());
Class libProviderClazz = null;//variable libProviderClazz of type Class
try {
// Load the library class from the class loader.
libProviderClazz = cl.loadClass(PROVIDER_CLASS);
// Cast the return object to the library interface so that the
// caller can directly invoke methods in the interface.
// Alternatively, the caller can invoke methods through reflection,
// which is more verbose and slow.
LibraryInterface lib = (LibraryInterface) libProviderClazz.newInstance();
}
catch (Exception exception)
{
// Handle exception gracefully here.
exception.printStackTrace();
}
Q: How do I add an Activity, I cannot add it to the manifest ?
A: Use Fragments, they don't need entries in the manifest.
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.