Class not found exception after adding library project - android

I have created a library project as .jar file and try to use it in another project. But I am getting Class not found exception while using it.I added all the activities from the library in my manifest file and its all permission. But still getting same error.
Here is my manifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity android:name=".Activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="in.moovo.moovobroker.Activity.Mainactivity"/>
</application>
My error stack trace here :
java.lang.NoClassDefFoundError: Failed resolution of: Lin/moovo/moovobroker/R$layout;
at in.moovo.moovobroker.Activity.Mainactivity.onCreate(Mainactivity.java:85)
at android.app.Activity.performCreate(Activity.java:6072)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:139)
at android.app.ActivityThread.main(ActivityThread.java:5298)
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:950)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
Caused by: java.lang.ClassNotFoundException: Didn't find class "in.moovo.moovobroker.R$layout" on path: DexPathList[[zip file "/data/app/in.moovo.moovobrokerappmapview-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 in.moovo.moovobroker.Activity.Mainactivity.onCreate(Mainactivity.java:85) 
at android.app.Activity.performCreate(Activity.java:6072) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424) 
at android.app.ActivityThread.access$900(ActivityThread.java:155) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:139) 
at android.app.ActivityThread.main(ActivityThread.java:5298) 
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:950) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
Suppressed: java.lang.ClassNotFoundException: in.moovo.moovobroker.R$layout
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)
... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

This error is not for Activity. This is for in/moovo/moovobroker/R$layout which you used into in.moovo.moovobroker.Activity.Mainactivity.
The way you are using is not correct way. When you decide to use jar file, the classes which are into jar, should not have dependency on any external resources.
Correct way is to use aar file. Read how to Create aar file in Android Studio

jar files contain only code! No activities can be found in your jar file. If you wish to use the activity resource files, you should use aar file instead.
Specifically: the 'aar' bundle is the binary distribution of an Android Library Project.
The file extension is .aar, and the maven artifact type should be aar as well, but the file itself a simple zip file with the following entries:
/AndroidManifest.xml (mandatory)
/classes.jar (mandatory)
/res/ (mandatory)
/R.txt (mandatory)
/assets/ (optional)
/libs/*.jar (optional)
/jni/<abi>/*.so (optional)
/proguard.txt (optional)
/lint.jar (optional)
These entries are directly at the root of the zip file.
See also this post for details.

Related

Android Package Name Dex Issue

I'm to trying create an application with package name "com.cvs.myapp". App builds perfectly but crashes in device at runtime.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cvs.myapp/com.cvs.myapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.cvs.myapp.MainActivity" on path: DexPathList[[zip file "/data/app/com.cvs.myapp-DG6fxWRjVXjsKQV_vwJwAw==/base.apk"],nativeLibraryDirectories=[/data/app/com.cvs.myapp-DG6fxWRjVXjsKQV_vwJwAw==/lib/x86, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.cvs.myapp.MainActivity" on path: DexPathList[[zip file "/data/app/com.cvs.myapp-DG6fxWRjVXjsKQV_vwJwAw==/base.apk"],nativeLibraryDirectories=[/data/app/com.cvs.myapp-DG6fxWRjVXjsKQV_vwJwAw==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
It's just a new project with no other additions containing only a MainActivity class. Same new project will work if i rename the package to "com.demo.myapp".
Using Analyse APK, I can see that the classes.dex file doesn't contain the MainActivity. Is the name "cvs" causing the issue as it will work perfectly with any other name instead of it?
gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
build.gradle(project)
classpath 'com.android.tools.build:gradle:3.6.3'
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cvs.myapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.cvs.myapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As you mentioned this is happening when you are using the latest Gradle that is currently 3.6.3, It has a new packaging tool ZipFlinger, which might be causing the issue, try below code to disable it
android.useNewApkCreator=false
Moreover I got some relevant links, please have a look.
https://issuetracker.google.com/issues/150198184
After upgrading to android 3.6.1 apk cant be installed on devices
Android App runtime crash after update gradle plugin 3.6
MultiDex problem with Android Studio 3.6 when implementing it the right way

Android Provider in Dynamic Feature module not found in Base Module

I am using some libraries in my dynamic feature module. These libraries are Adding some providers to the manifest.
For example in my build.gradle file in the dynamic module:
dependencies {
...
implementation 'com.github.esafirm.android-image-picker:imagepicker:1.11.1'
...
}
This library adds the following tag to the manifest:
<provider
android:name="com.esafirm.imagepicker.helper.ImagePickerFileProvider"
android:authorities="{$applicationId}.imagepicker.provider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/image_picker_provider_paths" />
</provider>
The problem is when I build my base module the app crashes because it cannot find the provider. How can I solve this?
As it is stated here:
The manifest for your app’s base module is similar to that of any other app module. Keep in mind, when Google Play generates your app’s base APK, it merges manifests for all modules into that of the base APK.
But why only providers are affected? Are activities ignored?
This is the stacktrace I get:
java.lang.RuntimeException: Unable to get provider com.esafirm.imagepicker.helper.ImagePickerFileProvider: java.lang.ClassNotFoundException: Didn't find class "com.esafirm.imagepicker.helper.ImagePickerFileProvider" on path: DexPathList[[zip file "/data/app/ir.rashin.mototel-h7B8g42gBcVABkik16vqzw==/base.apk"],nativeLibraryDirectories=[/data/app/ir.rashin.mototel-h7B8g42gBcVABkik16vqzw==/lib/x86, /system/lib]]
at android.app.ActivityThread.installProvider(ActivityThread.java:6396)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
at android.app.ActivityThread.access$1100(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.esafirm.imagepicker.helper.ImagePickerFileProvider" on path: DexPathList[[zip file "/data/app/ir.rashin.mototel-h7B8g42gBcVABkik16vqzw==/base.apk"],nativeLibraryDirectories=[/data/app/ir.rashin.mototel-h7B8g42gBcVABkik16vqzw==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.AppComponentFactory.instantiateProvider(AppComponentFactory.java:121)
at android.support.v4.app.CoreComponentFactory.instantiateProvider(CoreComponentFactory.java:62)
at android.app.ActivityThread.installProvider(ActivityThread.java:6380)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853) 
at android.app.ActivityThread.access$1100(ActivityThread.java:199) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Try this solution.
Add provider with tools:node="remove" in you app build.gradle
<provider
android:name="com.esafirm.imagepicker.helper.ImagePickerFileProvider"
android:authorities="{$applicationId}.imagepicker.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:node="remove" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/image_picker_provider_paths" />
</provider>
In Android Studio it will be marked as red but when you build bundle with
./gradlew clean bundleRelease
it should finish with success.
Now you can generate .apks file and unzip it:
java -jar bundletool-all-0.10.2.jar build-apks --bundle=app.aab --output=release.apks --ks=release.keystore --ks-pass=pass:xxxxxx --ks-key-alias=xxxxxxkey --key-pass=pass:xxxxxx
unzip release.apks
After this you can decode your .apk files (for example with apktool) to take a look at their manifests.
Now in base-master.apk you shouldn't see your provider entries, and in {dynamic-module}master.apk it should be available.
java -jar apktool_2.4.0.jar d release/splits/base-master.apk

In Android Studio, when I try to launch my app, it says "My First App has stopped" in the Android emulator

I believe what is causing it is a stack trace.
I am going through the official Android app bulding tutorial here: https://developer.android.com/training/index.html
I finished the "Start Another Activity" section and have found this error while trying to test it. I was also not able to run the app in the earlier parts of the tutorial.
That led me to this link: Unfortunately MyApp has stopped. How can I solve this?
Which led me to this link:
What is a stack trace, and how can I use it to debug my application errors?
But I am still having trouble figuring it out.
Here is a snippet of the error log:
12-14 09:52:06.962 3696-3696/com.example.myfirstapp I/zygote: at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
12-14 09:52:06.962 3696-3696/com.example.myfirstapp I/zygote: at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:438)
12-14 09:52:06.962 3696-3696/com.example.myfirstapp I/zygote: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:807)
12-14 09:52:06.962 3696-3696/com.example.myfirstapp D/AndroidRuntime: Shutting down VM
12-14 09:52:06.964 3696-3696/com.example.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 3696
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/base.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/base.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
12-14 09:52:06.964 3696-3696/com.example.myfirstapp E/AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
... 12 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/base.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.myfirstapp-R68LcIdIMck0utpOhrKfsA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 15 more
Any suggestions on how to figure this out? Thanks!
Edit:
I believe this is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" >
<!-- The meta-data tag is required if you support API level 15 and lower -->
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
You have incorrect closing tag in your manifest:
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" >
<!-- The meta-data tag is required if you support API level 15 and lower -->
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
This is the incorrect closing tag without open tag:
<!-- The meta-data tag is required if you support API level 15 and lower -->
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
Try removing it.
UPDATED:
From the tutorial, you're missing the open tag of <meta-data, so you need to correct it by adding the open tag:
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" />
After adding the tag, your code will be like this:
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" >
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>

Android: Rare and Non-Reproducible ClassNotFoundException

I have a published app that gets reports about a ClassNotFoundExceptionfrom users that I went beyond my wits end trying to fix. I have not been able to reproduce this crash and similar instances of this error here on SO have either been cases of incorrect path to a dependency or a different package in the MainActivity than the manifest.
The issues with the wrong path to the dependencies are an Eclipse thing, while all the other errors could be easily fixed with Android Studio's "sync" and "clean project" (which I did to no avail).
I've triple checked all the package names and did not find any discrepancies.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.johan.fsc">
<uses-permission android:name = "android.permission.VIBRATE"/>
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:fullBackupContent="true">
<activity
android:name="com.johan.fsc.MainActivity"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:theme="#style/splashscreenTheme"
android:hardwareAccelerated="false"
android:largeHeap="true">>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name = "com.johan.fsc.SettingsActivity"
android:theme="#style/PreferencesTheme"/>
</application>
Here is the error report I always get from users:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.johan.fsc/com.johan.fsc.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.johan.fsc.MainActivity" on path: DexPathList[[zip file "/data/app/com.johan.fsc-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.johan.fsc.MainActivity" on path: DexPathList[[zip file "/data/app/com.johan.fsc-1/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:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
... 10 more
Suppressed: java.io.IOException: Failed to open oat file from dex location '/data/app/com.johan.fsc-1/base.apk'
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:295)
at dalvik.system.DexFile.<init>(DexFile.java:80)
at dalvik.system.DexFile.<init>(DexFile.java:59)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:262)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:231)
at dalvik.system.DexPathList.<init>(DexPathList.java:109)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:57)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:370)
at android.app.LoadedApk.makeApplication(LoadedApk.java:562)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4818)
at android.app.ActivityThread.access$1500(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
... 7 more
Caused by: java.io.IOException: Failed to open oat file from /data/app/com.johan.fsc-1/arm/base.odex (error Failed to open oat filename for reading: No such file or directory) (no dalvik_cache availible) and relocation failed.
... 22 more
Caused by: java.io.IOException:
... 22 more
Caused by: java.io.IOException: Failed to remove obsolete file from /data/dalvik-cache/arm/data#app#com.johan.fsc-1#base.apk#classes.dex when searching for dex file /data/app/com.johan.fsc-1/base.apk: Permission denied
... 22 more
Suppressed: java.lang.ClassNotFoundException: com.johan.fsc.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/output.
It would seem that the culprit is java.io.IOException: Failed to remove obsolete file from /data/dalvik-cache/arm/data#app#com.johan.fsc-1#base.apk#classes.dex when searching for dex file /data/app/com.johan.fsc-1/base.apk: Permission denied, but I have not found why it's being invoked for some users and not the majority, and more importantly--how to fix it.
Possible Cause 1: There is a typo in the manifest in the line android:largeHeap="true">>. xml line ends with >>. This may be preventing the Intent-Filter from executing.
Possible Cause 2: My guess is that the phone is searching for a class <PackageName>.<PackageName>.<ClassName> instead of <PackageName>.<ClassName> . So definitely <PackageName>.<PackageName>.<ClassName> is not found and ClassNotFoundException would be the result. In android studio, in the manifest, by default activity name is written like this: android:name=".<ClassName>" instead of android:name="<PackageName>.<ClassName>" . You can give this a try.
Most phones may be checking whether packagename is added before the class name in the manifest. If it is added then they don't add it while searching of the class. While few phones must be adding the package name by default while searching for the class. Hence, they cannot find your class.
Certainly, you're naming your activity wrong. An activity CAN NOT consist the package's name in its name attribute.
Since you're already defining the package name here
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.johan.fsc">
You shouldn't mention that name here
<activity android:name="com.johan.fsc.MainActivity"
The only situation you should use package name along with a class's name is when the java class resides in a different package, as done in import statements.
Even if your activity is not present in the src folder and is instead present in a nested folder, android studio should handle the linking of manifest and the activity itself. Yet again, package name shouldn't be used there.

How to solve the ClassNotFoundException of android project in eclipse?

I have a android project built with Eclipse. I can build the APK, but it crashed as the following logs:
E/AndroidRuntime(27898): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/pili/pldroid/streaming/camera/demo/CameraStreamingActivity;
E/AndroidRuntime(27898): at com.example.rtmpdemo.MainActivity$1.onClick(MainActivity.java:35)
E/AndroidRuntime(27898): at android.view.View.performClick(View.java:4757)
E/AndroidRuntime(27898): at android.view.View$PerformClick.run(View.java:19757)
E/AndroidRuntime(27898): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(27898): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(27898): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(27898): at android.app.ActivityThread.main(ActivityThread.java:5258)
E/AndroidRuntime(27898): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27898): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(27898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
E/AndroidRuntime(27898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
E/AndroidRuntime(27898): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity" on path: DexPathList[[zip file "/data/app/com.example.rtmpdemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.rtmpdemo-1/lib/arm, /vendor/lib, /system/lib]]
E/AndroidRuntime(27898): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(27898): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(27898): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(27898): ... 11 more
E/AndroidRuntime(27898): Suppressed: java.lang.NoClassDefFoundError: com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity
E/AndroidRuntime(27898): at dalvik.system.DexFile.defineClassNative(Native Method)
E/AndroidRuntime(27898): at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/AndroidRuntime(27898): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/AndroidRuntime(27898): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
E/AndroidRuntime(27898): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/AndroidRuntime(27898): ... 13 more
E/AndroidRuntime(27898): Suppressed: java.lang.ClassNotFoundException: com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity
E/AndroidRuntime(27898): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(27898): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(27898): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(27898): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(27898): ... 12 more
E/AndroidRuntime(27898): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
W/ActivityManager( 810): Force finishing activity com.example.rtmpdemo/.MainActivity
I have defined the CameraStreamingActivity absolutely and registered in the AndroidManifest.xml. The structure of the project like this:
./AndroidManifest.xml
./assets
./libs
./libs/android-support-v4.jar
./libs/armeabi-v7a
./libs/armeabi-v7a/libpldroid_ffmpegbridge.so
./libs/pldroid-camera-streaming-1.2.0.jar
./lint.xml
./proguard-project.txt
./project.properties
./src
./src/com
./src/com/example
./src/com/example/rtmpdemo
./src/com/example/rtmpdemo/MainActivity.java
./src/com/pili
./src/com/pili/pldroid
./src/com/pili/pldroid/streaming
./src/com/pili/pldroid/streaming/camera
./src/com/pili/pldroid/streaming/camera/demo
./src/com/pili/pldroid/streaming/camera/demo/AudioStreamingActivity.java
./src/com/pili/pldroid/streaming/camera/demo/CameraStreamingActivity.java
./src/com/pili/pldroid/streaming/camera/demo/StreamingBaseActivity.java
./src/com/pili/pldroid/streaming/camera/demo/TestActivity.java
The AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtmpdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.rtmpdemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity" >
</activity>
<activity
android:name="com.pili.pldroid.streaming.camera.demo.AudioStreamingActivity"
android:label="#string/title_activity_audio_streaming" >
</activity>
<activity android:name="com.pili.pldroid.streaming.camera.demo.TestActivity" >
</activity>
</application>
</manifest>
HOWEVER, it works fine after i import the project into Android Studio.
The import-summary.txt as following:
ECLIPSE ANDROID PROJECT IMPORT SUMMARY
======================================
Ignored Files:
--------------
The following files were *not* copied into the new Gradle project; you
should evaluate whether these are still needed in your project and if
so manually move them:
* .DS_Store
* ic_launcher-web.png
* proguard-project.txt
Replaced Jars with Dependencies:
--------------------------------
The importer recognized the following .jar files as third party
libraries and replaced them with Gradle dependencies instead. This has
the advantage that more explicit version information is known, and the
libraries can be updated automatically. However, it is possible that
the .jar file in your project was of an older version than the
dependency we picked, which could render the project not compileable.
You can disable the jar replacement in the import wizard and try again:
android-support-v4.jar => com.android.support:support-v4:19.1.0
Moved Files:
------------
Android Gradle projects use a different directory structure than ADT
Eclipse projects. Here's how the projects were restructured:
* AndroidManifest.xml => app/src/main/AndroidManifest.xml
* assets/ => app/src/main/assets/
* libs/armeabi-v7a/libpldroid_ffmpegbridge.so => app/src/main/jniLibs/armeabi-v7a/libpldroid_ffmpegbridge.so
* libs/pldroid-camera-streaming-1.2.0.jar => app/libs/pldroid-camera-streaming-1.2.0.jar
* lint.xml => app/lint.xml
* res/ => app/src/main/res/
* src/ => app/src/main/java/
Next Steps:
-----------
You can now build the project. The Gradle project needs network
connectivity to download dependencies.
Bugs:
-----
If for some reason your project does not build, and you determine that
it is due to a bug or limitation of the Eclipse to Gradle importer,
please file a bug at http://b.android.com with category
Component-Tools.
(This import summary is for your information only, and can be deleted
after import once you are satisfied with the results.)
What did i miss?
Thanks.
Actually, your code don't have any problem. And this is the runtime error.
I sure that root cause is coming from libs which you import by wrong way.
"./libs/pldroid-camera-streaming-1.2.0.jar"
Try to remove this lib and import again by other way such as:
way 1: Java build path-> Libraries -> Add External JARS..
way 2: manual copy this lib directly to ".libs/" folder

Categories

Resources