Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
feugatos Avatar asked Oct 12 '13 11:10

feugatos


People also ask

How do I project a Gradle project in IntelliJ?

Convert a regular project into a Gradle project Open your project in IntelliJ IDEA. In the Project tool window, right-click the name of your project and select New | File. In the dialog that opens enter build. gradle and click OK.

How do I create a new kotlin Gradle project in IntelliJ?

Create a new Kotlin/Native project in IntelliJ IDEA In IntelliJ IDEA, select File | New | Project. In the panel on the left, select Kotlin Multiplatform. Enter a project name, select Native Application as the project template, and click Next. By default, your project will use Gradle with Kotlin DSL as the build system.

How do I build and run a Gradle project in IntelliJ?

From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. icon. In the list that opens, select Run Gradle task. In the Select Gradle Task dialog, specify the project and the task that you want to execute before launching the project.


1 Answers

  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( e.g. 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'
}
like image 78
eleven Avatar answered Oct 08 '22 18:10

eleven