http://stormzhang.com/devtools/2014/12/18/android-studio-tutorial4/

https://github.com/stormzhang/9GAG

我的一位同学的关于android的博客(刚开始请多多关照)

Android Studio

主题

字体


**快捷键**Action          Mac OSX                         Win/Linux注释代码(//)    Cmd + /                         Ctrl + /注释代码(/**/)  Cmd + Option + /                Ctrl + Alt + /格式化代码       Cmd + Option + L                Ctrl + Alt + L清除无效包引用     Option + Control + O            Alt + Ctrl + O查找          Cmd + F                         Ctrl + F查找+替换       Cmd + R                         Ctrl + R上下移动代码      Option + Shift + Up/Down        Alt + Shift + Up/Down删除行             Cmd + Delete                    Ctrl + Y扩大缩小选中范围    Option + Up/Down                Ctrl + W/Ctrl + Shift + W快捷生成结构体     Cmd + Option + T                Ctrl + Alt + T快捷覆写方法      Ctrl + O                        Ctrl + O快捷定位到行首/尾 Cmd + Left/Right              Ctrl + Left/Right折叠展开代码块     Cmd + Plus,Minus                Ctrl + Plus/Minus折叠展开全部代码块Cmd + Shift + Plus,Minus       Ctrl + Shift + Plus,Minus文件方法结构      Cmd + F12                       Ctrl + F12查找调用的位置     Ctrl + Option + H               Ctrl + Alt + H大小写转换       Cmd + Shift + U                 Ctrl + Shift + U

自动导包

GRADLE

9GAG/app/build.gradle

// 声明是Android程序apply plugin: 'com.android.application'android {    // 编译SDK的版本    compileSdkVersion 21    // build tools的版本    buildToolsVersion "21.1.1"    defaultConfig {        // 应用的包名        applicationId "me.storm.ninegag"        minSdkVersion 14        targetSdkVersion 21        versionCode 1        versionName "1.0.0"    }    // java版本    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_7        targetCompatibility JavaVersion.VERSION_1_7    }    buildTypes {        debug {            // debug模式        }        release {            // 是否进行混淆            minifyEnabled false            // 混淆文件的位置            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }    }    // 移除lint检查的error    lintOptions {        abortOnError false    }}dependencies {    // 编译libs目录下的所有jar包    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:support-v4:21.0.2'    compile 'com.etsy.android.grid:library:1.0.5'    compile 'com.alexvasilkov:foldable-layout:1.0.1'    // 编译extras目录下的ShimmerAndroid模块    compile project(':extras:ShimmerAndroid')}
    • 文件开头apply plugin是最新gradle版本的写法,以前的写法是apply plugin: ‘android’, 如果还是以前的写法,请改正过来。
    • buildToolsVersion这个需要你本地安装该版本才行,很多人导入新的第三方库,失败的原因之一是build version的版本不对,这个可以手动更改成你本地已有的版本或者打开 SDK Manager 去下载对应版本。
    • applicationId代表应用的包名,也是最新的写法,这里就不在多说了。
    • android 5.0开始默认安装jdk1.7才能编译,但是由于mac系统自带jdk的版本是1.6,所以需要手动下载jdk1.7并配置下,具体可以见我这篇博客Mac下安装和管理Java
    • minifyEnabled也是最新的语法,很早之前是runProguard,这个也需要更新下。
    • proguardFiles这部分有两段,前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明,免去了我们很多事,这个文件的目录在 /tools/proguard/proguard-android.txt , 后一部分是我们项目里的自定义的混淆文件,目录就在 app/proguard-rules.txt , 如果你用Studio 1.0创建的新项目默认生成的文件名是 proguard-rules.pro , 这个名字没关系,在这个文件里你可以声明一些第三方依赖的一些混淆规则,由于是开源项目,9GAG里并未进行混淆,具体混淆的语法也不是本篇博客讨论的范围。最终混淆的结果是这两部分文件共同作用的。
    • compile project(‘:extras:ShimmerAndroid’)这一行是因为9GAG中存在其他Module,不知道Module的概念可以看下这篇博客Android Studio系列教程二–基本设置与运行, 总之你可以理解成Android Library,由于Gradle的普及以及远程仓库的完善,这种依赖渐渐的会变得非常不常见,但是你需要知道有这种依赖的。
    • 以上文件里的内容只是基本配置,其实还有很多自定义部分,如自动打包debug,release,beta等环境,签名,多渠道打包等,后续会单独拿出来讲解。

9GAG/gradle

#Thu Dec 18 16:02:24 CST 2014distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https://services.gradle.org/distributions/gradle-2.2.1-all.zip

可以看到里面声明了gradle的目录与下载路径以及当前项目使用的gradle版本,这些默认的路径我们一般不会更改的,这个文件里指明的gradle版本不对也是很多导包不成功的原因之一。

9GAG/extras/ShimmerAndroid/build.gradle

每一个Module都需要有一个gradle配置文件,语法都是一样,唯一不同的是开头声明的是 apply plugin: ‘com.android.library’

9GAG/build.gradle

// 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.0.0'    }}allprojects {    repositories {        jcenter()    }}

内容主要包含了两个方面:一个是声明仓库的源,这里可以看到是指明的jcenter(), 之前版本则是mavenCentral(), jcenter可以理解成是一个新的中央远程仓库,兼容maven中心仓库,而且性能更优。另一个是声明了android gradle plugin的版本,android studio 1.0正式版必须要求支持gradle plugin 1.0的版本。

9GAG/settings.gradle

这个文件是全局的项目配置文件,里面主要声明一些需要加入gradle的module,我们来看看9GAG该文件的内容:

include ':app', ':extras:ShimmerAndroid'

文件中的 app, extras:ShimmerAndroid 都是module,如果还有其他module都需要按照如上格式加进去。

友盟多渠道打包

在AndroidManifest.xml里面会有这么一段:

<meta-dataandroid:name="UMENG_CHANNEL"android:value="Channel_ID" />

里面的Channel_ID就是渠道标示。我们的目标就是在编译的时候这个值能够自动变化。

第一步 在AndroidManifest.xml里配置PlaceHolder

<meta-dataandroid:name="UMENG_CHANNEL"android:value="${UMENG_CHANNEL_VALUE}" />

第二步 在build.gradle设置productFlavors

android {      productFlavors {        xiaomi {            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]        }        _360 {            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "_360"]        }        baidu {            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]        }        wandoujia {            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]        }    }  }

或者批量修改

android {      productFlavors {        xiaomi {}        _360 {}        baidu {}        wandoujia {}    }      productFlavors.all {         flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]     }}

很简单清晰有没有?直接执行 gradle assembleRelease , 然后就可以静静的喝杯咖啡等待打包完成吧。

assemble: 允许直接构建一个Variant版本,例如assembleFlavor1Debug。

assemble: 允许构建指定Build Type的所有APK,例如assembleDebug将会构建Flavor1Debug和Flavor2Debug两个Variant版本。

assemble: 允许构建指定flavor的所有APK,例如assembleFlavor1将会构建Flavor1Debug和Flavor1Release两个Variant版本。

项目中使用的完整的gradle文件配置

apply plugin: 'com.android.application'def releaseTime() {    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))}android {    compileSdkVersion 21    buildToolsVersion '21.1.2'    defaultConfig {        applicationId "com.boohee.*"        minSdkVersion 14        targetSdkVersion 21        versionCode 1        versionName "1.0"        // dex突破65535的限制        multiDexEnabled true        // 默认是umeng的渠道        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]    }    lintOptions {        abortOnError false    }    signingConfigs {        debug {            // No debug config        }        release {            storeFile file("../yourapp.keystore")            storePassword "your password"            keyAlias "your alias"            keyPassword "your password"        }    }    buildTypes {        debug {            // 显示Log            buildConfigField "boolean", "LOG_DEBUG", "true"            versionNameSuffix "-debug"            minifyEnabled false            zipAlignEnabled false            shrinkResources false            signingConfig signingConfigs.debug        }        release {            // 不显示Log            buildConfigField "boolean", "LOG_DEBUG", "false"            minifyEnabled true            zipAlignEnabled true            // 移除无用的resource文件            shrinkResources true            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            signingConfig signingConfigs.release            applicationVariants.all { variant ->                variant.outputs.each { output ->                    def outputFile = output.outputFile                    if (outputFile != null && outputFile.name.endsWith('.apk')) {                        // 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk                        def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"                        output.outputFile = new File(outputFile.parent, fileName)                    }                }            }        }    }    // 友盟多渠道打包    productFlavors {        wandoujia {}        _360 {}        baidu {}        xiaomi {}        tencent {}        taobao {}        ...    }    productFlavors.all { flavor ->        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    compile 'com.android.support:support-v4:21.0.3'    compile 'com.jakewharton:butterknife:6.0.0'    ...}

开发笔记

ctrl+alt f 全局变量
ctrl+j live + templates 模板
SystemClcok.sleep()==Thread.sleep();

@ViewInject(R.id.xxx) View.inject(context);=findeviewbyid(R.id.xxx);//注解

模拟器快捷键:

Esc 后退  F2 菜单

文本加密 text encrypt

templates 模板  Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) 内存卡是否存在 ProgressDialog 进度条dialog

创建桌面小部件

1.需要在清单文件里面配置元数据

<receiver android:name="ExampleAppWidgetProvider" ><intent-filter>    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-data android:name="android.appwidget.provider"           android:resource="@xml/" /></receiver>

2.需要配置当前元数据里面用到的xml res/xml

example_appwidget_info.xml

3.需要配置一个广播接收者

ExampleAppWidgetProvider

4.实现一个桌面小部件的xml(根据需求:桌面小控件长什么样子就实现什么样子)

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="40dp"android:minHeight="40dp"android:updatePeriodMillis="86400000"android:previewImage="@drawable/preview"android:initialLayout="@layout/example_appwidget"android:configure="com.example.android.ExampleAppWidgetConfigure" android:resizeMode="horizontal|vertical"android:widgetCategory="home_screen|keyguard"android:initialKeyguardLayout="@layout/example_keyguard"></appwidget-provider>
桌面小部件管理 AppWidgetManager  远程view 设置文本view.setTextViewText(R.id.xxx,"xxxx"); 设置onclick()为 view.setonclickpengdingIntent(xxx,xxx);
按照比例变化: ScaleAnimation sa=new ScaleAnimation(0.5f, 1f, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);旋转:RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,            0.5f);    // 设置动画的时间    rotateAnimation.setDuration(5000);    // 设置动画无限循环    rotateAnimation.setRepeatCount(Animation.INFINITE);
隐藏键盘 editortext.setinputtype(Inputtype.type.null);

更多相关文章

  1. Android(安卓)ORM框架GreenDao用法
  2. Android系统权限和root权限
  3. 【Android】Android(安卓)SDK下载和更新失败的解决方法!!!
  4. 【Android(安卓)系统开发】 编译 Android文件系统 u-boot 内核
  5. 彻底解决Android(安卓)应用方法数不能超过65K的问题
  6. android google map apiKey 相关操作
  7. AndroidManifest.xml中 andorid 版本号 versionCode, versionNam
  8. Android(安卓)studio开发找不到HttpClient问题
  9. Android(安卓)-- android.support.v7.internal.widget.ActionBar

随机推荐

  1. android 调试elf程序脚本
  2. Android之Tab类总结
  3. android camera: android.hardware.camer
  4. android 5.1 usb调试默认关闭设置方法
  5. Android(安卓)请求WebService 并且解析
  6. android TextWatcher
  7. android之Radio状态
  8. android下载封装类Download,支持断点下载
  9. eclipse 运行Android程序出错解决办法
  10. Android(安卓)网页无法打开 net:ERR_UNKN