Android - Kotlin 文件在编译后存在于 apk 中
Android - Kotlin files are present in apk after compilation
我有一个 android 项目,其中包含 java 和 kotlin 文件。
编译后,当我解压生成的apk文件时,我可以在他们的包路径中看到我项目的所有Kotlin文件。
但是 Java 文件不存在。
我该如何解决这个问题,并阻止将 Koltin 文件添加到 apk 中?
谢谢
我的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)
}
}
}
终于找到问题所在:
我不得不从源集
中删除resources.srcDirs = ['src']
这是在复制 src 文件夹下的所有非java文件
我有一个 android 项目,其中包含 java 和 kotlin 文件。
编译后,当我解压生成的apk文件时,我可以在他们的包路径中看到我项目的所有Kotlin文件。 但是 Java 文件不存在。
我该如何解决这个问题,并阻止将 Koltin 文件添加到 apk 中?
谢谢
我的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) } } }
终于找到问题所在:
我不得不从源集
中删除resources.srcDirs = ['src']
这是在复制 src 文件夹下的所有非java文件