将项目从Android Studio版本2转换为3-配置问题

时间:2018-09-19 14:00:36

标签: android android-studio gradle

我在配置android项目时遇到问题。我将Android Studio [AS]从v2升级到了v3,但遇到了很多问题。我在文档中更改了gradle: gradle-wrapper,gradle插件版本,存储库google或jcenter而不是maven。请看配置: 我有一个名为mytwayapp的项目,其中有build.gradle:

  buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        //{ url "http://jcenter.bintray.com/" }
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

    allprojects {
        repositories {
            google()
            jcenter()
        }
    }

    dependencies {
        implementation project(':app')
    }

接下来,我使用build.gradle创建了一个名为app的模块:

apply plugin: 'com.android.application'


android {
    compileSdkVersion 23
    buildToolsVersion '27.0.3'
//    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "com.mytway.activity.registerformactivity"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        preDexLibraries = false;
        javaMaxHeapSize "2g"
    }
    sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/res/assets/fonts'] } }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    lintOptions {
        abortOnError false
    }


}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.android.gms:play-services:8.4.0'
    implementation 'com.android.support:appcompat-v7:23.1.1'
    implementation 'com.android.support:support-v4:23.1.1'
    implementation 'com.android.support:support-v13:23.1.1'
    implementation 'joda-time:joda-time:2.9.4'
    implementation files('../libs/MPAndroidChart-v3.0.1.jar')

    //to remove when Dex is over memory
    testImplementation group: 'org.powermock', name: 'powermock-api-easymock', version: '1.6.6'
    testImplementation 'org.powermock:powermock-mockito-release-full:1.5.4'
    testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
    testImplementation 'org.powermock:powermock-module-junit4:1.5.1'
    testImplementation 'org.mockito:mockito-all:1.9.5'
    testImplementation 'org.powermock:powermock-mockito-release-full:1.6.1'
    testImplementation 'org.easymock:easymock:3.1'
//    testCompile files('build/lib/powermock-easymock-1.6.6-full.jar') 
    testImplementation 'junit:junit:4.12'
}

接下来是gradle-wrapper:

#Thu Mar 16 20:14:22 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

问题: 我无法与gradle同步,如果尝试则出现错误: 无法初始化com.android.tools.idea.gradle.eclipse.GradleImport类

当我尝试从cmd安装时,然后:

./gradlew install -x test

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\android\BitBucket\mytwayapp\build.gradle' line: 22

* What went wrong:
A problem occurred evaluating root project 'mytwayapp'.
> Could not find method implementation() for arguments [project ':app'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
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 2s

这意味着问题在于主build.gradle带有对“:app”的依赖性,但是看起来不错。 我尝试刷新,重建,失效,重新启动,但是仍然有问题。 项目结构看起来很奇怪,这是一个屏幕: enter image description here

enter image description here

答案: 下面的提示不起作用,我将Android Studio从3.4降级到3.1,并在进行了一些外观更改后将其编译。谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

尝试更新到最新版本:

dependencies {
    classpath 'com.android.tools.build:gradle:x.x.x'
}

还有buildToolsVersion中的build.gradle

答案 1 :(得分:0)

尝试在应用gradle文件中更改具有buildToolsVersion相同数字前缀的de compileSDkVersion和targetSdkVersion,如下所示:

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    //    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.mytway.activity.registerformactivity"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
}