Unable to instantiate activity from library - android

I am trying to create an application that has different flavours (e.g. free, paid), by putting as much common code as I can into a library, and making projects for each flavour. At the moment, the Java class for the main activity will be the same in all versions, it is merely the layout and some constants defined in the "values" resource directory that need to change to control the behaviour.
I have defined the activity in my "free" project's manifest file as follows:
<activity
android:name="librarypackagename.Foo"
android:label="#string/bar"
android:launchMode="singleTask"
android:uiOptions="splitActionBarWhenNarrow"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
But when testing the application, I receive the following error:
"Unable to instantiate Activity ComponentInfo{freeapppackagename/librarypackagename.Foo}: java.lang.ClassNotFoundException: Didn't find class "librarypackagename.Foo" on path: DexPathList[[zip file "/data/app/freeapppackagename-2.apk"],nativeLibraryDirectories=[/data/app-lib/freeapppackagename-2, /vendor/lib, /system/lib]]"
Is it not possible to use an activity from a library as the main activity? I have also tried this by extending Foo in my free app project, and using that as the main activity, but I still get the same exception.
Edit (removed manifest files, as it turned out they weren't related to the issue):
Also, in Eclipse, I've just noticed I am getting the following error message (although the project still builds):
Found 2 versions of android-support-v4.jar in the dependency list,
but not all the versions are identical (check is based on SHA-1 only at this time).
All versions of the libraries must be the same at this time.
Versions found are:
Path: D:\Work\Eclipse\LiteApplicationName\libs\android-support-v4.jar
Length: 556198
SHA-1: 4a6be13368bb64c5a0b0460632d228a1a915f58f
Path: D:\Work\Eclipse\LibraryName\libs\android-support-v4.jar
Length: 385685
SHA-1: 48c94ae70fa65718b382098237806a5909bb096e
Jar mismatch! Fix your dependencies
Strangely enough, the android-support-v4.jar file appears in "Referenced Libraries" in the application project, but in "Android Private Libraries" in the library.

There are a couple things to look out for.
Where you define your activity's class, ensure you have something to the effect of package com.librarypackagename; at the top of the file.
You should have two AndroidManifest.xml files, one for your library and one for your main project.
In your library AndroidManifest.xml, ensure that the element defining your activity has a parent XML tag that is <library> and not <application>
In your library AndroidManifest.xml, ensure that you are setting the package attribute to something like com.librarypackagename
Let me know if you have any questions about any of these points. It would be helpful if you provided more information about the current structure of your project.

Turns out that it was related to the library mismatch issue. I right clicked on the application project, and selected Android Tools > Add Support Library. This installed a package, and then "android-support-v4.jar" appeared in the application project's "Android Private Libraries" directory as well. Now I am no longer getting the exception.

Related

manifest merger failed when the same library is imported in 2 feature modules

I am running into a build issue where gradle complains about "getMergedManifest failed manifest merger failed"
the error in the log shows lines like:
WARN - .tools.idea.model.ManifestInfo - getMergedManifest failed Manifest merger failed : Attribute provider#com.google.firebase.provider.FirebaseInitProvider#authorities value=(com.xxxxxxxxxx.feature_team.firebaseinitprovider) from AndroidManifest.xml:10:13-72
is also present at AndroidManifest.xml:36:559-633 value=(com.xxxxxxxxx.base.firebaseinitprovider).
Suggestion: add 'tools:replace="android:authorities"' to <provider> element at AndroidManifest.xml:8:9-12:39 to override.
What am i suppose to do here?
I don't have any authorities section in any of my manifest files.
The error arrives when in 2 feature modules the same shared library is imported which in result is depending on some google libs. I am sure that all versions of external libs are the same.
Anyone knows how to solve this?
------- edit. Still got errors ------.
......./base/src/main/AndroidManifest.xml:70:13-46 Error:
Attribute activity#com.facebook.LoginActivity#splitName value=(feature_match) from [:feature_match] AndroidManifest.xml:70:13-46
is also present at [:feature_topvoters] AndroidManifest.xml:73:13-50 value=(feature_topvoters).
Suggestion: add 'tools:replace="android:splitName"' to <activity>
element at AndroidManifest.xml:68:9-71:75 to override.
......./base/src/main/AndroidManifest.xml:74:13-46 Error:
Attribute activity#com.github.gorbin.asne.core.OAuthActivity#splitName value=(feature_match) from [:feature_match] AndroidManifest.xml:74:13-46
is also present at [:feature_topvoters] AndroidManifest.xml:77:13-50 value=(feature_topvoters).
Suggestion: add 'tools:replace="android:splitName"' to <activity> element at AndroidManifest.xml:72:9-75:80 to override.
So basically the issue appears when i try to add the same library in both 2 feature modules. What do i need to tell the manifest to prevent this merge conflict.
Both manifest files from the feature modules are empty. It must be the thirdparty manifest files causing the issue.
I thought the multiple feature/module structure would increase the architecture. But by now i really think it is way easier to have one big module. There are just too many issues in the whole gradle build model.
Ok after 10 hours of trying to find a solution i came up with the following:
I cloned the library causing the issue (Facebook).
Instead of compiling against maven i created a new library and put the complete source of the facebook SDK inside.
Inside the manifest.xml from that facebook SDK, there is an activity defined:
<activity
tools:replace="android:theme"
android:name="com.facebook.LoginActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
/>
I removed the activity from the manifest.xml and moved the activity definition into the main app manifest.xml
In this way the manifest merger will not try to merge two of the same activity definitions, but uses the one defined in the main app.
Works. But it is a shitty solution of course.
Not sure if this a bug in the build process. I have spend over 20 hours trying to fix this. Cannot find a way.
Multi module applications are not easy to construct.

After adding google-play-service_lib my resourceIds cannot be recognized

I am using ADT 23.0.2.
I just imported the google-play library to my workspace and added the reference to it in my project.
Now nowhere in my project the resource ids can be recognized because there is no R file.
(Anywhere I have R.id.blah I get the error "R cannot be resolved to a variable);
It's just gone. What am I supposed to do?
I exactly followed the steps by developers.google.com. It's nothing complicated but I don't know why this happened.
(I have all layout files in place and no import for android.R are in my class files.)
Before adding the
Tools I use:
Here are screenshots from my app properties:
My manifest
<permission
android:name="com.appname.appname4.MAPS_RECEIVE"
android:protectionLevel="signature">
</permission>
<uses-permission android:name="com.appname.appname4.MAPS_RECEIVE"/>
<activity
android:name="com.appname.appname4.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.appname.appname4.SearchResultListActivity"
android:label="#string/title_activity_search_result_list"
android:parentActivityName="com.appname.appname4.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.appname.appname4.MainActivity" />
</activity>
<activity
android:name="com.appname.appname4.ContactInfoActivity"
android:label="#string/title_activity_property_detail"
android:parentActivityName="com.appname.appname4.SearchResultListActivity"
>
</manifest>
I had similar issues when I first started Android programming. Here are some fixes that worked for me in the past.
1. Fix/Check all xml(layout) files.
Many times a R file not being generated is due to an issue with your layout file or Android Manifest file. Follow this link and work your way down the page checking permissions and the general layout format.
2. Project/Clean.
go to Project->Clean.
Also make sure you have selected the option to build project automatically.(Without this option check marked R file is never generated automatically).
3. Fix Project Properties.
Right click on your project in Package explorer then choose fix project properties.
Repeat Step 1 after this.
4. Builders.
Go to Project->Properties then Builders. Select the appropriate boxes.
Repeat Step 1, and Clean and rebuild project.
5. Appcom7 and Google Play services error.
Sometimes to fix errors associated with installing these two reference libraries. I copy there folders into a generic library folder on my hard(where I keep most of my eclipse reference libraries), after this I delete them from the Package explorer and from the "default" directory where they were installed. I Fix project properties again and clean/rebuild. This should get you back to your non-reference library version of the package. Then I manually add them as Projects to my eclipse IDE and finally follow this to add them as references to my project. Then fix project properties,clean and rebuild.
6. Reference Question.
Finally, referring to this StackOverflow page has helped me countless times. And one way or another fixed my issue.
Good Luck.

Unable to instantiate activity ComponentInfo

I'm working with an Android project I've Imported from someone else. I've got all the dependencies sorted, there are no errors in the project, but when I try and launch it, I get:
04-08 16:49:41.761: E/AndroidRuntime(19254): FATAL EXCEPTION: main
04-08 16:49:41.761: E/AndroidRuntime(19254): java.lang.RuntimeException: Unable to
instantiate activity ComponentInfo{com.me.app/com.me.app.ui.ActivityDashboard}:
java.lang.ClassNotFoundException: Didn't find class
"com.me.app.ui.ActivityDashboard"
on path: /data/app/com.me.app-1.apk
My Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar">
<activity
android:name=".ui.ActivityDashboard"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
</application>
This seems to be a common problem, I've read all the other questions I've found and done the following, all to no avail:
ADT and the SDK are fully updated
I'm building against Android 2.3.3 (SDK 10)
This is selected under Android Build Target, and the Library is in the Java Build Path
All the classpaths in the Manifest are correct, I've double checked them all.
All Activities are in the Manifest, with the correct Intents.
and the Library is in the Java Build Path
That is incorrect. Please back out this change, then move the JAR into your project's libs/ directory.
While adding a JAR manually to your build path will satisfy the compiler at compile time, it will not add the contents of the JAR to the APK file at runtime, resulting in ClassNotFoundExceptions and the like.
Note that I am referring to third-party JARs, such as the first four entries in your screenshot. None of those appear to be in libs/, and all need to be.
The sole exceptions for the all-JARs-must-be-in-libs/ rule are:
Android's own platform JAR, attached to your project via selecting the build target (reason: this JAR's contents specifically does not need to be included in your APK)
Android library projects, which have their own libs/ directories for any third-party JARs that they reference
Your core error is that com.me.app.ui.ActivityDashboard is not found, suggesting that this is from one of your four JARs that are not in libs/.

Implementing an Android library project

I have an android project that I want to "clone" for a second similar project which only differs by one file: it's sqlite database (assets/mydata.sql).
I've turned the source project (reslib) into a library and added it to my clone project's properties (the source project shows up under "Library Projects" as reslib.jar)
Thing is, I'm not sure how to launch the main activity in the source project. The source project's main activity sets-up a TabHost. How do I launch into the source project's main activity from my clone project? I started pasting code into "cloneActivity.java" to fire up the TabHost but then wondered if there was a better way.
Doing this is pretty straight-forward. In the manifest for your dependent project, you need to specify the source project's activity as the one you want to launch.
Suppose your source project has package name com.example.source, your dependent project has package name com.example.dependent, and the main activity in your source project is MainActivity.java.
Then in AndroidManifest.xml in your dependent project, you would have something like the following:
<application
android:icon="#drawable/logo" >
<activity
android:name="com.example.source.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- possibly lots more activities and other declarations -->
</application>
Important Notes: In the manifests for both your source project and the dependent project, you must make sure to list all activities, permissions, etc. If later on you add an activity to your source project, you'll need to remember to add it to the dependent project's manifest as well.
Also, you'll need to copy anything in the source project's assets directory to the dependent project—and don't forget to keep that in sync as well. (This is true as of June 2012, I've heard that some future version of the Android build tools will likely alleviate this headache.)
And finally, if you use Eclipse to create the projects, it will create a default layout main.xml. Since resources in the dependent project override resources in the source project, make sure this doesn't trip you up.

Android java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

Ok guys this doesn't make sense to me at all and I don't understand why I am getting this error in my app. I have registered the activity class in the android manifrest xml..
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobile.batteryhelper/com.mobile.batteryhelper.Battery}:
java.lang.ClassNotFoundException: Caused by: java.lang.ClassNotFoundException: com.mobile.batteryhelper.Battery in loader dalvik.system.PathClassLoader[/mnt/asec/com.mobile.batteryhelper-1/pkg.apk]
Here is the androidmanifest.xml. Yes the .Battery class extends Activity also.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Battery"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Have you tried to clean your workspace. Goto Project->Clean and select your project. For some reason eclipse doesn't build my manifest automatically.
You have put 3rd party library in library folder and reference them. Go to Properties/ Java Build Path/ Libraries/ Add External JARs.
Check the Jar files in the Properties/ Java Build Path/ Order and Export
Have you tried to initialise anything in your class before OnCreate gets called?
If you initialise static variables (for example) it can give you this (infuriatingly obtuse) error.
Actually I found my answer to this same issue here:
java.lang.ClassNotFoundException on working app
You don't provide a name for the Application tag, only the Activity. So if your issue was the same as mine, you should be able to remove the following and things will work:
android:name=".Battery"
I had the same problem. It was a pretty confusing error until I found a way out. Go to Properties>Java Build Path>Order and Export and move your android-support-v4.jar to the top. It worked fine for me. Try it out.
Either your manifest package is not com.mobile.batteryhelper, or there is no com.mobile.batteryhelper.Battery class in your code. Possibly you don't have a public constructor for the class or the class is not declared public (though I think you would get a slightly different error message in that case).
Make sure that you aren't using ProGuard to strip your code. Make sure the class is public, in the correct package (same as your manifest package), and doesn't have a constructor. Also it would be useful to see the entire error stack crawl in case there is something useful inside of it.
Right click on your Battery class > BuildPath > Exclude
Then
Right click on your Battery class > BuildPath > Include your class again
I got this problem and fix it by moving all .jar files (external library files) which put in "lib" folder to "libs" folder. Somehow, eclipse only know .jar files put in libs folder
I had the same problem and I solved it by updating the ADT plugin.
See the crash log carefully:
loader dalvik.system.PathClassLoader[/mnt/asec/com.mobile.batteryhelper-1/pkg.apk
Here mnt means external SD card.
Normally, when android:installLocation="auto" is set in
AndroidManifest.xml, if the device won't have enough phone memory, then the
app will be stored on the SD card. Once you remove the SD card from
the device, the app can't find the APK location, then the app crashes.
Steps to produce the crash:
Issue able to replicate in HTC sensation 2.3.4 version.
Move the app to SD Card by going to
Settings-->Manage applications->Click on the app-->Move to SD card
Remove the SD card from the device and open the app
If you try with a Samsung device, it won't crash, but it won't show the app if you remove the SD Card. This behaviour is for a few devices only.
you can just remove the app from your mobile phone or your emulator
What worked for me is deleting the innermost files inside "build" folder, which are the files inside generated, intermediates, outputs, and tmp.

Categories

Resources