How to build two Android apps in one Android Studio project? - android

I have a project in Android Studio. I would like to be able to build two Android apps from the same project. For example, the project contains two files: ActivityA.java and ActivityB.java.
I would like to be able to build one Android app where ActivityA is the main launcher and another Android app where ActivityB is the main launcher. When building the project, I'd like it to generate two .apk files. Is this possible? What would I need to configure to make ActivityA the main launcher for app #1 and ActivityB the main launcher for app #2?

Create two folders/flavors AppA and App2 similar to the picture below:
They both have a manifest like so:
<activity
android:name="com.onegravity.testapplication.ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Make sure that the manifest in the main project folder doesn't define a launcher Activity.
If you use the same package name for the two apps then defining those two flavors in the Gradle script becomes simply:
productFlavors {
AppA {}
AppB {}
}
(put that below the buildTypes block).
Please read this to get a good understanding of flavors and what you can do with them:
http://developer.android.com/tools/building/configuring-gradle.html#workBuildVariants
http://developer.android.com/tools/building/manifest-merge.html

Sounds really messy.
I can see building two applications from a common code base but since the
manifest only accepts one application element you would have to change the manifest when you build.
If the problem is a common code base why not create a library with the common code and then import the library as a module in two separate projects.

Related

Could not identify launch activity: Default Activity not found after upgrading to Android Studio 4.0

I've been having this problem since upgrading to Android Studio 4.0 on macOS. When I try to start my app from Android Studio, I get the following error:
Could not identify launch activity: Default Activity not found
Error while Launching activity
What's a bit unique about my project is that the default activity is defined in the manifest of another library used by my project, not the project itself. This was all working fine before the upgrade, but now it isn't for me. When I open the project containing the dependency, it builds and runs fine. I've already tried the following steps:
clean the project, rebuild
invalidate caches and restart
reinstall Android Studio
delete all generated files and folders (build, gradle, .idea, etc)
You can solve the problem as follows:
Click your Module Selection window,then click Edit Configurations…
Then set Launch Options to Nothing; that's okay:
To dovetail off of Mike N.'s comment, it looks like it is an issue to be resolved in the next point release: https://issuetracker.google.com/issues/158019870
For the details of the quick fix, for me I looked at the Merged Manifest tab, which is at the bottom-left of the AndroidManifest.xml pane. This shows all of the library manifests combined with your activity's.
The dialog said an error occurred where browser:1.0.0 manifest had a minSdk of 15 whereas all the rest of my minSdk are 14. I clicked on:
use a compatible library with a minSdk of at most 14, or increase this
project's minSdk version to at least 15.
Which brought up my minSdk to 15, and the error with the launcher went away, it will now insta-launch on my device. So I would sit tight for AS 4.0.1 but in the meantime, check the Merged Manifest.
For me this was happening in a project in which the main activity was declared in the manifest of an imported module (e.g. not in "app" module).
The solution for me was to add again the activity declaration in the manifest file of my top project:
<activity android:name="com.cristian_slav.elements.MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
On my side it was a issue with the manifest. You can detect this type of issue by watching the merged manifest tab. For more details you can watch the link shared by Mike N. in the first comment.
The problem will arise in new projects when one forgets to tick the default activity checkbox during adding anyone of the first or subsequent new activity.
This can be rectified at later stage by going to Run> Edit Configurations > Launch : in which change to specified activity and select the activity you choose to keep as the first page on launch of app.
Try these steps:
Close Android Studio
Go to
C:\Users\my_user_name.AndroidStudio4.0\system\caches
Delete "caches" folder
Relaunch Android Studio
set Launch Options to (Nothing)

Why do I get two shortcuts of my app, with different behaviour for each?

In android studio (1.0.2), I made a android library on its own project : I've just created a new project and changed the build.gradle, replacing apply plugin 'com.android.application' with 'com.android.library' (github repository). So building it I am ending with a .aar file. Notice that I am also trying to use Kotlin language in order to build it.
Then I imported this aar library file into an existing android project of my own (github repository), adding adjustement to the app build.gradle.
Though it works, at least on my android Emulator (x86 image of Jeally Bean api : 4.2.2), I got a strange behaviour :
In the application list, I got two shortcuts for my new application
But also, while a shortcut just starts with the library main activity, the other starts my owner ChessPositionManager activity, and goes immediatly inside the contained FileExplorer activity.
So, is that behaviour of having two different icons with a little different behaviour is explicable ? Can I avoid it and at the same time keep the fact that I am coding the library and my application separately ? (Indeed, a library module can easily be added to any project, but it is still coupled with this project, as far from what I've understood).
I assume that your library file has a manifest declaration making it show up in your launcher. See if your library manifest has:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If it does that is why its showing up twice, that intent filter signals to android you want it to show up.
Also, I suggest taking a look at http://developer.android.com/guide/components/intents-filters.html

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.

Default Activity not found in Android Studio

I just upgraded to Android Studio 0.2.8 and I am getting an error that says "Default Activity not found" when I try to edit the run configurations.
When I launch Android Studio I get this error "Access is allowed from event dispatch thread only"
The activity I am using is a fragment activity.
So far I've tried rebuilding and invalidate caches/restart. Both were of no use.
Please let me know what I can do to fix the problem.
Have you added ACTION_MAIN intent filter to your main activity? If you don't add this, then android won't know which activity to launch as the main activity.
ex:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="com.package.name.MyActivity"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In Android Studio, right click on the project and choose Open Module Settings. Then go to the Sources tab in your module, find the src folder, right click on it and mark it as Sources (blue color).
EDIT: There is no sources tab in later versions of Android Studio, but you can edit the build.gradle file instead: https://stackoverflow.com/a/22028681/1101730 (thanks for comment Josh)
I had the same problem while importing a project into Android Studio, probably caused by the fact that the project has been developed on an earlier version of Android Studio than currently installed on my computer.
What solved it was simply choosing:
File -> Invalidate Caches / Restart...
and then selecting Invalidate and Restart.
If you don't have the tab and you started with an empty activity try this.
Below is a sample code example:
<application android:label="#string/app_name">
<activity android:name=".HelloActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Now go to your AndroidManifest.xml file. Next copy the intent filter from this code. Look at your manifest file really good and paste the intent filter in the exact place it is in the code above. (after the .yourActivityName> part of the manifest.) I hope this helped.
If you are still getting an error that says "Default Activity not found" when you try to edit the run configurations even after executing:
Invalidate cache and Restart.
Then try deleting the Settings/Preferences folder:
/< USER_HOME_DIR >/.AndroidStudioPreview3.2
or on Mac :
/Users/<USER_NAME>/Library/Preferences/.AndroidStudioPreview3.2
When I clicked "Open Module Settings", there was no "Source" tab, I think because that's been removed for newer versions of Android Studio (I'm on 0.8.14). So I had to do this instead:
Add these lines to the build.gradle file inside the android { ... } block:
android {
...
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
After editing the file, click Tools > Android > Sync Project with Gradle Files.
Credit to this answer and this comment.
Following did the trick for me. From Run -> Edit Configuration.
My problem came down to an additional error that was popping up sometimes, stating
Manifest Merger failed with multiple errors in Android Studio
What ultimately was causing my problem was the fact that there were multiple manifests (a debug Manifest, and a manifest in the new module I had just imported), and they were not merging correctly due to this. After seeing this answer, I was able to analyze the merged manifest and find the cause of the issue and fix it.
Press app --> Edit Configurations
After that change value in Launch on "Nothing"
this happened to me because I capitalized the paths in the manifest. changed:
<intent-filter>
<action android:name="ANDROID.INTENT.ACTION.MAIN"/>
<category android:name="ANDROID.INTENT.CATEGORY.LAUNCHER"/>
</intent-filter>
to
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
and it was fixed
I ran into the same issue today and was going through the answers here.
For me the difference was that yesterday it worked fine, so I figured it could not really be a configuration issue, neither configuration nor Version of Android Studio changed.
Fortunately, I tried a restart of Android Studio before trying any of the other answers, and luckily that solved the issue.
So for all people out there who run into this: first thing to try is to restart Android Studio and only if that does not solve the issue try the other answers here.
I just experienced the same error in Android Studio 1.5.1. and just found the source of the problem. I am not sure whether the cause was a human error or some strange glitch in the behaviour of the IDE, but none of the existing StackOverflow questions about this subject seemed to show anything about this so I figured I post it as an answer anyway.
For me, either one of my team members or the IDE itself, had changed the launcher activities manifest entry, causing it to look like this:
<activity
android:name="com.rhaebus.ui.activities.ActivitySplash"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<id android:name="android.intent.id.LAUNCHER" />
</intent-filter>
</activity>
While it should, in fact, look like this:
<activity android:name="com.rhaebus.ui.activities.ActivitySplash"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <!-- Change Here -->
</intent-filter>
</activity>
So please double, triple, quadruple check the format of your launcher activity in the manifest and you might be able to save yourself some time.
Hope this helps.
EDIT:
I strongly suggest people not to go with the answers that suggest to manually select a launcher activity inside the configuration options of your module, as this caused the application to no longer be shown in the list of installed apps on both the Samsung Galaxy S5 Neo and the Samsung Galaxy S6 (at least for me).
You can get Android Studio not to complain by going to the "Edit Configurations" menu (tap "Shift" three times, type "Edit Configurations"), then change Launch Options > Launch to "Nothing".
I'll probably add a generic activity containing instructions, just to avoid any confusion.
Please make sure in manifest that package name is same with your main activity
In Android Studio switch to Project perspective (not Android perspective).
Make sure that your project follows the gradle plugin's default structure (i.e. project_dir/app/src/main/java...)
Delete all build folders and subfolders that you see.
In the toolbar click Build -> Clean Project, then Build -> Rebuild Project.
Try to run the project.
In my case, it worked when I removed the .idea folder from the project (Project/.ida) and re-opened Android Studio again.
Right click on the project and choose Open Module Settings.
Go to the Sources tab in your module.
Find your src folder.
Right click on it and mark it as Sources.
This solved the error in my case.
If you changed name of directories (class structure) for example com.dir.sample to com.dir.sample1, after that don't forget to change package com.dir.sample to com.dir.sample1.
Modify "Workspace.xml" (press Ctrl + Shft + R to search it)
Modify the activity name with package name
Make sure to change "name="USE_COMMAND_LINE" to value="false"
Reload the project
Done!
For those like me who were struggling to find the "Sources tab":
Here you have to mark your "src" folder in blue (first click in Mark as: Source, then in your src folder), and you're good to go.
There are two steps you can take:
Go to configurations and enter the name of activity to be launched
If it is still not working Disable Instant Run
I have tried all solutions, but not working at all.
than I have tried to disable Instant run in my android studio.
Go to Android Studio Settings or Preferences (for MAC) -> Build,Execution,Deployment -> Instant Run.
uncheck the Instant run functionality and than after click sync project with gradle files from file menu
now run your build...
In some case you can have model with some category field, if you will refactor it in all places, it may refactor it in manifest file and then and the tag xml will became invalid.
Many possibilities can be happened for this kind of case (Default Activity not Found)
If all code are fine, especially on Manifest, just just need to Invalidate Cache and restart the studio,
But sometime it is happened when you have duplicate activity declaration on manifest, not only for the Main activity, it is also triggered by child activities as well.
That happened with me, Default activity not found because i have duplication child activities on manifest, after deleted that, everything is well
Check if there is any duplicate tag in your AndroidManifest.xml file.
I figured it out. I mistakenly added final keyword in activity declaration. Once I removed it everything works!
public class SplashActivity extends AppCompatActivity {
...
}
In my case, this happened because of sudden shutdown of the system.
I followed these steps:
Close Android Studio
Go to
C:\Users\my_user_name.AndroidStudio4.0\system\caches
Delete "caches" folder
Relaunch Android Studio
In Android Studio
Go to edit Configuration .
Select the app.
choose the lunch Activity path.
apply, OK.
Thanks!!
Default Activity name changed (like SplashActivity -> SplashActivity1) and work for me

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.

Categories

Resources