5
votes

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)
        }
    }
}
1
You can use proguard for obfuscate your code. Just turn on it int build.gradle file: android {buildTypes {release { minifyEnabled true ...} } }.p.alexey
Please explain in detail what "I can see all my Kotlin files" means. For example, you might want to show the output of the APK Analyzer or unzip or something that demonstrates what you are seeing.CommonsWare
@p.alexey Proguard seems to be turned on correctly, and I can see some warnings concerning Proguard while building the apkLeguman
@CommonsWare I edited my question and added a screenshot of the unzipped apk outputLeguman
There's something fairly strange going on here. That's not normal. Did you make significant changes to your build.gradle file for this module?CommonsWare

1 Answers

6
votes

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