Where to put gitIgnore file in Android Studio? - android-studio

I am developing an app and I must take that project to GitHub. Now, I must make gitIgnore file. I know that file is used to ignore some specified files from my project. I used gitIgnore.io service and I received generated file. This is my gitIgnore file:
# Created by https://www.gitignore.io/api/android
### Android ###
# Built application files
*.apk
*.ap_
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio Navigation editor temp files
.navigation/
# Android Studio captures folder
captures/
# Intellij
*.iml
.idea/workspace.xml
# Keystore files
*.jks
### Android Patch ###
gen-external-apklibs
But I don't know were to copy this, and where to put that in my Android project.
Could someone help me?
I copied this file into my gitIgnore file in Android Studio, but when I have pushed that project on gitHub my gitnigore file looks like this:
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
So, that files that I copied into Android Studio are not here. What is the problem?

Normally when creating a new project the gitignore file is generated for you.
Here is the correct .gitignore file.
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
This is were you have to put it.

.gitignore file must be in the root directory of the project not just in android projects but in any project types
I hope my answer would be useful

Location
The file should always be placed in the root directory of the project.
Contents
Check out this handy page on Github with template gitignore files for Android and other languages - they represent best practice and are regularly updated.

Related

Android Studio: build.gradle (project) not visible in "Project"

I am trying to rebuild an old (working) project in AndroidStudio. It builds Ok but strangely, the project's build.gradle is not visible in the AS Project tab.
Note that the module's build.gradle appears but not the project's build.gradle.
Both files ARE indeed there
Directory of C:\dev\AndroidProjects\apps\Passwords\app
11/09/2020 04:16 PM 767 build.gradle
1 File(s) 767 bytes
Directory of C:\dev\AndroidProjects\apps\Passwords
11/09/2020 03:13 PM 352 build.gradle
1 File(s) 352 bytes
The project otherwise builds just fine.
The projects build.gradle IS active: If I make changes to it outside AS using a text editor, the changes DO take effect.
What I've Tried:
Restart PC and AS
Cleaned/Rebuilt Project
Invalidate Caches & Restart
Searched SO for problem. Question 47406517 was similar but solution didn't work
Copy build.gradle from another known-good project.
Notes:
Android Studio: V3.6.3
PC: Win10-64
Project Name: "Passwords"
I'm at a loss for other things to try. Any suggestions?
(I guess as a work-around, I can always use an external text editor but that's annoying).
I found the answer in SO question #46230078. The solution was simple: Close AndroidStudio, delete the directory ".idea" from the project directory, restart AndroidStudio. This did it!

Why Android Studio (3.4) created .gitignore doesn't exclude all `/.idea`?

Whenever a new Android Studio project is created (I'm using Android Studio 3.4), the .gitignore content is as below.
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
Why isn't entire /.idea to be ignore but only the selective few? Are the remaining of any special use?
There are some that you generally want to check into version control (anything project-specific but not user-specific). See What to gitignore from the .idea folder? for more information. If you don't want to version control them, you can exclude the whole directory.
Excluding all .idea-data would also exclude gradle.xml
I guess this is important for building.
But I am just guessing.

Android studio configuration with Gradle

I am having an issue to configure Android studio after importing gradle project. My project structures is as following:
Root Project
|_.config
|_settings.gradle
|_build.gradle
|_app_module
|_lib1_module
|_lib2_module
|_ etc.
Please note, that for several reasons I am not able to move settings.gradle and build.gradle files to project root . Also, the files mentioned above are configured correctly, so there is no problem with the build.
So the problem is that once the project in imported, the all gradle related file and folders are are generated inside .config folder, which doesn't disturd me.
Also the .idea folder and all relevant sub-folders are generated in this very folder.
So this is the problem. For some reason, the studio doesn't recognize all sub-modules are under source control. I.e. it's vcs.xml is empty and contains only project root. Actually the problem is resolved by adding the modules via settings--> subversion menu, but I'd really like out-of-the-box solution. I've tried to put .idea folder under source-control - doesn't help. Upon project creation, the vcs.xml is re-written and empty again.
So, the main question is:
Is there a possibility to
Force studio to generate .idea folder and the root project folder without changing location of .gradle files OR
Make studio NOT to re-write it's settings files upon project import (e.g. keep predefined vcs.xml)
PS. I'm aware the putting all generated files (including gradle folder, gradlew, etc.) will probably solve the issue, but I wouldn't like to go so far.
Thank all in advance
It should be a comment, but it is too long.
Android Studio works with a project.
Inside this project you can configure more modules but it need this structure:
Root Project
|_.idea
|_.gradle
|_.config
|_build.gradle
|_app_module
|_build.gradle
|_lib1_module
|_build.gradle
|_lib2_module
|_build.gradle
|_ etc.
|_settings.gradle
|_build.gradle
Inside the settings.gradle you have to define all modules:
include ':config' , ':app_module' , ':lib1_module', ':lib2_module'
Under the root folder, Android Studio when builds the project with gradle, makes same folders, like the .idea.
I read that your are not able to move the settings.gradle file.
In this case Android Studio recognizes the config folder as the root of a project and build the .idea folder inside.
|_.config (root)
|_.idea
|_.gradle
|_build.gradle
|_settings.gradle
|_myModule
Force studio to generate .idea folder and the root project folder without changing location of .gradle files OR
As I know you can't avoid this.
Also ,
Make studio NOT to re-write it's settings files upon project import
The .idea, the .gradle folder, the .iml files are built locally by the IDE when the project is built. The IDE re-writes these resources, also these files contain local paths and usually they are not commited in VCS for this reason.

Which files can I ignore when I backup a Android studio project by myself?

I create a Android Studio project which is located in the folder E:\Android_Studio_Project\MessageCleanup
I hope to backup the project by myself.
I think that all files located in the folder E:\Android_Studio_Project\MessageCleanup\app\build\outputs\apk can be ignore, is it right?
And more, are there other files which can be ignored? Thanks!
you can ignore:
.gradle
.idea
app/build
build
local.properties
you could ignore
.gitignore (if you don't use git or dont want to exclude files from git)
The rest I would try to keep.

copy static files to build output folder in gradle

I was using ANT before (Android Project) and i had "static" files in the same packages as my code
Here is an example
src/com/my/app/test/Parser.java
src/com/my/app/test/json_to_parse.json
When executing the unit tests, the json file was copied into the gen folder, therfor it was possible to access the json in the test with
getClass().getResourceAsStream(fileName)
I had to convert the project to gradle, but now the tests are failing.
After checking the "build" folder, i've realised, the .json files are not there, therefor the getResourceAsStream method returns null.
Any idea how to include these "static" files (json, xml, ...) into the build folder?
Moving the files into the resources folder did not work out of the box in Android Studio (even though is should have)
This should be fixed in Android Studio 1.2.
However, this is what i did:
Moved all static files into the resources folder.
In my unit-test module i've added this to the build.gradle file
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
Now, all files located inside src/test/resources will be copied into /classes/test where i can access them with
getClass().getResourceAsStream(fileName)
If i keep the package structure inside the resources folder the same as it was in the java folder, i don't need to adjust any code.
To complete the story a bit more:
JUnit4 runner requires
getClass().getResourceAsStream(name)
while Robolectric requires
getClass().getClassLoader().getResourceAsStream(name)
The files you are asking about are called "resource files" in Maven/Gradle lingo.
Gradle assumes that you are using the Maven Standard Directory Layout.
So, either you move your files into src/test/resources (then Gradle will pick them up automatically), or you tell Gradle that it should look for resources in some other place.
In the latter case, you need to modify the processTestResources task. However, keeping resource files in the same directory as source code is a bad practice. So I advise the former option.
if your problem is happen when you create apk with AndroidStudio.
you can create a jar file that includes your resources with jar.exe
for example i put a.txt into resources directory
and run this code in cmd:
"C:\Program Files\Java\jdk1.7.0_79\bin\jar" cvfe res.jar -c resources
after that a jar file "res.jar" was created
then add that res.jar into libs folder in your project
when your apk is creating resources are added to your final apk and you can use this code to acsess a.txt:
someclass.class.getClassLoader().getResourceAsStream("resources/a.txt");
with this job no need to change Gradle setting.

Resources