1、使用 idea 创建 android 工程
2、 配置构建 sdk
3、配置 gradle
a、进入 gradle 官网,选择 install (默认是最新版本)
b、选择包管理安装,手动安装选择下面一个即可
c、安装 sdk 并通过 sdk 安装 gradle
安装 sdk:curl -s "https://get.sdkman.io" | bash
根据安装提示,执行 source 命令使 sdkman 生效
source "/Users/xxxx/.sdkman/bin/sdkman-init.sh"
然后执行 gradle 安装命令
安装成功
4、下载软件 Genymotion,并在IDEA安装一个Genymotion插件与之关联起来
a、下载安装 adb
模拟器下载地址:Genymotion Desktop download - Genymotion Android Emulator
配置自己下载安装的 android sdk 路径
b、安装插件
配置模拟器路径
5、配置完成执行命令打包构建
gradle assembleDebug 打包出现异常如下
Starting a Gradle Daemon (subsequent builds will be faster)FAILURE: Build failed with an exception.* What went wrong:
A problem occurred configuring root project 'My Application'.
> Could not resolve all files for configuration ':classpath'.> Could not resolve com.android.tools.build:gradle:8.2.2.Required by:project : > com.android.application:com.android.application.gradle.plugin:8.2.2> No matching variant of com.android.tools.build:gradle:8.2.2 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.6' but:- Variant 'apiElements' capability com.android.tools.build:gradle:8.2.2 declares a library, packaged as a jar, and its dependencies declared externally:- Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8- Other compatible attribute:- Doesn't say anything about org.gradle.plugin.api-version (required '8.6')- Variant 'javadocElements' capability com.android.tools.build:gradle:8.2.2 declares a component for use during runtime, and its dependencies declared externally:- Incompatible because this component declares documentation and the consumer needed a library- Other compatible attributes:- Doesn't say anything about its target Java version (required compatibility with Java 8)- Doesn't say anything about its elements (required them packaged as a jar)- Doesn't say anything about org.gradle.plugin.api-version (required '8.6')- Variant 'runtimeElements' capability com.android.tools.build:gradle:8.2.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:- Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8- Other compatible attribute:- Doesn't say anything about org.gradle.plugin.api-version (required '8.6')- Variant 'sourcesElements' capability com.android.tools.build:gradle:8.2.2 declares a component for use during runtime, and its dependencies declared externally:- Incompatible because this component declares documentation and the consumer needed a library- Other compatible attributes:- Doesn't say anything about its target Java version (required compatibility with Java 8)- Doesn't say anything about its elements (required them packaged as a jar)- Doesn't say anything about org.gradle.plugin.api-version (required '8.6')* Try:
> Review the variant matching algorithm at https://docs.gradle.org/8.6/userguide/variant_attributes.html#sec:abm_algorithm.
> No matching variant errors are explained in more detail at https://docs.gradle.org/8.6/userguide/variant_model.html#sub:variant-no-match.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.BUILD FAILED in 22s
根据异常提示:Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
发现gradle 使用的是默认的 jdk8 版本,与工程要求的 11 版本不一致
2种解决方式如下:
1、gradle.properties 中 指定 jdk 版本:org.gradle.java.home=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home
2、在命令行中指定
gradle -Dorg.gradle.java.home=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home assembleDebug
解决后再次打包即可完成 !!!