[置顶] Android Studio使用心得

时间:2021-12-23 16:39:21

说实话 开始接触这个工具 真的觉得很恶心 毕竟大陆被墙  很多东西用起来不是很方便 而且Eclipse转到Android Studio还是一个跨度 废话不多说  下面 讲下我遇到的问题

1. 安装的时候(Setup Wizard - Download Components) 这个要下载很长时间 甚至下载不了 (PS: 这个选择并下载2.25G的组件是studio的一个bug,评论里有人提醒,感谢这位同学。如果网速不行想跳过这步的可以在bin目录的idea.properties增加一行:disable.android.first.run=true就行了,mac平台的右键安装包->Show Package Contents 就找到bin目录了。)

 

2.新建项目成功后会下载Gradle,貌似这个过程不*也是可以下载,但是访问特别慢,建议*下载。那么下载的Gradle到什么地方呢?  打开C:\Users\Administrator\.gradle\wrapper\dists\gradle-1.10-all\d90a2yjknzzhpcfgm937zpcte 你会看到需要的gradle版本 例如我的是gradle-1.10 我会去百度上搜这个下载 一大堆 下载之后把gradle-1.10-all.zip复制到此目录下(C:\Users\Administrator\.gradle\wrapper\dists\gradle-1.10-all\d90a2yjknzzhpcfgm937zpcte)

 

注:如果是导入一个项目一直处于Building 那么去修改项目Gradle目录下的gradle-wrapper.properties 文件里边的distributionUrl 最后边改成已经下载的gradle版本例如 我已经有gradle-2.2.1-all.zip 但是没有gradle-2.4-all.zip的 所以我会改成distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip

 

如果导入项目之后 下载Android studio那么结束掉任务 去修改项目根目录下的build.gradle
改成你现在的版本

 dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

 

 

3. 关于build.gradle的配置:

   主工程app:

    apply plugin: 'com.android.application'  表示申明此工程为主工程

 

 dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])  默认不需要多解释

compile project(':StudioKlowerBase')}  申明主工程依赖的Library 注意拼写规则, 名字要与你的Library名字一样

 

buildTypes {
release {
minifyEnabled true(表示打包签名的时候 是正式包 会执行混淆代码)
       proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      定义代码混淆文件 注意:proguard-rules.pro要放在主工程的目录下
    }
}
完整代码如下:
apply plugin: 'com.android.application'

android {
compileSdkVersion 19
buildToolsVersion "19.1.0"

defaultConfig {
applicationId "com.klowerbase.test"
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile project(':StudioKlowerBase')
}

--Library 工程的配置
apply plugin: 'android-library'定义为Library
dependencies {    classpath 'com.android.tools.build:gradle:1.2.2' 定义编译的gradle版本
 }
完整代码如下:
buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.2.2'    }}apply plugin: 'android-library'dependencies {    compile fileTree(include: '*.jar', dir: 'libs')}android {    compileSdkVersion 19    buildToolsVersion "19.1.0"    sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        // Move the tests to tests/java, tests/res, etc...        instrumentTest.setRoot('tests')        // Move the build types to build-types/<type>        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...        // This moves them out of them default location under src/<type>/... which would        // conflict with src/ being used by the main source set.        // Adding new build types or product flavors should be accompanied        // by a similar customization.        debug.setRoot('build-types/debug')        release.setRoot('build-types/release')    }}

项目的配置 代码如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.2.2'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()    }}
 
解决Task '' not found in root project '***'.方法1:删掉.iml里的<component name="FacetManager"> ... </component>方法2:删掉.iml跟.idea文件夹 重新导入程序经过实验:第二种方法 有效由于我用的gradle-2.2.1 项目结构有些变化,如下截图:
<img src="http://img.blog.csdn.net/20150720130051120?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

最后在附上一些常用的快捷键:

Ctrl+Alt+L  格式化代码

Ctrl+Alt+space 代码提示

Ctrl+Alt+O 优化导入的类和包

Alt+Insert 生成代码(如get,set方法,构造函数等)

Ctrl+Shift+Space 自动补全代码

Ctrl+空格 代码提示

Ctrl+R 替换

Ctrl+Y 删除行(ctrl+x不是删除行,是剪切。如果不选中,则为剪切当行。ths for 貌似掉线)Ctrl+D 复制行Ctrl+/ 或 Ctrl+Shift+/  注释(// 或者/*...*/ )