7

I'm trying to migrate my existing game (written in libgdx) from Eclipse to Android Studio. After migrating my desktop project work properly, but I have problem with Android project. In android I'm using Admob ads.

I get the following error message:

enter image description here

As you can see Android Support Repository is installed SDK Manager

My project structure:

enter image description here

In Project Gradle file I have this :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'org.robovm:robovm-gradle-plugin:1.9.0'
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = 'My Game'
        gdxVersion = '1.7.1'
        roboVMVersion = '1.9.0'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.6.0'
    }

    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
    }
}

project(":desktop") {
    apply plugin: "java"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile project(":core")

        compile 'com.android.support:appcompat-v7:23.1.1'
        compile 'com.google.android.gms:play-services:8.4.0'

        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        compile fileTree(dir: '../libs', include: '*.jar')
    }
}

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "org.robovm:robovm-rt:$roboVMVersion"
        compile "org.robovm:robovm-cocoatouch:$roboVMVersion"
        compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
    }
}

project(":html") {
    apply plugin: "gwt"
    apply plugin: "war"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
    }
}

project(":core") {
    apply plugin: "java"


    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile fileTree(dir: '../libs', include: '*.jar')
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

Here is Android Gradle file:

apply plugin: 'com.android.application'

android {

    buildToolsVersion "23.0.2"
    compileSdkVersion 23

    defaultConfig {
        targetSdkVersion 23
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }

}

// needed to add JNI shared libraries to APK when compiling on CLI
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
    pkgTask.jniFolders = new HashSet<File>()
    pkgTask.jniFolders.add(new File(projectDir, 'libs'))
}

// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}

task run(type: Exec) {
    def adb = "$System.env.ANDROID_HOME/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.flexsolutions.bubbles.era.android/com.flexsolutions.bubbles.era.android.AndroidLauncher'
}

// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin
    // ignores any nodes added in classpath.file.withXml
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }

    jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.6
    }

    classpath {
        plusConfigurations += project.configurations.compile
        containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
    }

    project {
        name = appName + "-android"
        natures 'com.android.ide.eclipse.adt.AndroidNature'
        buildCommands.clear();
        buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
        buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
        buildCommand "org.eclipse.jdt.core.javabuilder"
        buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
    }
}

// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
        scopes = [COMPILE: [plus: [project.configurations.compile]]]

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
                        }
                    }
                }
            }
        }


    }
}

I have tried to change android support to other version , but I get same error.

Any help would be appreciated.

Thanks

piotrek1543
  • 18,034
  • 7
  • 69
  • 86
Jovan
  • 4,340
  • 12
  • 55
  • 94
  • I'm not 100% sure, but I believe to use the google repository you installed you need to add "mavenLocal()" to the "repositories" in "allprojects". – p.streef Jan 07 '16 at 08:14

3 Answers3

3

I think that your problem with importing the project is caused by this

buildToolsVersion "19.1.0"
compileSdkVersion 19

You're trying to use an older buildTools than your Android Support libraries:

    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.android.support:appcompat-v7:21.0.0'

All you need to do is change the values above to 21.

Just follow these steps:

Prerequirements

Make sure that you've downloaded the latest extras as well as the Android 5.0 SDK via the SDK-Manager.

Picture of the SDK Manager


Android Studio

Open the build.gradle file of your app-module and change your compileSdkVersion to 21. It's basically not necessary to change the targetSdkVersion SDK-Version to 21 but it's recommended since you should always target the latest android Build-Version. In the end you gradle-file will look like this:

android {
    compileSdkVersion 21
    // ...

    defaultConfig {
        // ...
        targetSdkVersion 21
    }
}

Be sure to sync your project afterwards.

Android Studio Gradle Sync reminder

Change also your buildToolsVersion:

buildToolsVersion "19.1.0"

to

buildToolsVersion "21.1.2"

It should work


If you would like to make your project more recent than above, here's an build.gradle of my Android project created a while ago:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.piotr.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'

    compile 'com.google.android.gms:play-services:8.4.0'
}

EDIT: Something still missing. Try these steps

  1. Update your dependencies to the latest version as above.

  2. Add this line

    `apply plugin: 'com.android.application' before

before

`android {
         buildToolsVersion "19.1.0"` 

and change

repositories {
    mavenCentral()
    ...

with

 repositories {
          jcenter()
          mavenCentral()
          ...

Hope it help

Community
  • 1
  • 1
piotrek1543
  • 18,034
  • 7
  • 69
  • 86
  • thanks for your answer, I try to change gradle file as you described but I get the same error. Maybe android components are on another repro or I need to add some other repro. I haven't idea. I lose 2 weeks on this – Jovan Jan 06 '16 at 12:43
  • so you changed `compile 'com.android.support:`, `compileSdkVersion`, `buildToolsVersion` in every file and place where they are, right? The same error - you mean: `Could not find compile 'com.android.support:support-v4:21.0.0` - I mean this exactly or a similar one? – piotrek1543 Jan 06 '16 at 12:47
  • Yes, I changed as you comment and answer. And I get this error `Error:Could not find com.android.support:support-v4:21.0.0. Searched in the following locations: https://repo1.maven..` – Jovan Jan 06 '16 at 12:57
  • ok so open your Standalone SDK Manager (in Android Studio `Tools -> SDK Manager -> Standalone SDK...`) and install the latest version of buildtools, check at the bottom if support library is installed and install API 20, API 21, API 22. After that add an screenshot of this manager to see what you have installed ;-) – piotrek1543 Jan 06 '16 at 13:03
  • See edit for screen shot. Probably I will have to reinstall Android Studio and SDK – Jovan Jan 06 '16 at 13:10
  • Well, notice that you have `23.1.1` version of Support, you're trying to set 21.0.0, which is not available ;-) So I said forget about 21 and change your `build.gradle` with dependencies versions as in my example app, I mean `23` and `8.4.0` for google play service – piotrek1543 Jan 06 '16 at 13:19
  • I have uninstall Android Studio and SDK, reinstall back. And same error. I change build.gradle as you suggest and again same error `Could not find com.android.support:appcompat-v7:23.1.1. Searched in the following locations:` . I went crazy :) – Jovan Jan 06 '16 at 15:02
  • I set `buildToolsVersion "23.0.2"`, and `compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.google.android.gms:play-services:8.4.0'`. Everything is as you suggest in comment above :) – Jovan Jan 06 '16 at 15:11
  • Same error :( I edited my question with all my settings. Sorry for your time – Jovan Jan 06 '16 at 16:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99962/discussion-between-piotrek1543-and-jovan). – piotrek1543 Jan 06 '16 at 16:36
2

Finally I find solution

I created blank new libGDX project using libGDX project setup with same package as my previous setup, and I copy all of my classes from old project to this new one. After all done I get working version of my code.

I didn't notice any differences in buld.gradle filed as comments and @piotrek1543 answers.

I don't know what was problem exactly, but my project now work.

I hope this will help anyone in future!

Jovan
  • 4,340
  • 12
  • 55
  • 94
0

Very Sad, This error comes up every time I include nexus repository manager in the gradle setup

repositories{
maven{  url "http://localhost:8081/nexus/content/groups/public/" }
}

I was forced to remove delete the local nexus repository, and it compiles. I feel more and more, google is insensitive to the needs of developers from developing countries where internet is really expensive and slow. i was forced to leave just

repositories{
jcenter()
}
Dr Deo
  • 4,347
  • 11
  • 38
  • 65