Export Android Studio Project
Google's preferred Integrated Development Environment (IDE) for developing Android Apps is Android Studio. Occasionally an App needs to be moved between PC's there can be several reasons for this, including:
- Handing development over to a new programmer.
- Moving code to another computer or development environment.
- Distributing demo, example and training source code.
Google used to use the Eclipse IDE. This supported an export option that allowed a project to be exported as a zip archive. It allowed files to be deselected for exclusion from the zip archive. The zip file could then be imported into another Eclispe IDE. Can an export project option be used in Android Studio? This article discusses how to export an Android Studio project. In brief it is to use a zip tool, but there are considerations. If you're in a hurry jump down to the Android Project Export Files Steps section. Otherwise read on for background information.
Android Studio Move Project to Another Computer
In Android Studio export project to zip involves using an external tool, such as 7-Zip for Windows. When sharing code between a team of developers the common method is to use a version control system (VCS), a.k.a source code management (SCM) system, with the Git system being extremely popular. But what about sharing code to a wider audience or across different development environments. This is where a zip archive is useful for distributing code. Especially as the zip format is widely supported. Fortunately it is easy to zip up the important parts of an Android Studio project, i.e. the code and resources. None of the settings and configuration files need to be included in the zip. They are associated with a particular hardware and software combination. A developer using the zip may have a different set up to the original coder, even using different development tools.
The Tek Eye article Android Project Structure provides a brief overview of the default files generated in a Studio project for an Android App. Understanding what the various files do allows us to determine which files can be excluded from a zipped Android project. A good starting point to determine what to exclude is to look at the contents of the .gitignore file. The following default Git ignore file is generated by Studio when a project is created. It is used by Git to determine the folders and files not to add to source control. These files and folders can be excluded from the zip file:
*.iml .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build /captures
To distribute just the source code for import into another Studio a lot of the other files in the projects folder can be excluded:
- .idea - This directory relates to the Studio IDE so can be ignored.
- .gitignore - The Git ignore file in any of the project directories is not required. They will be generated by Studio during the import.
- build - Any build directories are not needed. Studio will recompile all the source code.
- libs - The libs folder if it is empty.
- gradle - The gradle wrapper is not required as again it will be generated.
- gradle.properties - Not required if the default has not been added to.
- gradlew.* - The Gradle script files are not required.
- local.properties - Stores the path to the SDK, this may be different on another machine therefore it is not required (it will be generated on import).
The src directory in the default apps folder is where the source code for the project exists. Please note that other source code folders can exist and the apps folder can be renamed (refactored). By default everything in main would be in the zip file. However, not everything in apps may be required:
- test - This folder can be excluded if the App has no unit tests written.
- androidTest - This folder can be excluded if the App has no instrumentation tests.
- proguard-rules.pro - If no rules have been added to the default file then it is not required.
If both the test directories are removed edit the build.gradle file in the app folder to remove the junit dependency. Likewise if the proguard-rules.pro file is removed edit the same build.gradle to remove the reference to it. See the details for changing build.gradle in the next section. Doing all of the above means a minimum zip archive will have:
- The app directory (minus the files and directories discussed above)
- The build.gradle files (one in project and the main one in the app directory)
- A settings.gradle file
Android Project Export Files Steps
The following steps were determined from a Windows computer. For other systems additional files may require deletion, e.g. .DS_Store on Macs. Remember the following is performed only to allow the Android source code for an App to be imported into Studio on another machine or into another IDE. These steps are based on the default project structure and will require changing for large complex projects.
An Android project is stored in the file system as it appears in Studio:
Start by using the file manager, e.g. Windows Explorer, to copy the whole project into another temporary location. Then use the file manager to remove the files not needed in the zip archive (also see the basic batch file at the end). Make the following changes to the copied project:
- Delete directory .gradle
- Delete directory .idea
- Delete any build directories, including the one in app
- Delete the gradle directory
- Delete all .gitignore files, including the one in app
- Delete the gradle.properties file if it hasn't been added to
- Delete the gradlew and gradlew.bat script files
- Delete all *.iml files, including the one in app
- Delete the local.properties file
- Delete the app/libs directory if it is empty
- Delete app/proguard-rules.pro if no rules have been added
If proguard-rules.pro is deleted edit build.gradle so that the line:
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- If there are no unit tests delete the test directory in app/src
- If there are no instrumentation tests delete the androidTest directory in app/src
If both test directories are deleted remove the following line from the build.gradle dependencies section (where x.xx is a version number):
With these changes made the copied directory can now be zipped, preserving directory structure. The created zip file is ready for distribution.
Importing the Zip File in Studio
To import the project on another computer first extract the files. In Studio the import does not move any folders or files. Therefore ensure the project is extracted to the required location. In Studio use the Import project option on the Welcome screen (to return to the Welcome screen close any open projects).
From the import dialog select the top level build.gradle file and the project is opened. A message asking to set up Gradle is shown. Select OK.
Wait for Studio to configure the project, adding the local files for settings and Gradle (watch the status bar at the bottom of the Studio screen). The project is now ready for editing and running in Android Studio.
Possible Unable to Update Error
When importing a project into Studio you may see an unable to update error, e.g. unable to update Gradle:
In this case the Unexpected Error dialog displays the message Unable to update wrapper to use Gradle x.y, where x.y is a version number. It displays the path to a file and Access is denied in brackets. This is usually due to write protected files. Remove the read only attribute for write protected files. On Windows use the context menu on the project directory (normally right-click). Then select Properties and clear the Read-only check box. Click Apply and confirm attribute changes with Apply changes to this folder, subfolders and files selected.
Failed to Sync Gradle Error
Another error message that can be displayed when importing a project is a Failed to sync Gradle when the version of Gradle used by the project is not available.
The correct version needs to be downloaded. Even better click the link Fix Gradle wrapper and re-import project Gradle settings in the error message. This will sync the project Gradle with the one Studio is using. It is possible to prevent this error being displayed, see the next section.
When a Gradle update message is displayed select Update to update the project's Gradle version.
Version Independent Library References in Gradle
If an Android project isn't dependent upon using a particular version of library, then the build.gradle files can indicate that the latest available version should be used. This is useful for example projects, demo code and projects used for training. In the dependencies section in Gradle files change the specific version number for a library to a +. This tells Gradle to use the latest available version.
For example this entry from a build.gradle file in the app directory:
Can be changed to:
Likewise, in the project's root build.gradle file changing:
Would upgrade the projects use of Gradle from 1.5 to the latest version (and preventing a Failed to Sync Gradle error).
The + can be used for minor versions so the line:
Specifies the latest minor update of version 23 of that library.
Starter Batch File for Android Project Cleaning
On a Windows PC the following basic batch file can be used to perform most of the folders and files deletion mentioned in the article.
Note: Take care using batch files, a lot of damage to a system can be done by batch files executed from the wrong location.
This batch file can be create in a copied Android project directory, e.g. as clean.bat. It will clean a basic Android project to it's code and Gradle build file (ready for zipping and/or importing into Studio). Adapt the commands as required for a particular project. The batch file shown below deletes the test directories and ProGuard file. Remove those lines as necessary, if not removed remember to edit the build.gradle file in the app directory to remove the references as detailed in the article. The libs directory is deleted, remove that line if the project has additional libraries (jar files). Take care using batch files, it is your responsibility for their actions. If in doubt don't use it.
@echo Clean a Android Studio project ready for importing and zipping pure code @echo Modify this file to meet project requirements pause @echo Remove Gradle code, added back in on import rmdir .gradle /s /q @echo Remove IDE files rmdir .idea /s /q del *.iml /f /s del local.properties @echo Remove build folders, will be recreated rmdir build /s /q rmdir app\build /s /q @echo Remove Gradle Wrapper, will be added back in rmdir gradle /s /q @echo Remove Git ignore files del .gitignore /f /s @echo Remove other Gradle files del gradle.properties del gradle?.* @echo Remove libs folder rmdir app\libs /s /q @echo Remove ProGuard rules del app\proguard-rules.pro /f @echo Remove test code rmdir app\src\androidTest /s /q rmdir app\src\test /s /q @echo Clear Read-only attributes attrib -R *.* /s @echo Do not forget to edit build.gradle in the app directory
- For more information on the files found in an Android project see the article Android Project Structure.
- To remove a project from Studio see the article Delete a Project in Android Studio IDE.
Author:Daniel S. Fowler Published: Updated: