0
votes

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

2
Give an example of code you want to call. Is it an activity you want to start? Is it a static method in a class?Gavriel
It is an activity. Launched via an intent. I'm getting a ClassNotFoundException when trying to do that.Pratik Mandrekar
Show your app's manifest.xml, and the code you use to launch the activity from your lib and the full error message you getGavriel
Did you add your library as a dependency in your app? Can you show your other app build.gradle?RaGe
I have added compile project(':mylibraryproject') to the dependencies section of my build.gradle and I have added include ':mylibraryproject' project(':mylibraryproject').projectDir = new File('mylibraryproject/ProjectName')` to the app's settings.gradle.Pratik Mandrekar

2 Answers

0
votes

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

0
votes

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