ERROR: Could not find method repository() when using OneDrive SDK

ERROR: Could not find method repository() when using OneDrive SDK

我正尝试按照显示的说明使用 OneDrive SDK here。不幸的是,当我添加 SDK 安装指南中显示的依赖项时,我无法执行 Gradle 项目同步。我收到以下错误消息:

ERROR: Could not find method repository() for arguments [build_xqhe0znpcz2uhmxa019kwrtr$_run_closure2@18623fec] on project ':app' of type org.gradle.api.Project.

有人可以帮我找出问题所在吗?我的 build.gradle 文件如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.onedrivesdkapp"
        minSdkVersion 16
        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'
        }
    }
}

repository {
    jcenter()
}

dependency {
    // Include the sdk as a dependency
    compile ('com.onedrive.sdk:onedrive-sdk-android:1.3+') {
        transitive = false
    }

    // Include the gson dependency
    compile ('com.google.code.gson:gson:2.3.1')

    // Include supported authentication methods for your application
    compile ('com.microsoft.services.msa:msa-auth:0.8.+')
    compile ('com.microsoft.aad:adal:1.1.+')
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

似乎 onedrive-sdk-android 库不再维护,因为最新提交是 2 年前。

问题: 您导入依赖项的代码不正确,这就是 Gradle 抛出错误的原因。

解决方法:执行以下步骤。

步骤 1:从 gradle 文件中删除以下行

repository {
    jcenter()
}

dependency {
    // Include the sdk as a dependency
    compile ('com.onedrive.sdk:onedrive-sdk-android:1.3+') {
        transitive = false
    }

    // Include the gson dependency
    compile ('com.google.code.gson:gson:2.3.1')

    // Include supported authentication methods for your application
    compile ('com.microsoft.services.msa:msa-auth:0.8.+')
    compile ('com.microsoft.aad:adal:1.1.+')
}

第 2 步: 将所有依赖行移动到 dependencies 块。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // Include the sdk as a dependency
    implementation ('com.onedrive.sdk:onedrive-sdk-android:1.3+') {
        transitive = false
    }

    // Include the gson dependency
    implementation ('com.google.code.gson:gson:2.3.1')

    // Include supported authentication methods for your application
    implementation ('com.microsoft.services.msa:msa-auth:0.8.+')
    implementation ('com.microsoft.aad:adal:1.1.+')
}

放在一起,您的 gradle 文件看起来像

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.onedrivesdkapp"
        minSdkVersion 16
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // Include the sdk as a dependency
    implementation ('com.onedrive.sdk:onedrive-sdk-android:1.3+') {
        transitive = false
    }

    // Include the gson dependency
    implementation ('com.google.code.gson:gson:2.3.1')

    // Include supported authentication methods for your application
    implementation ('com.microsoft.services.msa:msa-auth:0.8.+')
    implementation ('com.microsoft.aad:adal:1.1.+')
}