ANT Android project to Android Studio

2019-06-05 22:17发布

问题:

I have a fully working Android application based on ANT build tools. It is used with Eclipse but I have to make it work with Android Studio as well. I managed to export the project with ADT Gradle Export Tool and the application itself works great. However, I can't get the test project to work.

Here's build.gradle generated by Eclipse ADT plugin.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

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

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

My project structure

.
├── AndroidManifest.xml
├── ant.properties
├── build.gradle
├── build.xml
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── local.properties
├── MyApp.iml
├── proguard-project.txt
├── project.properties
├── README.md
├── res
│   ├── drawable-hdpi
│   │   └── ic_launcher.png
│   ├── drawable-ldpi
│   │   └── ic_launcher.png
│   ├── drawable-mdpi
│   │   └── ic_launcher.png
│   ├── drawable-xhdpi
│   │   └── ic_launcher.png
│   ├── layout
│   │   └── main.xml
│   └── values
│       └── strings.xml
├── src
│   └── com
│       └── example
│           └── myapp
│               └── MainActivity.java
└── tests
    ├── AndroidManifest.xml
    ├── ant.properties
    ├── build.xml
    ├── proguard-project.txt
    ├── project.properties
    └── src
        └── com
            └── example
                └── myapp
                    └── MainActivityTest.java

For me it seems that Android Studio is not able to detect tests/src/PACKAGE as a package. It is classified as casual directories instead.

I would really appreciate any hints.

EDIT: If tests/src is renamed into tests/java everything works. But this is not a solution since tests will stop working with ANT and/or Eclipse.

回答1:

Inside build.gradle file of main module.

android {
    sourceSets {
        tests {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
}

Can you try this and let me know If doesn't work, i will delete it.



回答2:

With some help from a colleague I managed to resolve the issue. The proper build.gradle file for a project exported as mention in the question should look like

// (...)

android {
    // (...)

    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 {
                java.srcDirs = ['tests/src']
                res.srcDirs = ['tests/res']
                assets.srcDirs = ['tests/assets']
            }
        }

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Additionally, changing default test package name may be required since Gradle by default expects that package to be like application-package.test whereas Ant used application-package.tests convention. This may be however, some issue specific only to my project nevertheless I thought sharing it can be beneficial to someone in the future.