Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Kotlin files are present in apk after compilation

I have an android project with both java and kotlin files.

After compilation, when I unzip the generated apk file, I can see all the Kotlin files of my project in their package path. The Java files however are not present.

How can I fix this, and stop the Koltin files from beeing added to the apk ?

Thanks

Screenshot of unzipped apk content

My build.gradle:

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'

repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
    jcenter()
    flatDir{
        dirs 'libs'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: '*.jar')
    /* A bunch of dependencies */
}

android {

    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {

        minSdkVersion 19
        targetSdkVersion 27

        multiDexEnabled true

        setOutputPath applicationVariants, goevent["outputDir"], goevent["outputName"]
        setOutputPath testVariants, goevent["outputDir"], goevent["outputNameTest"]
    }

    dataBinding {
        enabled = true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'main/AndroidManifest.xml'
        pickFirst 'META-INF/maven/com.squareup.okio/okio/pom.properties'
        pickFirst 'META-INF/maven/com.squareup.okio/okio/pom.xml'
    }

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

        androidTest.setRoot('tests')
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

    signingConfigs {
        release {

            //Fetch the signing file
            File signFile = rootProject.file('signing.properties')

            //Read the signing properties file
            Properties properties = new Properties()
            properties.load(new FileInputStream(signFile))


            if (properties['signingKeystorePath'] && properties['signingKeystorePassword'] && properties['signingKeyAlias'] && properties['signingKeyPassword']) {
                String toolsPath = System.getenv("TOOLS")
                if(toolsPath != null) {
                    storeFile file(toolsPath + properties['signingKeystorePath'])
                    storePassword properties['signingKeystorePassword']
                    keyAlias properties['signingKeyAlias']
                    keyPassword properties['signingKeyPassword']
                }

            } else {

                //If the signing file doesn't exist or the properties are not set
                if (project.hasProperty("signingKeystorePath")) {
                    storeFile file("$signingKeystorePath")
                    storePassword signingKeystorePassword
                    keyAlias signingKeyAlias
                    keyPassword signingKeyPassword
                }

            }
        }
    }
    lintOptions {
        abortOnError false
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.pro'
                    signingConfig signingConfigs.release
            zipAlignEnabled true
        }
    }

    dexOptions {
        jumboMode = true
    }
}

def setOutputPath(variants, dir, name) {
    variants.all { variant ->
        variant.outputs.all { output ->
            def relativeRootDir = output.packageApplication.outputDirectory.toPath()
                    .relativize(rootDir.toPath()).toFile()

            output.outputFileName = new File("$relativeRootDir/$dir", name)
        }
    }
}
like image 821
Leguman Avatar asked Nov 14 '18 21:11

Leguman


1 Answers

Finally found the issue:

I had to removed resources.srcDirs = ['src'] from the sources set

This was copying all non-java files contains under the src folder

like image 198
Leguman Avatar answered Sep 27 '22 17:09

Leguman