Pratik Mandrekar
Pratik Mandrekar

Reputation: 9568

Android - Add existing app to another app as a library project

I have generally followed the steps to convert my app into a library project by updating the build.gradle to have apply plugin: 'com.android.library' and then including it my other app by adding it to the settings.gradle and build.gradle.

I can see that the library project compiles when I go a gradle sync on the app to which I have added the library. However I am unable to invoke or access code from the library from within the app. What's the right way to do this?

The library project is an app that has a MainActivity and generally behaves like any Android app. Is there anything else that needs to be done to make it into a proper library project?

Exception while launching Activity from library from app that includes the library:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{myapp.debug/mylib.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "mylib.MainActivity" on path: DexPathList[[zip file "/data/app/myapp.debug-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.ClassNotFoundException: Didn't find class "mylib.MainActivity" on path: DexPathList[[zip file "/data/app/myapp.debug-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at android.app.Instrumentation.newActivity(Instrumentation.java:1066) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5254)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  Suppressed: java.lang.ClassNotFoundException: com.janacare.aina.MainActivity at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 13 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Upvotes: 1

Views: 2815

Answers (2)

jay solanki
jay solanki

Reputation: 7

Implement this code for launch library module Activity in another android project

1) Intent i = new Intent(Intent.ACTION_MAIN);

2) i.setComponent(new ComponentName("myparent.company.com.myparent", "myparent.company.com.myparent.MainActivity"));

3)startActivity(i);

but Gives me Error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{mychild.agamitech.com.mychild/mychild.agamitech.com.mychild.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {myparent.company.com.myparent/myparent.company.com.myparent.MainActivity}; have you declared this activity in your AndroidManifest.xml?

I want use My Another Project activity into Many other projects

Upvotes: 0

JNTX 1412
JNTX 1412

Reputation: 75

maybe you should try these

in android studio, click file->new->import module->browse to your library will be app location
this method will automatically add your app to gradle and import your app to your project

Upvotes: 0

Related Questions