user3475234
user3475234

Reputation: 1573

AndroidStudio - change default src folder name

In AndroidStudio, which I recently started using, the primary folder for source code displayed in the Project view is called java, and the files are stored in app/src/main/java/com/...

I would like the folder to be called src instead of java (for the purpose of interfacing with a separate tool). Is there any way I can do this and still have the project compile normally in Android Studio? I tried renaming the folder from java to src, and it disappears from the project view - I'm assuming I need to tell Gradle where to find the source files, but I don't know how. Any help would be appreciated.

Upvotes: 4

Views: 2505

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

Is there any way I can do this and still have the project compile normally in Android Studio?

Add a sourceSets closure to app/build.gradle file, telling Gradle the new location of your Java code. Rename your directory to match, and then sync the project files with Gradle.

For example, here is an app/build.gradle file from a newly-created Android Studio project, where I added the sourceSets closure:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.commonsware.myapplication"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/src']
        }
    }
}

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

Here, I am saying that the main sourceset's Java code resides at src/main/src relative to the module root.

After adding that, I renamed java/ to src/ as you described, then used the yellow banner in the build.gradle editor to sync the files. Android Studio seems happy, and the project compiles normally.

Upvotes: 5

Related Questions