【问题标题】:Build and Run an old Android Project in Android Studio在 Android Studio 中构建并运行旧的 Android 项目
【发布时间】:2021-07-16 12:30:06
【问题描述】:

我收到了集成到 AWS 服务的 Android 项目的压缩项目源代码。我需要了解并能够向应用程序添加一些新功能,但我无法在 android studio 中成功运行该项目。我正在使用 Android Studio 4.1.3。

我是 Android 和 AWS 的新手,我只是在学习这些技术。

我应该如何以及从哪里开始研究代码?

另外,如果您能帮我解决我在 Android Studio 中遇到的问题。 我已经尝试杀死所有 gradle 守护进程并杀死所有 java 进程,但我仍然遇到同样的问题。

Android Studio 问题

无法找到方法 'org.gradle.api.internal.file.DefaultSourceDirectorySet.(Ljava/lang/String;Ljava/lang/String;Lorg/gradle/api/internal/file/FileResolver;Lorg/ gradle/api/internal/file/collections/DirectoryFileTreeFactory;)V' org.gradle.api.internal.file.DefaultSourceDirectorySet.(Ljava/lang/String;Ljava/lang/String;Lorg/gradle/api/internal/file/FileResolver;Lorg/gradle/api/internal/file/collections/DirectoryFileTreeFactory ;)V

Gradle 的依赖缓存可能已损坏(这有时会在网络连接超时后发生。)

重新下载依赖并同步项目(需要网络) Gradle 构建过程(守护进程)的状态可能已损坏。停止所有 Gradle 守护进程可能会解决此问题。

停止 Gradle 构建过程(需要重新启动) 您的项目可能正在使用与项目中的其他插件或项目请求的 Gradle 版本不兼容的第三方插件。

在 Gradle 进程损坏的情况下,您也可以尝试关闭 IDE,然后杀死所有 Java 进程。

下面是 build.gradle(项目)

buildscript {
ext.kotlin_version = '1.3.31'
repositories {
    google()
    jcenter()
    
}
dependencies {
    classpath 'com.android.tools.build:gradle:4.1.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.amazonaws:aws-android-sdk-appsync-gradle-plugin:2.9.+'
}}
allprojects {
repositories {
    google()
    jcenter()  }}
task clean(type: Delete) {delete rootProject.buildDir}

下面是 build.gradle (app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.amazonaws.appsync'
android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.dostcandle.ecrf"
    minSdkVersion 24
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
dataBinding {
    enabled = true
}}
dependencies {
def lifecycle_version = "2.0.0"
def room_version = "2.2.1"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.fragment:fragment-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.amazonaws:aws-android-sdk-core:2.15.+'
kapt "androidx.room:room-compiler:$room_version"
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.jaredrummler:material-spinner:1.3.1'
implementation 'ph.ingenuity.tableview:tableview:0.1.0-alpha'

implementation 'com.amazonaws:aws-android-sdk-appsync:2.8.+'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'com.amazonaws:aws-android-sdk-auth-ui:2.15.+'

implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.15.+'
implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.15.+'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'}

【问题讨论】:

标签: android android-studio kotlin build.gradle gradle-kotlin-dsl


【解决方案1】:

您使用的是与 Android Gradle Plugin 4.x / Gradle 6+ 不兼容的旧版 AppSync SDK。现在,我看到您使用的是 AGP 4.x。

更多详情请见this GitHub issue

你有两个选择:

  1. 使用旧版本的 Gradle 和 Android Gradle 插件。具体来说,插件的 3.6.3 和 Gradle 的 5.6.4。

  2. 将您的 AppSync 依赖项更新到至少 3.1+。详情请见the setup notes in the project's README

来源:我在 AppSync SDK 中编写了此问题的修复程序。

【讨论】:

  • 谢谢 Jameson,我已更新 AWS AppSync SDK 设置。该应用程序正在构建,但在打开时崩溃。我认为问题出在 AWS Amplify 设置上。我看到了一篇文章,其中包含一个贬低的 AWS Amplify 设置,而那是应用程序中的一个。您能否指出我如何将 AWS Amplify 设置更新到最新的正确方向?
  • @akgm 您实际上并没有使用 Amplify——您使用的是适用于 Android 的 AWS 移动开发工具包。Android 开发工具包的最新版本是 2.23.0。 (请参阅 github.com/aws-amplify/aws-sdk-android/releases/tag/… 。)要更新,请找到所有依赖项,例如 com.amazonaws.aws-android-sdk*。每 2.15.+ 更改为 2.23.0。如果它对您有所帮助,请随时支持/接受此解决方案。
【解决方案2】:

应用这些:

id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'com.amazonaws.appsync'

而不是这些:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.amazonaws.appsync'

如果仍然无法正常工作,那么只需添加以下内容:

android {
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

【讨论】:

  • 谢谢!试过这个,但我得到了这个错误。 构建文件 '/home/liyah/AndroidStudioProjects/ecrf/app/build.gradle' 行:11 评估项目 ':app' 时出现问题。 > 在 org.gradle.api.Project 类型的项目 ':app' 上找不到参数 [com.android.application] 的方法 id()。
【解决方案3】:

创建新项目大部分代码应该已经过时了。如果您想运行现有代码,请将 jcenter() 更改为 mavenCentral() 还检查 aws 文档https://aws.amazon.com/getting-started/hands-on/build-android-app-amplify/

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.32"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        // Add this line into `dependencies` in `buildscript`
        classpath 'com.amplifyframework:amplify-tools-gradle-plugin:1.0.2'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}
apply plugin: 'kotlin-kapt'
apply plugin: 'com.amplifyframework.amplifytools'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.dostcandle.ecrf"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    implementation 'com.amplifyframework:aws-api:1.17.3'
    implementation 'com.amplifyframework:aws-datastore:1.17.3'

    implementation 'com.jaredrummler:material-spinner:1.3.1'
    implementation 'ph.ingenuity.tableview:tableview:0.1.0-alpha'

    implementation 'com.amazonaws:aws-android-sdk-appsync:2.8.+'
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
    implementation 'com.amazonaws:aws-android-sdk-auth-ui:2.22.6'

    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.22.6'
    implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.22.6'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2019-04-26
    相关资源
    最近更新 更多