How to start a non existent Activity mentioned in Manifest?

前端 未结 2 1242
长情又很酷
长情又很酷 2021-01-17 22:55

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.

2条回答
  •  感动是毒
    2021-01-17 23:11

    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?

    External loading of DEX files (class byte code)

    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.

提交回复
热议问题