Search code examples
androidandroid-studioandroid-studio-2.0

Not able to run hello world in Android studio


I installed a Android studio and created a new Hello World application. I tried to run using the emulator which I've set up but gradle sync gives me error which is below. I'm new to android development. Any help would be great.

My build.gradle is:

// 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
}

My build.gradle for (Module app)

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'
}

And when I try to run my application it gives me following error:

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>

Though I've configured these dependencies in Module settings of my project. Thank you in advance


Solution

  • Use this in your 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'
        }