I want to create an Android studio project which contains 3 modules:
MyGameLib
MyGamePro
MyGameFree
I create an empty project, which has default module "app" and there is a default directory "app" in the project root folder. I add two additional module, MyGameLib and MyGameFree, so far everything is OK.
But when I tried to rename the modue 'app' to 'MyGamePro', only the module name is changed in Android studio. The folder name is still 'app'.
If I manually change the folder path name from 'app' to 'MyGamePro', the module becomes incorrect in the studio (becomes empty).
Please refer to the screenshot, I only changed module 'app' to 'MyGamePro', but in windows explorer, folder path is still 'app'.
Any idea how to fix this?
Have you tried to refactor the module as opposed to changing the filename, this will automatically change all the required files (e.g. Manifest) in the studio so they are not incorrect.
Right click the module name: app, Refactor -> Rename (to myApp, click ok)
If so a couple of good points are made towards the end of this article about management of adding multiple and priorities which may be of some help.
https://developer.android.com/sdk/installing/create-project.html#ReferencingLibraryModule
Related
I try to build and run the sample helloworld/mobile in project (https://github.com/android/car-samples)
My question is about how to select the sample app directory as described in README.md:
Select a sample app directory (e.g. helloworld/mobile) in the Project Structure UI on the top left. Each sample app has two build targets, one for the mobile platform and the other for the automotive platform.
<<<<
I went through Project Structure UI, I tried different possibilities but no way,
Build menu always shows Make Module car-simples-main and when I launch Make Module, nothing happens.
Wondering if something has changed in the last Android Studio version,
I use Android Studio Chipmunk | 2021.2.1 Patch 1
Anyone can help?
Thanks.
First of all, let's understand how the git directory is structured.
The directory has the two folders car-lib/CarGearViewerKotlin and car_app_library, which are gradle projects that can be built using gradle.
How do you know?
Any folder contains gradle wrapper and a build.gradle and settings.gradle files means that they're ready to be built using gradle, and these can contain modules inside of them.
Now helloworld/mobile is a module, or you can view it as a sub-project.
How do you know?
If you open up the parent's settings.gradle file, you will see the line include ':helloworld:mobile' meaning that the directory helloworld/mobile is a sub-project or module for the top level car_app_library project.
Modules by themselves are not runnable, they need to be contained in a parent project with top-level gradle files.
Now the solution for your problem should be: open up these two as projects in Android studio individually: car-lib/CarGearViewerKotlin and car_app_library, it will create a default run configuration for each project.
You won't be able to run helloworld/mobile alone, you'll need to run the parent project car-samples/car_app_library.
However, You should technically be able to open the root directory (the git directory containing all of the projects) and creating a run configurations by hand for each project, and you should be able to run the projects using one Android Studio window opened.
As a personal preference, I like to have my Android Application Project name in the UpperCamelCase format such as "MyAndroidApp" while its source code directory name in the lower-case format such as "/home/user/projects/myandroidapp".
This does not appear to be possible when creating a brand-new project named "MyNewAndroidApp", because Eclipse will save the source code under "/home/user/projects/MyNewAndroidApp" which is not what I wanted. Now if I use -> Project -> Refactor -> Rename, and replace "MyNewAndroidApp" with "mynewandroidapp", the new source code directory name will be good (i.e., in all lower cases), but the new project name will not be (i.e., also in all lower cases).
Neither does this appear to be possible with an existing project named "myoldandroidapp" whose source code directory name is "/home/user/projects/myoldandroidapp". I just can't change the project name to "MyOldAndroidApp" while keeping its source code directory name unchanged. I have also tried to modify file ".project" but still can't achieve my goal.
After some further investigation, I have noticed that:
the project name is tied to -> Properties -> Resource -> Path
the source code directory name is tied to -> Properties -> Resource -> Location
However, these project properties do not appear to be modifiable.
If it is not possible to change the project name while keeping the source code directory name unchanged inside Eclipse IDE, is it possible to achieve the goal outside of Eclipse IDE?
I think this is impossible because the project name is also the name of the source code folder in workspace (as default) or where you keep the source code. Any changes using "Refactor" in Eclipse also affect to folder name.
As naming convention, I suggest you should name your project name as UpperCameCase.
After some experimentation, I have found a way to achieve the goal when creating a brand-new Android project "MyNewAndroidApp":
Step 1: Create the source code directory first.
For example, type:
mkdir -p /home/user/projects/android/mynewandroidapp
Make sure that there is an extra directory level "android" between "projects" and "mynewandroidapp".
Step 2: Open Eclipse. Create project "MyNewAndroidApp" in the usual way, until arriving at page "Configure Project". Uncheck "Create Project in Workspace". In "Location", change the default substring "MyNewAndroidApp" to "android/mynewandroidapp". Finish the project creation.
Now the project name is "MyNewAndroidApp" while the source code directory name is "/home/user/projects/android/mynewandroidapp". The goal has been achieved.
Following are some limitations of this solution:
An extra directory level ("android") is needed. This may or may not be desirable.
For an existing Android project, achieving this goal also implies to add the extra directory level ("android") between the workspace and the source code sub-directory. Again, this may or may not be desirable.
I've read lots of "how-to" guides to importing libraries into Android Studio (now the only supported Android SDK, hmmpf) yet none of them explain what's actually going on during an import.
I can see projects that have Gradle build files everywhere and some that have none, why is this?
Half the stuff I get on Git-hub to play around with takes me more time to successfully import than the time it's saved me by using. My question is, what is required to successfully import and use your average library (project, not JAR or AAR) in Android Studio without having to refactor half the stuff?
Eclipse would allow me to use one single project (folder structure, not jar) as an "external" library and I needn't define what needs compiling where and when.
Android Studio keeps making copies of the project library inside my project folder then proceeds to complain about a whole host of things whilst it pollutes my project space with incorrectly named folders and packages (See screenshots)
Here's and incomplete import where I only imported the "core" folder
because I don't need this other blokes extra stuff - but it's missing
some definitions.
Here's a complete import of the whole folder (as you can see, samples
and all) but it's polluted my project folder and I don't need the
majority of it, and there's all these individual references now to
dependencies and things I don't think I should need.
How do I properly import a whole library with it's dependencies
without it polluting my current project?
Thanks in Advance!
EDIT
Ok here's what I have in one case of a library for an example.
The root folder of the zipped library - lots of garbage I don't need
What do I need to do to use this library? If I import the whole folder using Android Studio's "Import module" feature, it just makes a mess.
If I import just the "Core" folder it semi-works, but lacks some dependencies.
Why are there 3 individual gradle.build files?
Why are the required components in separate folders?
Should I just manually copy an arbitrary number of folders to my new "Libraries" folder under the root of my project?
Importing via File->Import Module
So the "sample" "module" (library..) requires the "layout" and "core" modules, that makes sense. But why if they're required aren't they all in the one module, using the same gradle.build file?
Now I've done that, I get this "Error:(23, 0) Could not read script 'C:\Users\me\AndroidstudioProjects\MyApp\gradle\scripts\gradle-mvn-push.gradle' as it does not exist."
It does exist at C:\Users\me\place_the_module_was_imported_from\gradle\scripts\ though, but
why if it's needed, was it not copied? Is this just bad Gradle...'ing?
Again, thanks!
I think I've found what seems to be the closest answer to this.
10 Gradle Basics
The addition of Gradle is going to be your largest hurdle during your transition over to Android Studio. Here are the basics of what you need to know:
Your Android Studio project will have a settings.gradle for the whole project
The settings.gradle file includes references to all modules which are included in the project and is generally updated automatically when you import or create a new module
Each Android Studio module will have its own build.gradle file
If one module depends upon another module, you will need to add that dependency in the build.gradle under the dependencies section
If your Module requries a jar file, it must be listed in that Module's build.gradle file
You may add remote dependencies to your project by listing them in a module's build.gradle file
From time to time, you will need to manually edit these gradle files Read more about Gradle over here.
Problem is that dependencies show up as modules in the UI so I now have a polluted workspace full of single-use libraries.
You can add the library via gradle.build and the necessary dependencies will be neatly added to your project without "polluting" your project. An example is the Facebook SDK
dependencies {
...
compile 'com.facebook.android:facebook-android-sdk:3.23.0'
}
As you can see from the image, the only place to know the external libraries am using is in the libraries folder of my project:
Add .jar file in your project
1 - Create libs folder under the app folder.
2 - copy your .jar file
3 - paste in app->libs folder
4 - after Click on File menu and select Project Structer option and open it
5 - Open one window in left side Modules under App folder select that folder
6 - In your window Top display 5 Option menu in select Dependencies option
7 - Then Right side display "+" "-" Sign click on "+" sign
8 - then open small dialog and choice "File dependencies" option
9 - then open another dialog here navigate .jar file in your libs folder and select
10 - click on Ok button and Apply And ok
add module project
1 - Click on File menu and select Import Module option
2 - then open one window here navigate your module and Finish
3 - Click on File menu and select Project Structer option and open it
4 - Open one window in left side Modules under App folder select that folder
5 - In your window Top display 5 Option menu in select Dependencies option
6 - Then Right side display "+" "-" Sign click on "+" sign
5 - then open small dialog and choice "Module dependencies" option
6 - then open another dialog here Module and select
7 - click on Ok button and Apply And ok
I picked the wrong name of a specific module which I imported to the Gradle project in Android Studio.
In this face I want to rename module Facebook1 to Facebook.
Is this possible in Gradle project and how to do it?
Android Studio >= 1.4.1
Right click the module -> Refactor -> Rename.
Enter the new name of the module, click OK.
Older versions of Android Studio
The following worked for me, tested in Android Studio (AS) 1.1 & 1.0.2, directly after importing a project.
Under the 'Android' tree view (ATV),
Right click the module name: app, Refactor -> Rename (to myApp, click ok)
Open settings.gradle
Change ':app' to ':myApp', and save
You will be prompted to 'Sync Now', do it (the prompt is a blue link above the file)
(Your project will disappear from ATV, but don't worry, just exit AS)
From a file manager (outside of Android Studio), open your project root directory, and rename the folder app to myApp
Re-open your project in AS, and you will see your module and Gradle scripts re-appear under ATV! :)
Congratulations - your module has now been renamed! (whew, time for some tea)
You can do it manually, with or without changing the folder structure (consider not change it for a minor impact on version controlling)
Changing folder name accordingly
1- open settings.gradle and change the name of your module (exemple from ':facebook1' to ':facebook')
2- change the folder name accordingly (from xxx/facebook1 to xxx/facebook)
3- If other Modules have dependencies on it: open the corresponding build.gradle and change dependency (from compile project(':facebook1') to compile project(':facebook') )
Without changing folder name
1- open settings.gradle and change the name of your module (exemple from ':facebook1' to ':facebook')
2- add a new line on "settings.gradle":
project(':facebook').projectDir = new File(settingsDir, '/facebook1')
3- If other Modules have dependencies on it: open the corresponding build.gradle and change dependency (from compile project(':facebook1') to compile project(':facebook') )
In the project view on the left in Android Studio,
right click on the module whose name you want to change
select 'refactor' -> 'rename'
choose the rename module option
follow step 1 - 2 and then rename the directory too
Android Studio 1.3.2,
1. switch Project view on the left in Android Studio
2. right click module which want to change -> Refactor -> Rename
3. do both "Rename directory" and "Rename module"
4. open settings.gradle, change module name
5. Build -> Clean Project
As of Android Studio 3.4.1
Rename module
In Project window, right-click module and select Refactor > Rename
Choose Rename module and press OK
Enter new module name and press OK
Important settings.gradle is updated automatically. However, you must manually update any build.gradle dependencies to reflect new module name:
dependencies {
// manually update this after renaming the module
implementation project(':newmodulename')
}
Rename package to reflect new module name
In Project window, right-click desired package and select Refactor > Rename
If there are other src sets with same package name (test or androidTest), a dialog will ask if you want to rename them as well. If so, select Rename package
Enter new package name and press Refactor
Select Do Refactor in the Refactoring Preview
Tip: Select File > Invalidate Caches / Restart to fix any unresolved resource references caused by renaming a module.
In AndroidStudio 2.2.2 renaming the module alone via refactor/rename worked for me. (32-bit linux Mint)
The rename dialog presents two options: "directory" or "module" (radio buttons so mutually exclusive, at least in one pass). I wasn't sure which to pick so I ended here looking for an answer. One answer said "do both" as two steps, another answer said "do module, then directory" as two steps. So I chose "module" first. To my surprise that was all that was needed, both module and directory were changed. settings.gradle was updated by the refactor/rename in the one step as well.
"gradle synce" and "clean build" were also triggered. Upon build to my tablet, no complaint about the name change.
just adding this answer in case someone else with 2.2.2 or later is unsure which to pick. Also to re-iterate what others have said, back up your project first.
Close Project and rename module folder oldName to newName then open Project and change module name include ':oldName' to include ':newName' in settings.gradle
This is working for me in Android Studio 1.2.1.1
Premise: I do NOT use the refactor function of Android Studio to rename the module
These are the steps I perform:
Rename the folder of the module (directly in the file system)
Modify the setting.gradle file accordingly (directly in the file system)
Open the project in Android Studio
That's all.
Different people seem to have success with different methods, but when I wanted to rename my project from:
com.example.myname.projectname
to
com.company.projectname
I tried everything and the only way I got it to work in the end was:
1) Exit Android Studio
2) back everything up! (find your project folder and make a copy of it)
3) Find and replace all instances of com.example.myname.projectname with com.company.projectname
Note that there are five places (actually it probably depends on your project, I had five) where you need to rename folders, as it creates subfolders called com\example\myname\projectname:
project\app\src\main\java
project\app\src\androidTest\java
project\app\build\generated\source\buildConfig\debug\
project\app\build\generated\source\buildConfig\test\
project\app\build\generated\source\r\debug\
Also, in project.idea\ there is a file called workspace.xml - in that file you'll need to find the text com\example\myname\projectname
Otherwise, do a search through the project directory to find "com.example.myname.projectname" and replace with "com.company.projectname"
4) Open Android Studio again. if the initial project directory name is the same (possibly if the project name itself is the same?) it will reopen if you had it open last time, otherwise you should be able to open as normal.
5) Go to File - Project Structure (CONTROL+ALT+SHIFT+S).
6) Under "Flavors", change the application ID to the new name.
7) Excit Android Studio and restart it.
8) Now you should be able to build your project.
9) Go have a drink, that was a lot more effort than it should have been!
One really important note, you can only have ASCII letters in your package name (you may be able to have numbers, but not as the first character). Java itself allows numbers to start a package name, but Android does not: Package names in Android studio when dev name starts with a number? I was trying to rename
My workaround (and from a look around the apps on my phone it seems to be what others are doing) is if you wanted a number at the start of either your company name or app name, write the word - instead of com.123company.55project perhaps use com.onetwothreecompany.fiftyfiveproject
When initially trying make a copy of an Android Studio project, I duplicated the project folder in finder, renamed it, brought it up in AS using "Import Project," refactoring the package and editing the manifest. At first first it seemed to run on my device without any issue.
I later noticed, however, that every time I ran either the original or the duplicate app on my device, one would replace the other's icon on the home screen.
I found a solution that said not to use "Import Project" but instead to use "Open Project" to open the duplicate. Doing so solves the initial problem of the app replaced with the other app, but opened a whole new can of worms where some of the pointers in the duplicate project refer to files and file paths in the original project. Apparently this is a known bug. I have not confirmed this with Google, however.
Searching for solutions only lead to "you need to use "Import Project" not "Open Project," which of course takes me back to the original problem. Any ideas?
Android uses the applicationId in the Gradle build file to set the package attribute in AndroidManifest.xml; this string is used by Android devices to disambiguate applications. If you install an app it will replace any other installation with that package ID. Change the value in Project Structure > Flavors > defaultConfig, or modify it directly in your build file.
Note that if you try to change it directly in AndroidManifest.xml, it will be overwritten at build time with what's in build.gradle; you need to modify it in the build file.
Try deleting *.iml in duped dir then import.
Instead of trying to copy the project outside of Eclipse, try copying it IN Eclipse, like this:
Copy and paste your project (ctrl+c and then ctrl+v)
Change the package name in your Manifest.
Rename your package name with refractor by right-clicking on the package.
Change your application name in your Manifest.
This will create a new source folder for the project and it will already have the new project in your Eclipse.