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
ClassNotFoundException
when trying to do that. – Pratik Mandrekarcompile project(':mylibraryproject')
to thedependencies
section of mybuild.gradle
and I have addedinclude ':mylibraryproject'
project(':mylibraryproject').projectDir = new File('mylibraryproject/ProjectName')` to the app'ssettings.gradle
. – Pratik Mandrekar