前提
Android Studio版本
Android Studio Bumblebee | 2021.1.1 Patch 2
Build #AI-211.7628.21.2111.8193401, built on February 17, 2022
Runtime version: 11.0.11+0-b60-7590822 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 12
Registry: external.system.auto.import.disabled=true, debugger.watches.in.variables=false
Non-Bundled Plugins: com.huangmb.idea (1.1), Dart (211.7808), com.localizely.flutter-intl (1.17.1-2020.3), org.moe.community (1.4.10), org.jetbrains.kotlin (211-1.6.10-release-923-AS7442.40), io.flutter (65.2.2), org.intellij.plugins.markdown (211.7142.37)
工程配置,targetSdk 32
,支持Android12
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.espprovisiondemo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.11'
}
报错一
安卓12或者更高版本,新建的android工程,直接run,有可能会报下面的错
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8
Failed to apply plugin 'com.android.internal.application'.
Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changingorg.gradle.java.home
ingradle.properties
.
解决办法
Android Studio --> Preferences --> Build,Execution,Deployment -->Build Tools --> Gradle 选择Java11的SDK
报错二
安卓12或者更高版本,新建的android工程,直接run,安装到手机上有可能会报下面的错
Installation did not succeed. The application could not be installed: INSTALL_FAILED_TEST_ONLY
解决办法
在项目根目录下,找到gradle.properties文件,并打开,添加下面的代码,
android.injected.testOnly=false
添加完成,点击右上角,同步一下,如下图所示
报错三
安卓12或者更高版本,用Gradle的方式,导入第三方库时可能会遇到下面报错
Failed to resolve:XXX
,导入第三方库没有成功
案例:
用Gradle的方式,集成第三方库ESP32到工程里面
按照第三方库ESP32作者的指导,
- 在工程根路径下的
build.gradle
,加入如下代码maven { url 'https://jitpack.io' }
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- 在app目录下
build.gradle
,加入如下代码
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.11'
按正常是会集成成功,但结果是如下报错Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
解决办法
在工程根路径下的
build.gradle
,加入如下代码maven { url 'https://jitpack.io' }
上面的第一步,不应该加在根路径下的build.gradle
,而是加到根路径下的settings.gradle
,这个应该是安卓12或者高版本的配置要求,如下:
这个时候,再去同步一下,就可以了。在Activity里面尝试引入,如下,发现已经有提示,成功了!!!
报错四
2 files found with path 'META-INF/DEPENDENCIES' from inputs:
报错
> Task :app:mergeDebugJavaResource FAILED
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/DEPENDENCIES' from inputs:
- C:\Users\admin\.gradle\wrapper\dists\gradle-6.8-bin\caches\transforms-3\c9524869f844e5aa0e4c08903f15e41f\transformed\jetified-httpclient-4.5.6.jar
- C:\Users\admin\.gradle\wrapper\dists\gradle-6.8-bin\caches\transforms-3\d37663de769a65fb667ce308a29d0c3c\transformed\jetified-httpcore-4.4.10.jar
Adding a packagingOptions block may help, please refer to
https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
for more information
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
解决办法
在报该编译错误的module的build.gradle中加入如下配置项,排除掉中间生成的META-INF/DEPENDENCIES文件 ,
android {
packagingOptions
{
exclude'META-INF/DEPENDENCIES'
}
}
结尾
今天的分享至此接近尾声喽,小伴们,觉得有点用的话,或者已经看到这里面来的请点赞加关注吧~~
后续分享更多Android原生技术及物联网技术相关文章。如果有任何疑问的话,欢迎在下方留言~