我是一个Android/Java开发的新手,我买了一本书“Java Programming for Android Developers For Dummies,第二版”。
这本书附带了代码示例部分http://users.drew.edu/bburd/Java4Android/ (02_01章),我知道这本书已经有几年的历史了,因此代码示例将是。然而,我在Android Studio 4.1.2上运行代码示例时遇到了大量问题。
我可以导入项目并同步Gradle文件。当我这样做的时候,它会在底部出现一个绿色的勾号。奇怪的是,它在旁边显示失败,并显示以下消息。
Gradle sync failed: Unsupported method: SyncIssue.getMultiLineMessage().
The version of Gradle you connect to does not support that method.
To resolve the problem you can change/upgrade the target version of Gradle you connect to.
Alternatively, you can ignore this exception and read other information from the model.
Consult IDE log for more details (Help | Show Log) (1 s 443 ms)
我的build.gradle文件如下所示
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
另外,如果我尝试运行该项目,我会看到以下屏幕
我读过许多类似问题的帖子,并完成了各种修复。似乎都不起作用,所以有没有人能解释一下发生了什么?我还在3台不同的PC上下载了android studio,以防它是某种bug。
谢谢你
发布于 2021-02-12 08:14:25
我应该说里面的文件已经非常过时了,而且你在每个项目上都会遇到很大的困难。但是,如果你仍然想要构建这个项目,这里有一些你可以做的事情。
查找文件gradle-wrapper.properties
并用下面的代码更新它
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
然后移动到项目级别的build.gradle
文件(有两个,一个在app目录内,一个在根目录下,使用根目录)
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
然后移动到应用程序级别的build.gradle
并使用以下内容进行更新
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.allyourcode.a02_01"
minSdkVersion 15
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.13.1'
}
然后同步项目,让Android studio施展它的魔力。
发布于 2021-02-12 08:06:03
正如堆栈跟踪所说,您应该升级您的Gradle版本。您正在使用一本“旧”书,其中的示例是使用Android Studio的旧版本构建的,该版本很好地支持旧版本的Gradle。但4.1是一个新版本,并不能很好地支持许多旧的Gradle特性。
https://stackoverflow.com/questions/66164623
复制相似问题