Adding external Android and Java projects to Andro

2019-04-30 05:05发布

问题:

So, we have been migrating from Eclipse to the new Android Studio IDE (knowing that it's an early release version). Everything was splendid until yesterday when we updated to 0.1.1, which removed the Project Structure UI interface, replacing it with a message telling us to use gradle.

So we began reading documentation, SO posts and watched the Google I/O presentation. We still don't understand how this works.

We've read this user guide and, like I said, countless SO posts. But can't get this to work.

So we have 2 projects. Both of them are Android projects. Both use external projects. One of them uses the Facebook Android SDK and the other uses a custom, straight up java project. The java project is continually being updated.
The java project was created in Eclipse and has that folder structure.
Both of the Android Projects were created fresh in Android Studio. How do we do this?

Do the library projects need their own build.gradle files?
Do the library projects need to be placed in the same folder as our Android Projects?

And depending on the answer to those questions, how do the build.gradle and settings.gradle files need to look?

[EDIT]
For clarification:

Facts:
Android Project A needs to use the Facebook SDK external library
Android Project B needs to use an external java project I created in Eclipse
Projects A and B were both created in Android Studio

Questions:
● What needs to go into Project A build.gradle file?
● Where does the Facebook SDK need to be?
● Does the Facebook SDK need a build.gradle file? If so, what does it need to say?

● What needs to go into Project B build.gradle file?
● Where does the java project need to go?
● Does this java project need a build.gradle file? If so, what does it need to say?

● Does the Facebook SDK or the java project need a settings.gradle file?

回答1:

Each project, module or library you compile need a build.gradle file.

The most simple Android project has the following build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

android {
    compileSdkVersion 17
}

If you imported a project from eclipse, you should add that lines to match the project structure :

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

        instrumentTest.setRoot('tests')
    }
}

You can add dependencies from other project or file like that :

dependencies {
    compile file('libs/adnroid-support-v4.jar')
    compile project(':libraries:actionbarsherlock')
}

For more informations, you can have a look at the documentation here.



回答2:

go to http://tools.android.com/tech-docs/new-build-system, download zip from the bottom of the page named gradle-samples-0.4.2.zip

check libsTest example

After you setup gradle build files you need to do extra hoop for Android Studio as it doesn't pickup changes from build.gradle