AndroidStudio 3.0 implementation、api

api 指令 完全等同于compile指令

implementation指令
使用了这个命令编译的依赖,对该项目由依赖的项目将无法访问到使用该命令编译的依赖,也就是将该依赖隐藏在内部,而不对外部公开。
简单的说,就是使用implementation指令的依赖不会传递

Android Studio 报错显示 mips64el-linux-android-strip 找不到

Android/Sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/bin/mips64el-linux-android-strip 找不到, 导致编译报错

也就是说在Android/Sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/ 路径下找不到 mips64el-linux-android-strip
如果ndk版本在r17版, 很有可能出现这个问题. 我的解决方案是:

  1. 先清除 Android/Sdk/ndk-bundle/ 下的内容
  2. https://developer.android.google.cn/ndk/downloads/older_releases 下载16b版本的ndk到本地, 并解压说, 将解压缩后的所有文件拷贝到 Android/Sdk/ndk-bundle/ 目录下
  3. 重新build工程

Android Studio利用Gradle删除没有使用到的资源和代码文件

一、打包时忽略无用资源

  我们在打包的时候默认会把没有用到的资源(比如图片)也打包成app,徒增了应用的大小。现在我们可以利用Gradle来优雅的去除没有用到的资源文件了!

就是在gradle中配置shrinkResources true。这个东西依赖于minifyEnabled,所以minifyEnabled也要为true才行。

官方推荐在正式版中这么写:

android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
}

如果你觉得debug包也太大,可以按照下面的方式写:

buildTypes {
debug {
minifyEnabled true
shrinkResources true
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

我通过测试确实可以大大减少应用的大小,但也发现了一个问题,很多没有用到的类还是会被打包进去。比如你用了lib,lib里有很多很多类,你可能就用到了一个类,但却把lib中的所有类都打包了进去,很不合理。而且因为去除无效代码的功能要依赖于混淆,混淆又是一个耗时的操作,还是没有彻底解决打包慢的问题。

二、打包时忽略无用的代码
我目前的需求是很多工具类都在一个lib中,但我平常只用到lib中的几个类,所以希望生成apk的时候自动拍出lib中没有用到的类。于是我想到了下面的办法:

1.我首先在debug模式下配置一下,在debug时也进行混淆,并且自定义了混淆的配置文件debug-proguard-rules.pro

buildTypes {
debug {
minifyEnabled true // 是否混淆
shrinkResources true // 是否去除无效的资源文件
proguardFiles getDefaultProguardFile('proguard-android.txt'),'debug-proguard-rules.pro'
}
release {
minifyEnabled true // 是否混淆
shrinkResources true // 是否去除无效的资源文件
// 混淆的配置文件
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

这样在debug的时候也会进行混淆,但我仅仅需要混淆lib包中的代码,而不混淆我app项目下的代码。这样就可以删除掉lib中无用的代码了。测试下来可以保证只有用到的东西才会被打包进来,其余的东西都没有被打包,效果不错!

解决 Could not find com.android.tools.build:gradle:3.0.0-alpha1 in circle ci

解决 Could not find com.android.tools.build:gradle:3.0.0-alpha1 in circle ci

如下:

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath 'com.tencent.bugly:symtabfileuploader:latest.release'
    }
}

修改成

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://dl.google.com/dl/android/maven2'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath 'com.tencent.bugly:symtabfileuploader:latest.release'
    }
}

https://blog.v9gg.com/myweishanli/article/623

androidstudio3.0 保存

Error:Execution failed for task ‘:app:javaPreCompileDebug’.

Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor. Please add them to the annotationProcessor configuration.
– auto-service-1.0-rc3.jar (com.google.auto.service:auto-service:1.0-rc3)
Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior. Note that this option is deprecated and will be removed in the future.
See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.

解决:
compileSdkVersion 25
buildToolsVersion ‘26.0.2’
defaultConfig {

applicationId “com.shijiusui.p.xxx”
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName “1.0”
testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”

multiDexEnabled true
javaCompileOptions {

annotationProcessorOptions {

includeCompileClasspath = true

}

}

}

Android Studio Windows上常用快捷键

Ctrl+Y 删除行 delete line
Ctrl+N 搜索类
Ctrl+Alt+L 格式化
Alt+? 提示
Alt+Enter 导包

Search Everywhere Double Shift
Project View alt+1
go to file ctrl+shift+N
recent files ctrl+E
navigation bar alt+home
drop files here to open

Ctrl+Shift+U 大小写切换
Shift+Shift 全局搜索
End 行尾 move caret to line end
Shift+End move caret to line end with selection
Home
Shift+Home

Ctrl+G 跳转 go to line

Android Studio Mac上常用快捷键

Android Studio Mac上常用快捷键

⌃:control
⇧:shift
⌥:alt(option)
⌘:cmd(command)

重构(提取为方法):
option+command+M

全局查找:
shift+command+F

搜索并替换:
command+R

大小写切换:
shift+command+U

导包:
option+Enter

全局找文件:
double shift

编译并运行
control+R

删除行:
command+X

全屏:
control+command+F

跳转至第几行:
cmd + L

引入重写父类的方法:
ctr + O

引入接口或抽象类方法的实现:
ctr + I

切换文件:
ctr + tab

局部代码块展开/收缩:
cmd + + / cmd + –

全部代码块展开/收缩:
sft + cmd + + / sft + cmd + –

类名/方法名/变量名 重命名操作:
sft + fn + F6

方法重构,方法抽离:
opt + cmd + M

抽离成方法参数:
opt + cmd + P

抽离为局部变量:
opt + cmd + V

抽离为成员变量:
opt + cmd + F

Android Studio代码行数统计插件Statistics

Android Studio 是没有提提供统计代码全部行数的功能的,但是对于开发者来说,这个功能确实必备的,Statistic统计代码行数非常方便,也很详细。

首先肯定是将插件下载下来,下载地址:https://plugins.jetbrains.com/plugin/4509

也可以直接从插件中搜索

首次安装在AS的View→Tool Windows→Statistic,选择之后会在AS的左下角出现statistic按钮。

一开始里面的内容是空白的,我们点击Refresh,如果还是空白,就点击Settings来添加需要统计行数的项目。

Android Stduio 统计代码行数

android studio统计项目的代码行数的步骤如下:
1. 在弹出Find in Path的框中的Text to find输入\n,接着勾选Regular expression(正则表达式),Context选择anywhere,
Scope根据你想要统计的范围进行选择,File mask选择*.java。(在这里统计项目的Java的代码行数)

2. 下图的Useages in generated code是自动生成的代码,上面那个就是项目的代码行数。