简体   繁体   中英

Create new Gradle based Android project in IntelliJ IDEA

The title pretty much says it all. I would like to create a new Gradle based Android project in IntelliJ IDEA (13 EAP) like I can do in Android Studio.

I've tried creating a new Android Project in IntelliJ but it uses the "old" build system, while creating a new Gradle Project creates a generic java project which doesn't have Android integration at all.

What can I do?

  1. Create an android project
  2. Create an empty file `build.gradle' in the root of your project
  3. Add to the file:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

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

android {
    compileSdkVersion 18
    buildToolsVersion "19"

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

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}
  1. Menu Run > Edit configurations and add( + ) Groovy run/debug configuration
  2. Fill script path to the previously created file 'build.gradle'
  3. Fill Script parameters with desirable task( eg installDebug or assemble )

To add adb shell command you may add new task to this script. Sample:

task launchDefaultActivity(type:Exec){
    commandLine './adb', 'shell', 'am', 'start', '-c', 'android.intent.category.LAUNCHER', '-n', 'com.example.AndroidGradle/.LaunchActivity'
}

Just an update: for IntelliJ IDEA 13.1.0, you can follow this link

As suggested, I just quote the details here

To create a Gradle-based Android project, do one of the following:

  1. If you are going to create a new project: click Create New Project on the Welcome screen or select File | New Project. As a result, the New Project wizard opens. If you are going to add a module to an existing project: open the project you want to add a module to, and select File | New Module. As a result, the New Module wizard opens.

  2. On the first page of the wizard, in the left-hand pane, select Android. In the right-hand part of the page, select Gradle: Android Module. Click Next.

  3. Specify the JDK and Android SDK to be used, and click Next.
  4. Specify your Android module settings, and click Next.
  5. Specify the settings for your Android-Gradle Foreground configuration, and click Next.
  6. Select an Android application template from the list, and click Next.
  7. Specify the settings for the selected Android Activity template, and click Next.
  8. Specify the name and location settings. For more information, see Project Name and Location or Module Name and Location.
  9. Click Finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM