【问题标题】:Not able to run hello world in Android studio无法在 Android Studio 中运行 hello world
【发布时间】:2016-12-23 10:21:48
【问题描述】:

我安装了一个 Android 工作室并创建了一个新的 Hello World 应用程序。我尝试使用我设置的模拟器运行,但 gradle sync 给了我下面的错误。我是android开发的新手。任何帮助都会很棒。

我的 build.gradle 是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

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

allprojects {
    repositories {
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的 build.gradle 用于(模块应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.rohit_mourya.helloworld"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    androidTestCompile 'junit:junit:4.12'
    compile 'javax.inject:javax.inject:1'
    compile 'javax.annotation:jsr250-api:1.0-20050927.133100'
}

当我尝试运行我的应用程序时,它给了我以下错误:

C:\Users\rohit_mourya\AndroidStudioProjects\HelloWorld\app\build.gradle
    Error:Error:line (27)Failed to resolve: junit:junit:4.12
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Show in File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:line (28)Failed to resolve: javax.inject:javax.inject:1
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Show in File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:Failed to resolve: javax.annotation:javax.annotation-api:1.2
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:Failed to resolve: com.google.code.findbugs:jsr305:2.0.1
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:Failed to resolve: org.hamcrest:hamcrest-library:1.3
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:Failed to resolve: org.hamcrest:hamcrest-integration:1.3
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:line (29)Failed to resolve: javax.annotation:jsr250-api:1.0-20050927.133100
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Show in File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>
    Error:Error:Failed to resolve: com.squareup:javawriter:2.1.1
<a href="openFile:C:/Users/rohit_mourya/AndroidStudioProjects/HelloWorld/app/build.gradle">Open File</a><br><a href="open.dependency.in.project.structure">Show in Project Structure dialog</a>

虽然我已经在项目的模块设置中配置了这些依赖项。提前谢谢你

【问题讨论】:

  • 感谢您的回复:repositories{ maven { url 'http://repo1.maven.org/maven2' } 但我收到此错误:Gradle sync failed: Could not find jsr250-api.jar (javax.annotation:jsr250-api:1.0-20050927.133100). Searched in the following locations: http://repo1.maven.org/maven2/javax/annotation/jsr250-api/1.0-20050927.133100/jsr250-api-1.0-20050927.133100.jar Consult IDE log for more details (Help | Show Log)
  • 致电testCompile 'junit:junit:4.12'

标签: android android-studio android-studio-2.0


【解决方案1】:

在你的 build.gradle 中使用它

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        sourceSets.main {

            jniLibs.srcDir 'src/main/libs'
            //hide the ‘jni’ folder so that the automatic gradle build doesn’t try to run

            //it’s own ndk-build process

            jni.srcDirs = [];
        }
        defaultConfig {
            applicationId 'com.example.rohit_mourya.helloworld'
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }

        productFlavors {
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:25.1.0'
    }

【讨论】:

  • 谢谢哥们,这个错误已经消失了,但是现在当我运行我的应用程序时,它给了我以下错误:Error:Execution failed for task ':app:ndkBuild'. &gt; A problem occurred starting process 'command 'C:\Users\rohit_mourya\AppData\Local\Android\android-ndk\ndk-build.cmd'' 我猜你给我的路径commandLine "C:\\Users\\rohit_mourya\\AppData\\Local\\Android\\android-ndk\\ndk-build.cmd", '-C', file('src/main').absolutePath 是错误的
  • 谢谢哥们。它终于奏效了。你能告诉我我的文件有什么问题以及你给我的解决方案吗?如果我正在创建新应用程序,那么我是否应该再次复制该文件内容(你给我的那个)?
  • @RohitMourya 是的,您可以将此文件内容复制到其他简单的 android 应用程序。只需更改 applicationId 'your package name' 的值。如果这个答案有用,别忘了点赞。
  • 好的。谢谢。而且我的声誉低于 15,所以无法投票回答
  • @RohitMourya 好的 np。但我认为你可以接受这个答案。
猜你喜欢
  • 1970-01-01
  • 2016-03-16
  • 2020-04-30
  • 2014-09-25
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多