本文适用于从Eclipse转AndroidStudio的开发者
Recently intends to write a series of Android primary development tutorials, estimated more than 40 articles, combined with my actual work experience, write some work often used in the technology, so that beginners can less detours, this is the first article of this series, 345 great God wrote.
Original address
One. Download and install
JDK Install and configure Java_home environment variables
这个就略过了,纯小白请自行百度
Android Studio Installation
截止到2016年4月9日Android studio的最新稳定版本为2.0,推荐使用该稳定版本。
:
1 website download page Portal
如果下载速度很慢,请复制下载连接到迅雷,万能的迅雷会帮你提速的。如果根本打不开请移步地址2
2 Androiddevtools
You need the basic will have here, strongly recommend to download here.
If the Local has already downloaded the SDK, it is recommended to download the zip-free version, the first time you start the configuration of the SDK directory is good.
Two. First time start
- After the installation is not anxious to start, the first launch will pop up check the SDK update components and other settings, click Finish may go to download, if not set up the agent, this interface will be stuck for a long time.
Workaround
- Close Android Sutdio, open the Bin folder under the Android Studio installation directory, locate idea.properties and open it.
- Add the following code to the tail of the file:
# Disable First Check SDK feature Disable.android.first.run=true
Click Configure Select SDK Manger to configure properties such as SDK directory
Click on the lower left corner to connect to launch SDK Manger Configuration SDK Update image
I want to say this step in the celestial kingdom is necessary, reason I think you understand
Here's how to use the SDK update Mirror site:
Please refer to Androiddevtools for details.
1. Launch the Android SDK Manager, open the main interface, select "Tools", then "Options ..." and pop up the "Android SDK manager-settings" window;
In the "Android SDK manager-settings" window, fill in the "HTTP proxy server" and "HTTP Proxy port" input boxes with the above mirror server addresses (which do not contain HTTP.//, such as) and ports, and select "Force https://. Sources to be fetched using http://... check box. Click the "Close" button to close the "Android SDK manager-settings" window and return to the main screen after Setup is complete.
Select "Packages", "Reload" in turn.
选择镜像网站的时候要注意的是,并不是所有的适合别人的就适合你还有可能根本无法更新,由于服务器远近带宽的问题,你应该选择适合自己的镜像,怎么选择。。。。我想说设置成功后选择更新个没下载的文件能成功下载更新并且速度不慢的就是合适的
Theme Settings
This is nothing to say, but the pursuit of the black theme of the small partners to care about this many, here is explained.
Start Page Click configure-> Select Prefrences->appearance->theme->darcula that's the black Visual Studio style theme->apply finished
If you want more font keyword color What personalized configuration, recommend to the following URL download
Http://color-themes.com/?view=index
Choose the theme you like to download, download to a jar file, using the method:
在AS中选择File->import settings ->选择下载的jar文件->勾选editor color 确认
Three. Project Engineering
- New Project
Follow the guidance step by step, pay attention to the minimum compatible version of the selection, the new project does not provide a compile version of the option, the default currently installed the highest version of the SDK, as to provide a few activity templates, template implementation are more classic, recommend a new look.
- Project directory Structure
Create a new project click on the upper left structure view toggle, switch to the project directory structure (the development is commonly used in Android view, relatively concise) where app2 and mylibrary is to facilitate the next explanation of the newly completed manually added module default new project is not.
MyApplication folder status quite eclipse of the workspace
不同的是AS工程目录下面多了用于全局项目的配置文件,比较重要的是local.properties、setting.gradle、gradle.properties 、build.gradle和gradle文件夹
- Local.properties
Where you can set up your SDK, NDK configuration, generally do not need to change
- Settings.gradle
The file contains only a sentence, the module contained in the Declaration
For example, the above project contains three module
include ‘:app‘, ‘:app2‘, ‘:mylibrary‘
- Gradle.properties
Gradle some parameter settings used in the compilation normally do not need to change
- Build.gradle
Define the dependencies of the version warehouse and some gradle commands generally do not need to change
Module directory
This directory is very similar to the Eclipse directory, it is easy to understand the corresponding relationship between the various directories, the more special is more than eclipse a series of mipmap folders, mipmap and drawable folders are used to store pictures, according to official documents is The Mipmap folder is only placed in the app icon. Other drawable resources that need to be used are placed in the corresponding Drawable folder as before.
New Moudle when the Drawable folder only default without resolution parameters, for different resolutions of the folder needs to be built manually, as provides a convenient way to create
On Moudle, right-click on Android Resource Directory, select the type drawable to the left list select density Select the DPI
Such as
Other res files are created in the same vein.
- Open the Internet download as engineering Gradle error problem
Sometimes we download a demo project from the Internet after open Gradle error can not run, this situation most due to the Gradle version inconsistency, as download corresponding version of the problem caused by the failure, generally there are two ways to solve
* Manually download the corresponding Gradle version to the local directory
这种方式不推荐有兴趣的自行搜索
* Modify the Gradle configuration to open the project
Refer to the same file for the local new project
Gradle-wrapper.properties
Distributionurl=https\://services.gradle.org/distributions/gradle-2.10-all.zip// the modified version number is the same as the local project file
Build.gradle
Dependencies { ' com.android.tools.build:gradle:2.0.0 ' // Modify version number same as local project file }
Four. Add dependencies
Understand the project structure after simple compilation run debugging generally there should be no problem, after all, the menu bar, the bottom function window function hints are very clear, the following is a comparison of the place to encounter problems: add dependent projects or run a third-party project error problem.
There are generally three ways to add dependencies
1. Adding a dependency on the Code warehouse
This is the simplest way to add a word directly to the dependencies inside the Build.gradle of the project module, and then rebuild it, such as:
12345678 |
dependencies {
compile fileTree(dir:
‘libs‘
, include: [
‘*.jar‘
])
testCompile
‘junit:junit:4.12‘
compile
‘com.android.support:appcompat-v7:23.3.0‘
compile
‘com.android.support:design:23.3.0‘
//添加了OKHttp的依赖
compile
‘com.squareup.okhttp3:okhttp:3.2.0‘
}
|
Can be added manually or you can right-click the item in open Module settings search Add (recommended this way)
AS会自动联网下载添加的依赖 这个一般不需要FQ
- Dependency of the JAR package
This is the simplest and does not have to be networked, like Eclipse to copy the jar package into the Libs folder, and then need to rebuild the project click on the menu bar of the Build button
Build complete the left side of the jar file in the directory bar should have a pointed tip to expand view jar Package Contents
Some jars also need to be associated. So library just need to build a folder named Jnilibs under the main folder of the SRC directory to copy the so library in (same as res directory)
3. Reliance on the Libray project
Most of the time we need to use a project downloaded from GitHub and add the library provided to our project by first importing module and then adding dependent
Open project structure, add new Moudle, select the module for the library you want to import in the downloaded project file
note Instead of importing the entire project folder that you downloaded, select the module folder
Add the imported library to the dependency of the destination project
当导入的library里的项目build.gradle文件里包含一些上传到版本仓库的信息的话导入过后可能会报错,这时候只需要把不必要的信息注释掉即可,需要保留的是编译配置,依赖关系,目录配置。如下
Apply plugin: ' Com.android.library '//Project compilation InformationAndroid {compilesdkversion23buildtoolsversion"23.0.2"Defaultconfig {minsdkversion11targetsdkversion23Versioncode1Versionname"1.0" }}//Dependency InformationDependencies {Compile Filetree (dir:' Libs ', include: [' *.jar ']) Testcompile' junit:junit:4.12 'Compile' com.android.support:appcompat-v7:23.2.1 'Compile' com.android.support:design:23.2.1 '}
Of course, you can also manually copy the module folder to the project directory and then modify the Settings.gradle file, but this is not recommended.
Five. Shortcut keys
As the automatic prompt function is very powerful, has the input after the automatic popup prompt does not need to like eclipse presses alt+/to prompt, as for other as's shortcut key many does not explain, last picture takes to self-discipline
Six. Plug-in installation
Open Setting page Find plugins search installation
Recommended a few as plug-ins, the specific use of your own Baidu
1. Gsonformat
2. Android-butterknife-zelezny
3. Jsononlineviewer
Seven. Multi-channel Package
Domestic on-line app generally to be released from the multi-channel, in order to statistical channel installation generally need each channel to produce an installation package, as comes with multi-channel packaging function, multi-channel packaging is definitely as one of the most practical functions, here is the key to talk about.
- Generate Signature File
Signature files can be generated in Build->generate signed APK---Select project Next->create new, in the same way as Eclipse
- Set up a signed file for packaging
Open module setting create a new signing key select signature file fill in the signature file password alias, etc.
- New Flavors Configuration
As an example, I created three new configurations and selected the signature to be packaged to configure a offline_debug, a release_360, a Release_baidu
- Modify module's build.gradle and mainfest files
After the configuration of the previous step, Build.gradle will be more than productflavors and singconfig configuration, we want to modify the productflavors part.
How to modify us to illustrate
This is the part of the Build.gradle file that was modified in
Defaultconfig {ApplicationID"Com.example.mark.myapplication"minsdkversion14targetsdkversion23Versioncode1Versionname"1.0"//buildconfig.is_sign = FalseBuildconfigfield "Boolean", "Is_sign", "false"} productflavors {offline_debug {signingconfig signingconfigs.signconfig//here is a demonstration of a variable's wordingManifestplaceholders = [app_name: "Debug Version"]} release_360 {signingconfig signingconfigs.signconfig//Buildconfig.is_sign = TrueBuildconfigfield "Boolean", "Is_sign", "true"//here is a demonstration of multiple variables.Manifestplaceholders = [app_name: "360 version", EXAMPLE: "360"]} release_baidu {signingconfig signingconfigs.signconfig Buildconfigfield"Boolean", "Is_sign", "true"manifestplaceholders= [App_name: "Baidu version", EXAMPLE: "Yahoo!"] } }
where defaultconfig within the Buildconfigfield "Boolean", "Is_sign", "false" is the build project generated in the Buildconfig class to add a bool variable named Is_sign, and assigns a value of false.
This variable can be used in the code, such as the following code can be used to switch the line and offline request address
Public Static String base_url; Static { if(buildconfig.is_sign) { = "On-line Address"; } Else { = "Offline Address"; } }
Manifestplaceholders = [app_name: "Debug Version"] in productflavors is the meaning of replacing the app_name in the Androidmanifest.xml file with the string after the colon. This sentence is only one of the variables to be substituted, the following example of the way you have multiple variables. This app_name is defined in the manifest file as follows ${variable name} Gradle will automatically replace the placeholder variable when it is packaged.
<activity android:name= ". Loginactivity " android:label=" ${app_name} "> <intent-filter> <action android: Name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity>
This variable definition can appear in mainfest any need string position, I above is just an example, please ingenious do not copy copy.
- Packaged
At the same time, select the configuration item you want to package click Finish to generate multiple apk at the same time.
Getting Started with Android Studio