Android Studio 的一些 常用操作和 编译报错 整理(持续更新中...)
目录
Android Studio 的一些 常用操作和 编译报错 整理(持续更新中...)
一、Android studio 把一段代码快速提取,构建一个函数接口的方法
二、Android Studio 删除一个不要的 module 模块
三、Android Studio Debug调试一张图简单说明主要功能
四、Android Studio 3. 2 版本的多渠道打包的错误处理
五、Android Studio 杀死自己进程或者杀死指定进程
六、Android Studio 3.2 引入 aar 包进工程
七、@SuppressLint("NewApi") 和 @TargetApi() 的作用
八、解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"错误
十、关于Android Studio3.2新建项目无法运行出现Failed to find Build Tools revision 28.0.2的解决方法
十二、由于未及时更新Android 的一些库,可能会导致报错(可能也会导致Unity编包也会报错),这是可以使用国内的资源网址
十四、Android Studio 快速把代码块定义为 region 块
一、Android studio 把一段代码快速提取,构建一个函数接口的方法
当我们在一个地方写了很多代码,想把它提取出来,放在自己定义的一个方法里面,只需要选中一些代码,然后操作快捷键 Cmd + Option + M(Mac) 就ok了,Windows是 Ctrl + Alt + M(如果你之前有把快捷键匹配成了eclipse的那么使用Alt+shift+M)。
二、Android Studio 删除一个不要的 module 模块
三、Android Studio Debug调试一张图简单说明主要功能
四、Android Studio 3. 2 版本的多渠道打包的错误处理
原编写:
productFlavors{
wandoujia{
//manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
xiaomi{
//manifestPlaceholders=[UMENG_CHANNEL_VALUE: "xiaomi"]
}
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
1)报错:
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
错误:
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=release, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
修改为:
productFlavors{
wandoujia{
//manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
}
xiaomi{
//manifestPlaceholders=[UMENG_CHANNEL_VALUE: "xiaomi"]
}
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
applicationVariants.all { variant ->
variant.outputs.all {
def fileName
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
if (variant.buildType.name.equals('release')) {
fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}-${formattedDate}.apk")
}
else if (variant.buildType.name.equals('debug')) {
fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}-${formattedDate}.apk")
}
outputFileName = fileName;
}
}
2)报错:
提示:Error:All flavors must now belong to a named flavor dimension.Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
这个一个错误,意思是:所有的flavors都必须属于同一个风格。
提示:Error:All flavors must now belong to a named flavor dimension.Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html
这个一个错误,意思是:所有的flavors都必须属于同一个风格。
添加代码:
在 defaultConfig 添加 flavorDimensions "versionCode",版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
defaultConfig {
applicationId "com.example.xan.testjsk"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}
五、Android Studio 添加 aar 依赖的两种方式
1)方式一
android{
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation (name: 'XX_library_vx.x.x', ext: 'aar')
}
2)方式二
android{
//不用写
/* repositories {
flatDir {
dirs 'libs'
}
}*/
}
dependencies {
将
implementation fileTree(dir: 'libs', include: ['*.jar'])
改为
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
}
五、Android Studio 杀死自己进程或者杀死指定进程
1)杀死自己
android.os.Process.killProcess(android.os.Process.myPid());
2)杀死指定进程
a) activityManager.killBackgroundProcesses(不能杀死自己)
(注意:需要权限:<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>)
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.android.mms");
b) activityManager.restartPackage("com.diandian")
(注意:需要权限: <uses-permission android:name="android.permission.RESTART_PACKAGES"/>)
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
activityManager.restartPackage("com.diandian");
六、Android Studio 3.2 引入 aar 包进工程
1)方法一:使用 implementation 添加 (最后记得 sync now)
2)添加 ‘*.aar’ (最后记得 sync now)
七、@SuppressLint("NewApi") 和 @TargetApi() 的作用
在我们日常开发Android当中,经常遇到写了一段正常的代码,但是系统报错,然后根据系统提示,会帮我们自动添加一个@SuppressLint(“NewApi”),
然后错误就撤销了原因是我们代码中使用了比我们所设置的
android:minSdkVersion= (即兼容最低版本)要高的方法
一般解决方法是在方法上加上@SuppressLint(“NewApi”)或者@TargetApi()或者设置更高的最低版本
1.@SuppressLint(“NewApi”)屏蔽一切因版本而导致新api中才能使用的方法报的android lint错误
2.@TargetApi() 屏蔽部分因版本而导致新api中才能使用的方法报的android lint错误
八、解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"错误
安装了Android Studio 3.2,打开一个旧工程,编译提示"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"
对新版NDK的研究,发现NDK的更新记录里有一段话
This version of the NDK is incompatible with the Android Gradle plugin
version 3.0 or older. If you see an error like
`No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android`,
update your project file to [use plugin version 3.1 or newer]. You will also
need to upgrade to Android Studio 3.1 or newer.
也就是说新版本的NDK与3.0及以前旧版的Android Gradle plugin插件不兼容
其实解决方法很简单,就是修改build.gradle中的红字部分,改为3.1以上版本即可
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
九、Android Studio CPP/C 报错 calling convention '__stdcall' ignored for this target [-Wignored-attributes]
解决方法:
Your best course of action is to use empty macros to make them go away.
#define __stdcall
十、关于Android Studio3.2新建项目无法运行出现Failed to find Build Tools revision 28.0.2的解决方法
解决: 去所创建项目下的app>build.gradle,打开build.gradle如下图 ,添加
buildToolsVersion "28.0.2"
十一、Android设置自己的应用为launcher
效果是让自己的应用在安卓设备启动后自动启动且显示为桌面(替换系统默认的launcher);
操作十分简单,在mainactivity的intent-fliter标签里添加下面的代码;
在AndroidManifest.xml 中添加,如下属性
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
这样重启以后系统会询问你是不是始终打开这个应用,或是仅此一次。
如果点了始终,就退不回以前的桌面了,想要修改的话,建议把上面四句话删除重新安装进安卓设备,以覆盖之前的应用。
十二、由于未及时更新Android 的一些库,可能会导致报错(可能也会导致Unity编包也会报错),这是可以使用国内的资源网址
buildscript {
repositories {
/*jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()*/
maven {url 'https://maven.aliyun.com/repository/google'}
maven {url 'https://maven.aliyun.com/repository/jcenter'}
maven {url 'https://maven.aliyun.com/nexus/content/groups/public'}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
/*jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}*/
maven {url 'https://maven.aliyun.com/repository/google'}
maven {url 'https://maven.aliyun.com/repository/jcenter'}
maven {url 'https://maven.aliyun.com/nexus/content/groups/public'}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
十三、Android Studio 生成函数注释的实现方法
1、函数名上输入
---> /**,然后回车,即自动生成基本注释(参数,返回等)
2、使用快捷键生成
---> 鼠标停留在函数体内,按下设置的快捷键即可生成基本注释
快捷键设置方法:在"Fix doc comment"上右键进行快捷键设置。
十四、Android Studio 快速把代码块定义为 region 块
1、window 上的快捷方式
-
先选择要折叠的一组代码
-
按下快捷键Ctrl+Alt+T
-
在弹出的菜单中选择region...endregion Comments
2、Mac 上的操作
-
将某一功能的多个方法或变量放在一起
-
选中该区域的代码,按快捷键
cmd +option + z
( surround with... ) -
选择
region...endregion Comments
这样就会自动在选中的代码最开始会最后生成region和endregion注释,自己再添加下分组的注释说明就ok了。