小白使用 Android Studio 遇到的问题
初次使用 Android Studio 开发新工程,需要配置一些环境变量,比如 SDK 和工程在硬盘存放的目录、需要支持的版本等,都需要和本机安装的 Android Studio 相匹配,但是有时候因为无意间的升级,可能会出现新建项目依然不能正常运行的情况,这就需要我们对 Android Studio 手动修改。下面记录几个常见的问题及相应的解决方式。
1 构建版本和 Android Studio 不匹配
Error:(26, 13) Failed to resolve: com.android.support:appcompat-v7:27.+
上面的问题是说我们升级的 Android API 和 Android SDK Build-Tools 不匹配。

我本机安装的 Android Studio 版本是 2.3.3,它使用的构建版本 Android SDK Build-Tools 是 26.0.1,能编译出的最高 API 版本为 26。修改 app 模块下的 build.gradle 文件,调整好版本,点击 Sync 同步构建就能顺利解决。
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.zhudongdong.explain"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
2 点击 Sync 后,报 java 空指针错误:
Unexpected exception during symbol building (rebuildModuleMaps)
java.lang.NullPointerException
at com.jetbrains.cidr.lang.workspace.OCWorkspaceManager.getWorkspace(OCWorkspaceManager.java:12)
解决方法:需要禁用 Android Studio 插件 Android NDK Support
,去掉勾选项。估计这是 Android Studio 自身的一个 bug ,知道原因的小伙伴,还请留言交流。

3 测试单元下报库文件冲突
Error:Conflict with dependency 'junit:junit' in project ':app'. Resolved versions for app (4.10) and test app (4.12) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
当使用测试单元时, Android Studio 自动从引入的 External Libraries
中寻找文件,而默认的 External Libraries 包含了测试单元的 4.10
和 4.12
两个版本。
解决方式:可以在 app 模块下的 build.gradle 文件中强制指定测试单元的版本。
android {
configurations.all {
resolutionStrategy.force 'junit:junit:4.12'
}
...
}
以上是近两天使用 Android Studio 新建工程出现的问题,以后有问题继续补充记录,遇到问题的朋友也可以留言交流。