How to Build Sample Android Location App from GitHub - android-studio

I downloaded the Main branch of the Sample Android Location App from GitHub as a ZIP file.
https://github.com/android/location-samples
Once on my computer, I unzipped the file to a subdir.
Then in Android Studio (4.1.1), I imported the project by specifying the unzip'd dir.
It opens fine, but I am unsure how to build it.
In addition, Studio editor shows several errors such as "Cannot resolve symbol 'AppCompatActivity'".
Not sure how to proceed now to build, test and debug. I am sure it is something simple I am overlooking. I checked the ReadMe. This is the first time I have attempted to download and Build a project from GitHub Android Samples, so just need a couple pointers.

After unzipping the file, you can import it to AS
Choose the dirrectory with Android icon to open the project
Then it will download the dependencies, jars, SDKs, etc. After downloading, you can refer to the upper right side, where you can choose which App to build, then click the green arrow, gradle will take care of the rest.
If it didn't show the green arrow, the gradle sync might have failed, and you can sync the project by click this

Related

Android Studio: file-> "Project Structure" missing options to change application id, version, version name, etc

So every time i updated my app, i use file -> project structure to increase the version and put version name. I also use it to name application id when i want to fork my codes into different app.
but today I can't find application id, version, version name, etc on file-> "Project Structure".
a few months ago i also got this problem and the options suddenly come back by itself.
so I don't know how to fix it. I need those options...
What I've tried
loading old projects
refreshing Gradle files and sync
rebuilding the project
If the "Sync Project with Gradle" does not appear, open the Android folder separately.i.e like an independent project. File->Open. In my case, I was working on a Flutter/Kotlin project. When I opened the Android folder it automatically started syncing and downloading the required gradle files.
oh my God, fixed it by
File > Sync Project with Gradle
File > project structure > modules (will still be blank)
File > project structure > suggestion -> click APP (bottom right will say "loading" with tiny fonts)
File > project structure > modules (now the options will appear)
I don't see "Sync Project with Gradle", but I saw "sync with file system". This worked once, but now Project structure is gone again. Clicking "sync with file system" doesn't work anymore.
Try the keyboard shortcut, for example on Windows is "ctrl+alt+shift+S"
I fixed by Open module settings (Right-click the project -> Open module settings) change some value and apply it
then the project structure is showing up

"Generate Signed APK" option is not display in android studio Build menu

"Generate Signed APK" option is not display in android studio Build menu,
What is the problem or something I missed to install?? Thanks
When I click "Build APK(s)" , the android studio is no response
For me, clicking File > Sync Project with Gradle File System solved it for me.
Firstly check with clean > rebuild project and see that 'app' is not having "red X" icon.
Check your gradle that you had not configured it with release config yet.
There must be a signing config in gradle file
Follow Below Steps for Nice and Easy Way:-
Go to Tools->Flutter->Open for editing in android studio.
open for editing in android studio
Choose separate window. This will open the android part of your project in separate window.
Wait to Gradle to finish it's build.
Go to Build-> Generate signed bundle/apk.
Follow this video for further process.
Thank me later..!
in my case, one of my harddisks hard stopped working (in which I had my first project).
File > Settings > Appearance & Behaviour > Build, Execution, Deployment > Build Tools > Gradle menu, changing the Gradle user home to another folder solved the issue. the original value of it consisted path to my first project which wasn't existing anymore.
I had the same issue not having the "generate signed bundle/apk" enabled in Android Studio interface and this thread helped to deduce what was my issue... I'm new to android development so I didn't know what to do at all but checking the build.gradle file I realized that there were issues with the synchronization of gradle, so I found this video, downloaded the latest version of Grandle, then went to File > Sync with Fyle System, and after the sync was completed the button appeared.... so seems like this has a direct relation to the right synchronization of gradle.

AndroidStudio doesn't recognize R

I am teaching android app programming at a highschool. I found Eclipse to fragile so we are now trying out Android Studio. But a couple of students get that Android Studio with R. in red font, showing that it is not recognized.
This happened for example when the student copied the contents of my files.
It sometimes happens when you open a proyect from other sources. Just Synchronize it or press Ctrl-Alt-Y.
R.java file issues : 1) check the naming convention of images in drawable folder with size.
2) check the style of your application
In your case it may be missing style .. So upadate the style in value folder as per application .
The most common reason for this error is bad XML coding. For example using android:src="#drawable#drawable/picture" in ImageView will give you R error as it will not let R.Java file to build. Its hard to trace such errors as XML file will not show any error.
Also take a look if your resources are of good quality!
Hope it helps! Cheers
Build -> Clean Project
Tools -> Android -> Sync Project with Gradle Files
and all OK!
Try adding the following:
android.enableAapt2=false
to gradle.properties and Refresh:
You can also do the following: Clean Project and Sync Project with Gradle Files.
try importing your project :
To migrate existing Android projects, simply import them using Android Studio:
In Android Studio, close any projects currently open. You should see the Welcome to Android Studio window.
Click Import Non-Android Studio project.
Locate the project you exported from Eclipse, expand it, select the build.gradle file and click OK.
In the following dialog, leave Use gradle wrapper selected and click OK. (You do not need to specify the Gradle home.)
Android Studio properly updates the project structure and creates the appropriate Gradle build file.
First if the existing project was developed in eclipse you need to import the project and then check what are the error. If your students are coping codes from your project then after coping the codes they need to clean and sync the project. If still your problem not solved try this in android studio :-
File/invalidate caches/restart.
Hope it helps you :)
Especially, for regeneration of R.Java file.
If you have tried all these options:
Clean Project
Rebuild Project
Invalidate Caches / Restart
deleted .gradle folder
deleted .idea folder
deleted app/build/generated folder
checked your xml files
checked your drawables and strings
and still face problem then check your classpath dependency in your Project Gradle Scripts and if it's, this:
classpath 'com.android.tools.build:gradle:3.3.2'
then downgrade it to, this:
classpath 'com.android.tools.build:gradle:3.2.1'
clean and rebuild it will sync your project.
I had the same issue, and the lint gave me a hand...
If you're using lint, it can help too.
In gradle.build(Module:app) add the lintOptions sub-element in android element as shown below, then lint will fix it for you!!.
android {
lintOptions {
abortOnError false
}
}
I hope it helps!!

Copy existing project with a new name in Android Studio

I would like to copy my Android project and create a new project from the same files just with a different name. The purpose of this is so I can have a second version of my app which is ad supported in the app store.
I found this answer here:
Android - copy existing project with a new name
But it's for Eclipse. How can I do this in Android Studio?
The steps in the link you specified should also work for Android Studio. Just make a copy (using a file manager) of the entire module folder and give it a new name. Now open it up and use Refactor -> Rename (right click on the item you want to rename) to rename your module and package.
See this for details about refactoring in IntelliJ/Android Studio.
If you are using the newest version of Android Studio, you can let it assist you in this.
Note: I have tested this in Android Studio 3.0 only.
The procedure is as follows:
In the project view (this comes along with captures and structure on the left side of screen), select Project instead of Android.
The name of your project will be the top of the tree (alongside external libraries).
Select your project then go to Refactor -> Copy....
Android Studio will ask you the new name and where you want to copy the project. Provide the same.
After the copying is done, open your new project in Android Studio.
Packages will still be under the old project name.
That is the Java classes packages, application ID and everything else that was generated using the old package name.
We need to change that.
In the project view, select Android.
Open the java sub-directory and select the main package.
Then right click on it and go to Refactor then Rename.
Android Studio will give you a warning saying that multiple directories correspond to the package you are about to refactor.
Click on Rename package and not Rename directory.
After this step, your project is now completely under the new name.
Open up the res/values/strings.xml file, and change the name of the project.
Don't forget to change your application ID in the "Gradle Build Module: app".
A last step is to clean and rebuild the project otherwise when trying to run your project Android Studio will tell you it can't install the APK (if you ran the previous project).
So Build -> Clean project then Build -> Rebuild project.
Now you can run your new cloned project.
If you use Gradle - don't forget to change applicationId attribute in app/build.gradle file.
As free3dom pointed out, here's what should be done:
Create a copy using file manager
Manually edit the app's build.gradle file to change the package name (you can use the file manager).
Manually edit AndroidManifest.xml to change the package name.
Run gradle sync.
Open the project in Android Studio, and refactor the package name.
Run gradle sync, again.
That seems to work without any problems.
This is a combination nt.bas's answer and step 9 of Civic's answer with visual examples because it took me a while to find out what was intended since I am new to Android Studio. It has been tested in Android Studio 3.2.1.
Open the project you want to clone in Android Studio. (In this example, the old project name was test5 and the new project name was test6)
In the left file-overview pane, click: Project (where it might currently say android).
Right mouse button click on the project within the file explorer pane and click refactor>clone.
Change the "New name" to your new project name and click ok.
File>open>New window>Select your new project>Open in new project window. In the new window, wait until the bottom line of Android studio is finished/says:"Gradle Sync Finished".
In the file overview pane: right mouse button click (RMB) on: app.java/< your old project name> (not the com.example.<your old project name>(androidTest) one, not the com.example.<your old project name>(test) one, just the blank one)
Enter the new name of your package and select both checkmarks, click refactor.
In the bottom left bar click "Do refactor".
Open app/res/values/strings.xml and change name of the old project (e.g. test5) to the new name of the project in line:
<string name="app_name">test5</string>
Open Gradle scripts/build.gradle (Module:app) and change the line to the same line with your new project name:%fig4
applicationId "com.example.a.test5"
A yellow line will appear at the top of your code pane, requesting gradle sync. Press "sync now".
in top bar, press build>Clean project.
If it says "Gradle build finished" in the bottom left, you click "Build>Rebuild project".
Now you should be able to compile and run your project again (if it worked in the first place).
The purpose of this is so I can have a second version of my app which is ad supported in the app store.
Currently the best way to do it is without copying the project.
You can do it using diffent flavors in your build.gradle file.
productFlavors {
flavor1 {
applicationId = "com.example.my.pkg.flavor1"
}
flavorAdSUpport {
applicationId = "com.example.my.pkg.flavor2"
}
}
In this way you have only a copy of the files and you can handle the difference easily.
I'm following these steps and it's been working so far:
Copy and paste the folder as used to.
Open Android Studio (v3.0.1).
Select Open an existing Project.
Close the message that will pop up with title: "Import Gradle Projects".
At left side on Android Tab go to: app -> java -> select the first folder (your project folder)
Refactor => Rename... (Shift + F6)
Rename Package, Select both options - Put the new folder's name in lowercase.
Do Refactor
Select: Sync Project with Gradle Files at toolbar.
Build => Clean Project
Go to app -> res -> values -> strings.xml, and change the app name at 2nd line.
In Android Studio 4.0 you need only these few steps:
in File Manager copy the project directory and rename the new one
enter in it and change applicationId inside app/build.gradle
open the existing new project in Android Studio
open one class file and highlight the package name part to change (e.g. from com.domain.appname to com.domain.newappname highlight appname)
right click on it -> "refactor" -> "rename"
choose "rename package"
in the dialog choose "Scope: all places" and click "preview" or "refactor"
The appendix of the Android Developer Fundamentals Course Practicals gitbook includes steps to copy and rename an existing project:
https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-practicals/content/en/appendix_utilities.html#copy_project
I've tried from nt.bas answer and gnyrfta answer which works well for me.
Quoting from nt.bas answer:
If you are using the newest version of Android Studio, you can let it assist you in this.
Note: I have tested this in Android Studio 3.0 only.
The procedure is as follows:
In the project view (this comes along with captures and structure on the left side of screen), select Project instead of Android.
The name of your project will be the top of the tree (alongside external libraries).
Select your project then go to Refactor -> Copy....
Android Studio will ask you the new name and where you want to copy the project. Provide the same.
After the copying is done, open your new project in Android Studio.
Packages will still be under the old project name.
That is the Java classes packages, application ID and everything else that was generated using the old package name.
We need to change that.
In the project view, select Android.
Open the java sub-directory and select the main package.
Then right click on it and go to Refactor then Rename.
Android Studio will give you a warning saying that multiple directories correspond to the package you are about to refactor.
Click on Rename package and not Rename directory.
After this step, your project is now completely under the new name.
Open up the res/values/strings.xml file, and change the name of the project.
A last step is to clean and rebuild the project otherwise when trying to run your project Android Studio will tell you it can't install the APK (if you ran the previous project).
So Build -> Clean project then Build -> Rebuild project.
Up to this point you only rename your whole project name. To rename packaging name you need to follow gnyrfta answer which was described as:
When refactoring the package name in Android Studio, you may need to click the little cogwheel up to the right by the package/android/project/etc - navigator and uncheck 'compact empty middle packages' in order to see each part of the package name as an own directory. Then for individual directories do refactor.
PS: If you're having an
Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split
lib_slice_0_apk was defined multiple times
Just delete build folder of appmodule and Rebuild the project!
This will fix the issue!
Go to the source folder where your project is.
Copy the project and past and change the name.
Open Android Studio and refresh.
Go to ->Settings.gradle.
Include ':your new project name '
When refactoring the package name in Android Studio, you may need to click the little cogwheel up to the right by the package/android/project/etc - navigator and uncheck 'compact empty middle packages' in order to see each part of the package name as an own directory. Then for individual directories do refactor.
This is important if you need to change all parts of the package name. For example, from com.example.originalproject to org.mydomain.newproject. Otherwise, the refactor/rename operation will only let you change "originalproject" to "newproject", and it will leave "com.example" unchanged. There is a good video that shows this: https://www.youtube.com/watch?v=dMK-RBVLeIY
Perhaps this will help someone.
For Android Studio 4.x Projects, you need following steps:
copy project directory to new project directory
from Android Studio, open new project directory
edit settings.gradle file by updating the rootProject.name='newProjectName'.
then sync gradle
and here you go the project is ready, and you can start updating manifest, packages, google-services.json and all other stuff
When you copy your project you will also need to delete the original remnant intermediate build (someActivity$4.class) files from the C:...\AndroidStudioProjects(project_name)\app\build\intermediates\classes\release... directories.
Otherwise you will almost certainly have build failures for the new project
if yo attempt to compile the copied project. Refactoring won't solve this.
I'm using Android 3.3 and that's how it worked for me:
1 - Choose the project view
2 - Right click the project name, which is in the root of the project and choose the option refactor -> copy, it will prompt you with a window to choose the new name.
3 - After step 2, Android will make a new project to you, you have to open that new project with the new name
4 - Change the name of the app in the "string.xml", it's in "app/res/values/string.xml"
Now you have it, the same project with a new name. Now you may want to change the name of the package, it's described on the followings steps
(optional)
To change the name of the package main
5 - go to "app/java", there will be three folders with the same name, a main one, an (androidTest) and a (test), right click the main one and choose format -> rename, it will prompt you with a warning that multiple directories correspond to that package, then click "Rename package". Choose a new name and click in refactor. Now, bellow the code view, here will be a refactor preview, click in "Do refactor"
6 - Go to the option "build", click "Clean project", then "Rebuild project".
7 - Now close the project and reopen it again.
Requirement and test on Android Studio 3.5
Make sure your old project working properly with your existing android studio library.
Copy project directory and rename folder for new project name e.g. Bramara
Open your existing new project using Android Studio 3.5. After opening project complete, Navigate to 1: Project -> Project. You may seen your project only has two directory pointing to new project folder and old project folder. Close your project.
Edit appl.iml on directory new project -> app -> appl.iml using text editor. Replace all old project name into new Project Name.
Reopen your exising new project. Navigate to 1: Project -> Project. You may seen your project only has one directory.
Navigate to 1: Project -> Packages. right click on your component -> Refactor -> Rename.
A Warning message will pop up. Make sure all change will apply to new project folder! After that choose Rename packages e.g. com.dedetok.bramara.
Navigate to 1: Project -> Android. Open app -> manifests -> AndroidManifest.xml. Fix Application Activity to new pakage name. Change your Application Name to a new one.
Open Gradle Scripts -> build.gradle (Module: app), change your applicationId to new project, e.g com.dedetok.bramara and sync project.
Clean and rebuild your new project.
Your new project is ready to edit/change.
Note: if adb run showing activity not found, edit your Run/Debug Configuration. Module should point to module application e.g. app.
As of February 2020, for Android Studio 3.5.3, the simplest answer I found is this video.
Note 1: At 01.24 "Find" tab appears below. Click "Do Refactor" and continue as in the video.
Note 2: If you have any Java/Kotlin files "Marked as Plain Text" you need to modify the package name at the top manually, i.e. package com.example.thisplaceneedstobemanuallyupdated
Note 3: Be careful about letter cases while renaming, just as in the video.
Note 4: If you want to update the project name on title bar of project window, modify rootProject.name = 'YourProjectName' inside "settings.gradle" file under "Gradle Scripts" directory.
The EASIEST (and definitely the quickest) way to do requires WINRAR, 7zip or similar archiving software:
Find the project folder in windows explorer - double click to open this folder.
Create a new folder and name it "Backup."
While still in the project folder, select all files / folders, except the "backup" folder.
Right-click and select "add to archive" or "create archive" (command will be different depending on your archiving software)
Name the archive and click ok.
Move this archive to the "Backup" folder.
You're Done - to open the backup archive, open "Backup" folder and right-click on the backup file. Select "Extract" or create a new folder to which the files will be extracted and hit "ok" then open the project as you normally would from Android Studio, etc.
I had problems with this following:
https://google-developer-training.github.io/android-developer-fundamentals-course-concepts-v2/appendix/appendix-utilities/appendix-utilities.html
on Android Studio version: 3.3.2
until I killed the .idea/workspace.xml file.
$ cp -rv Testcopysource/ TestCopyDest
$ rm TestCopyDest/.idea/workspace.xml
$ stdio.sh & # Run Android Studio on Linux
Prior to doing that Android Studio would still point to the original source folder and all renames were applied to the original source files (within Testcopysource in my example above).
In android studio 4.1.1:
Step 1
You copy the project in the file explorer and give it a new name.
Step 2
Open the copied project in the android studio and go to the Gradle Scrips files and change the name of the project to the new name in the settings and build files.
Step 3
Go to the properties Gradle file and add the line:
android.overridePathCheck=true
The simplest way would be to upload the project files to a Github repository and cloning or downloading the repo again to your computer

Android Studio: disabling "External build" to display error output create duplicate class errors

I am starting my migration from Eclipse to Android Studio, and start playing with new projects on Studio.
My test project was working fine till I got some errors messages.
I had to do some manipulation (https://stackoverflow.com/a/16876993/327402) to enable the error output to display, and found the issue that I fixed.
Unfortunately, after this "workaround" (Why the hell have I to make such things to see my errors?), I found that there was an error message that I cannot fix:
error: duplicate class: com.mypackage.name.BuildConfig
error: duplicate class: com.mypackage.name.R
I also noticed that I am not the only one to have this issue (see the comment in the SO answer I linked above)
First time, I was able to fix it by enabling "External build" again, but that happened again, because I needed to see the error output and everything is now broken, and I cannot find what happen.
With Eclipse, the R file was easy to find, in the gen folder, but with Android Studio, there are too many files, and I am a little bit lost.
Any idea/suggestion?
I've found a question like this that has some replies here:
Cannot resolve R.java, duplicate class
You can try this:
Delete the Build folder generated by Android Studio automatically
Also you can try to Rebuild project by clicking Build->Rebuild project after deleting build folder.
So, just to let you know...
A few minutes after I posted my question, Google released an update to Android Studio (0.1.5)
See link: https://plus.google.com/+AndroidDevelopers/posts/Y9vhvGaHCbh
Tor Norbye kindly answered my question in this community, and I am sharing here
So the workaround I quoted in the OP is no more mandatory.
Enabling again External build after upgrading Android Sudio let me see the real errors ( a library and some Gradle import that I fixed)
So, I consider the Android Studio upgrade as the best answer to this question...
Ok, I also have the same problem, and this is what worked for me.
I first unticked external build from compiler settings. Then when I compiled i get two errors related to R.java, duplicate class.
Then i delete the build folder manually from finder. Then rebuild it from android studio, but still same error.
Then I again go back to compiler preference and tick the external build setting, and it worked fine after that.
Looks like some bug.
apparently, or at least for me 0.1.5 has a bug and cannot run in external build
because of some path error you can read about here
https://code.google.com/p/android/issues/detail?id=56628
so i switched to internal building, and then i hit the double R symbol bug
after deleting the build path application is compiled without errors but the build folder is not fully rebuilt, I'm missing the R.java file which the internal builder does not make.
i've rebuilt it on the external builder on an unupdated version as a temp workaround while this issues is fixed.
btw if anyone knows how to tell internal gradle to rebuild the build folder please share.
I just had the same problem and found a way to solve it (You can do this with Android Studio open):
Go to you Android Studio projects folder located in c:\users[USERNAME]\AndroidStudioProjects
Locate your project folder and open it.
Delete the folder named build.
From here, enter your application name folder, where you can find another build folder among with libs and src folders and a file named build.gradle.
RENAME the build folder to build2 or something else.
Now in Android Studio go to Build->Rebuild Project.
And after the project was rebuilt, open again the folder where the build2 folder that you renamed is located.
Delete the new folder named build that was created.
Rename your build2 folder to build again.
Done.

Resources