Implementing an Android library project - android

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.

Related

Importing Eclipse Library Project into AndroidStudio

I have an Eclipse Project that I like to migrate to AndroidStudio, with the following Structure/Content
1 LibraryProject with most of classes for all my Apps. This Library has 1 Activity ("SharedStarterActivity"), which will be started from all my 3 Apps.
3 Apps using the mentioned LibraryProject. These Apps referencing an Activity of my ProjectLibrary App as the Start Activity in the Apps AndroidManifest.xml.
<activity android:name=".SharedStarterActivity"
android:label="#string/IndividualAppName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In a first look, it does not make sense, but each app has its individual AppProp.class, which is used by the SharedStarterActivity.class and makes each app individual.
Now my question:
How can I migrate this Projects to AndroidStudio ?
I have read the migration guide "from Eclipse to AndroidStudio", but I am stuck now at
"Import Eclipse Projects to Android Studio" -> You should decide how
you will import your existing Eclipse ADT projects depending on their
structure:
Before Trial and Erroring, I wanted to ask you experts, how you would import the projects and will the mechanic still work with referencing an Activity from LibraryProject
in my Android Project (one of the 3 Apps) ?
see there is more types available in Google to migrate eclips to studio but which method I use please follow the step.
1) Open existing project in eclipse remove the library from setting and close eclipse
2) Open AS(Android Studio) import from eclipse option available there, choose the option and open your project in AS.
3) Some Library are already avail in AS. Click on your Main module (Main) press F4 open setting and check your lib available in listing or not.
4) if Avail then add from there else find the latest version of same library and add as module to your Main project.
5) please properly configure your Gradle ( Main Gradle and app Gradle file)
you may also refer below link to migrate in details
https://developer.android.com/studio/intro/migrate.html
or
http://www.developer.com/ws/android/migrating-from-eclipse-to-android-studio.html

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.

Using Non-Production Activity for Testing with Android Studio

Before Android Studio, testing and Android app involved a separate Android project that would be ignored when building for production. With Android Studio, production code and test code exist within the same project, which itself only has one set of anything else (manifest, assets, resources).
This being the case, how would I define a custom Activity to be used only for testing? For Android to allow any Activity to be started, it must be declared in the manifest. Is there a way around this restriction? How can Android be instructed to load test-only Activities without polluting the production facets of the project?
Here's how to do it.
1. Define a new build type in your build.gradle:
buildTypes {
extraActivity {
signingConfig signingConfigs.debug
debuggable true
}
}
In mine I've given it the debug signing configuration and set it to debuggable; configure as you see fit.
2. Click the Sync Project with Gradle Files button.
3. Choose your new build type from the Build Variants window.
4. Set up source directories for your new build type
In my example, my files are going in the com.example.myapplication3.app Java package.
src/extraActivity/java/com/example/myapplication3/app
src/extraActivity/res
5. Create your new activity in the folders for your build type
Be aware that if you right-click on the package and choose New > Activity, there's a bug and it will not put the files for the activity into your new build type's folder, but it will put them in src/main instead. If you do that, you'll have to move the filers over to the correct folder by hand.
6. Create an AndroidManifest.xml file in src/extraActivity
This manifest gets merged with the version in src/main, so only add the bits that you need to overlay on top of the original:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication3.app" >
<application>
<activity
android:name=".ExtraActivity"
android:label="Extra Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In my example, I've set up my new activity as a launcher activity so I can see it in the Apps screen and confirm it's working; you may not need to do that. Since I'm giving my app two launcher icons, I also need to follow the advice at Two launcher activities and add this to my main actvity's intent-filter (in src/main/AndroidManifest.xml); you may not need to do this either:
<category android:name="android.intent.category.DEFAULT"/>
Here's a screenshot of my project layout after all this is done:
This works for me. I can switch build types back and forth with the Build Variants window (you can see the tab for it on the left-hand side of the screenshot above); building the debug variant only gives me one activity, and building the extraActivity variant gives me two.
I had the same problem. By following the answer by Scott Barta, I simply created a folder named "debug" in the "src" folder and created an AndroidManifest.xml with the activity used only for testing. This way the activity is added in the debug variant without creating a new variant like in the Scott Barta's answer.

Unable to instantiate activity from library

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.

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/.

Categories

Resources