When it comes to using AndroidStudio, except for newly created projects, the problem we face is how to import the original Eclipse code into AndroidStudio.
There is still relatively little information about this. I have explored it myself and summarized this blog, hoping to help developers avoid detours.
OK, get to the point.
In order to make it more convenient for us to use AndroidStudio, Google provides two ways to import Eclipse code: one is compatible with Eclipse and the other is the brand new Android Gradle Project.
Here, let’s start with the compatibility mode.
Compatibility mode
Under this mode, the code directory structure of the Eclipse era is guaranteed, and the overall operation and use are similar to Eclipse.
Most importantly, when you use Android Studio, you or others can also use Eclipse conveniently without interfering with each other.
Implementation steps
1. Export Gradle build files from Eclipse
File --> Export --> Generate Gradle build files in the Eclipse menu
Next, you will arrive at the warning interface, which will prompt AndroidStudio to directly import ADT projects. First pass, and then there will be an explanation of direct import.
Select your project, including the main project and library project.
Confirm generation
2. Modify export file parameters
After exporting, since adt has not been updated for a long time, some parameters need to be manually changed to ensure normal use.
In order to ensure that the explanation can be clearer, let me explain it in detail below:
There is no library project, only the main project
In this case, the directory you see is like this
The first thing you need to change is the build.gradle file
AndroidStudio 1.0 ~ 1.0.2 uses Android Gradle plugin 1.0.0, so you need to change 0.12.+ in the red box in the figure to 1.0.0
Then you need to update the Gradle version, specifying it as the required 2.2.1
In /gradle/wrapper/gradle-wrapper.properties
Included library project
In fact, the change method is the same as above. Just note that it is to change the build.gradle and /gradle/wrapper/gradle-wrapper.properties of the entire project.
Don't try to find build.gradle in the main project or library project
3. Import AndroidStudio
Enter AndroidStudio and select import non-AndroidStudio projects
Find the project directory that needs to be imported, and you can see that the icon is different from the project created by Eclipse.
Click OK to enter the long loading process, and then it can be used normally.
If you have created a project, you will open AndroidStudio and directly enter the previous project.
At this time, you can select File-->Import Project, and click OK after selecting
Bugs in compatibility mode
Packaging using IDE:
In the compatibility mode, only the main project is used, and the library project is not added to the apk, resulting in the failure to run normally.
As of 1.0.2, this bug still exists in Android Studio.
Because of this bug, you can use AndroidStudio programming in compatibility mode and enable Eclipse when packaging.
OK, the compatibility mode is introduced here. We start by introducing the protagonist: Android Gradle Project. What must be explained here is that Google would rather use this method. If there is no pressure from the team, it would be better to use this method.
Use the new directory structure
Convert directory structure
Open Import
New users:
Old user: File-->Import Project
Just find the original Eclipse project and import it directly.
If you have library projects, you need to pay attention to the import must point to the main project, not the directory of the entire project. Pointing to the project directory is not possible.
Specify the destination path
This place needs to be explained in detail
Let’s not talk about the above two options for now, check it.
The last item is mentioned: Create the Module name in camelCase style.
Camel style, which is familiar to those who work in Java. camelCase represents the camel style with lowercase initial letters, and CamelCase represents the camel style with uppercase initial letters.
Not checking this option means what your original project name looks like and what it looks like after conversion.
AndroidStudio here will analyze the . project file under the Eclipse project, and the name in it is the project name you see in Eclipse. (The folder name of this project will be used when this file is not available.)
Of course, there are exceptions. For example, countly-sdk-android is converted into countlysdkandroid. I don’t know about the underscore. I didn’t study the rules carefully, but CamelCase will remain normal.
Here I prefer to make the Module initials capitalized (I don’t know why it’s uncomfortable to see the lowercase of this letter). The original project is CamelCase style, so I won’t check it out.
Of course, what if you don’t pay attention to it and directly convert it to camelCase but don’t like it in the end? You can see the "How to Change Module Name" section later.
OK, click Finish after configuration
It will take a period of conversion after that. After success, you can see import-summary.txt. This file is very useful and will be discussed later.
At this point, your application can actually be compiled normally through AndroidStudio, and you can also write code.
But I hope you can continue to watch it, because the import-summary.txt that pops up after success writes some very important things.
Let you understand import-summary.txt
Manifest Merging
Your project uses libraries that provide manifests, and your Eclipse project did not explicitly turn on manifest merge. In Android Gradle projects, manifests are always merged (meaning that contents from your libraries' manifests will be merged into the app manifest. If you had manually copied contents from library manifests into your app manifest you may need to remove these for the app to build correctly.
This paragraph should be understood, it simply means that your Library's manifest file is valid.
No need to copy it manually to the list file of the main project. If you have copied it before, you must remove it to build correctly.
Ignored Files (this is the most important)
The reason why this part is important is that AndroidStudio tells you through this text what files it ignores when importing.
In other words, this part of the content has not been copied for you. If you don’t copy it yourself, then this part of your new Gradle project will be gone.
Here are some typical ignore files to check:
From SDK:* proguard-project.txt* proguard.cfgFrom UI:* cert/* cert/certFrom CrosswalkWebview:* ant.properties* build.xmlFrom SlidingMenuLibrary:* LICENSE.txt* pom.xml
Classify the above ignore files:
You need to pay attention to your Ignored Files section and copy it manually to the new project according to your needs.
Replaced Jars with Dependencies & Replaced Libraries with Dependencies
In the Replaced Jars with Dependencies section, there are the following text:
android-support-v4.jar => com.android.support:support-v4:21.0.3gson-2.3.jar => com.google.code.gson:gson:2.3joda-time-2.6.jar => joda-time:joda-time:2.6
You can see that the jar has been replaced. You can see that the jar package you added was not included in the project. So where did the jar package go?
Let's leave a suspense for the time being.
Let's continue to look at the Replaced Libraries with Dependencies section
ActionbarLibrary => com.actionbarsherlock:actionbarsherlock:4.4.0@aar com.android.support:support-v4:21.0.3
You can see that ActionbarSherlock is replaced with aar (Android ARchive, jar: Java ARchive). For an introduction to aar, you can read here, and this article will not be expanded in detail.
You can see that Library and jar have been replaced, but you can never find it in the current project. why is that?
Let me see the following operations:
Right-click on the Module and select Open Module Settings
Switch to Dependencies
Click the plus sign on the right to add and select Library dependency
You can see this interface:
OK, you can know the truth here. In AndroidStudio, you can add online libraries or jar packages. After the addition is completed, you can use them normally as usual.
Your colleagues will download it instantly when opening this project to ensure that your code can run normally.
This is also why some jars and Library disappear after your project is converted. They were transferred online.
Moved Files
This part is about the changes in your directory structure. You can basically think that he is talking nonsense and there is no need to read it.
Next Steps & Bugs
Haha, skip it
The last sentence
This import summary is for your information only, and can be deleted after import once you are satisfied with the results.
Once you feel that your project is fine, you can delete the file.
Import Android Studio project note
Through the above steps, your project has converted the directory structure. At this time, you can submit the code for other colleagues to download and use.
Other colleagues downloaded the code, and what they needed to introduce was an Android Gradle Project.
New users
If there is an IntelliJ file such as .idea in the project, the one you should choose should be the Open an existing Android Studio project instead of the Import Non-Android Studio project.
If the code you downloaded only has gradle files, even if you still have to choose Import Non-Android Studio project.
Old users still use: File-->Import Project
It should be noted here that for projects containing library projects, it is useless to specify the directory of the entire project.
At this point, we need to stop and summarize:
If you are importing an Eclipse project, specify the main project directory. AndroidStudio helps you complete the conversion through analysis.
If you are importing an AndroidStudio project, specify the directory for the entire project.
Appendix: How to change the Module name
Right-click Refactor-->rename on the Module, of course you can see that there is a shortcut key shift + F6, so that the name of the Module can be changed.
But it's not over yet, so there is no change to where the reference to this Module is referenced.
If it is a library project, you need to continue to look at the places that reference it and modify it. Mainly Gradle files of other Modules.
Tips:
Select Module, ctrl+c to copy the project name, and replace ctrl+v with a new name in the relevant Gradle file.
Finally, don't forget, settings.gradle, the module name of include here also needs to be changed. At this point, the modification is over.
After all modifications are completed, click Sync Now in the pop-up prompt to rebuild the project
Everything is normal after the build is completed.
Of course, if you don’t like the name of the folder, you can also modify the name of the folder and remember to import it again after changing it.
Summarize
The above is an introduction to the two ways of importing. Generally speaking, the first one is suitable for getting familiar with Android Studio in advance when you are helpless, and the second one is the more recommended way.
No matter which one you use, I hope you can get in touch with Android Studio as soon as possible and experience its power. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.