diff --git a/plugins/android-jps-plugin/src/KotlinAndroidJpsPlugin.kt b/plugins/android-jps-plugin/src/KotlinAndroidJpsPlugin.kt index a803cb966b5..52e99f228ca 100644 --- a/plugins/android-jps-plugin/src/KotlinAndroidJpsPlugin.kt +++ b/plugins/android-jps-plugin/src/KotlinAndroidJpsPlugin.kt @@ -23,17 +23,21 @@ import org.jetbrains.jps.model.module.JpsModule import java.io.File import org.jetbrains.jps.android.AndroidJpsUtil import com.intellij.util.PathUtil +import org.w3c.dom.Document +import javax.xml.parsers.DocumentBuilderFactory public class KotlinAndroidJpsPlugin : KotlinJpsCompilerArgumentsProvider { override fun getExtraArguments(moduleBuildTarget: ModuleBuildTarget, context: CompileContext): List { val module = moduleBuildTarget.module val pluginId = ANDROID_COMPILER_PLUGIN_ID val resPath = getAndroidResPath(module) - val manifestFile = getAndroidManifest(module) + val applicationId = getAndroidManifest(module)?.let { getApplicationPackageFromManifest(it) } - return if (resPath != null && manifestFile != null) listOf( - getPluginOptionString(pluginId, RESOURCE_PATH_OPTION_NAME, resPath), - getPluginOptionString(pluginId, MANIFEST_FILE_OPTION_NAME, manifestFile)) + return if (resPath != null && applicationId != null) { + listOf( + getPluginOptionString(pluginId, VARIANT_OPTION_NAME, "main;$resPath"), + getPluginOptionString(pluginId, PACKAGE_OPTION_NAME, applicationId)) + } else listOf() } @@ -56,21 +60,35 @@ public class KotlinAndroidJpsPlugin : KotlinJpsCompilerArgumentsProvider { private fun getAndroidResPath(module: JpsModule): String? { val extension = AndroidJpsUtil.getExtension(module) ?: return null - val path = AndroidJpsUtil.getResourceDirForCompilationPath(extension) - return File(path!!.absolutePath + "/layout").absolutePath + return AndroidJpsUtil.getResourceDirForCompilationPath(extension)?.absolutePath } - private fun getAndroidManifest(module: JpsModule): String? { + private fun getAndroidManifest(module: JpsModule): File? { val extension = AndroidJpsUtil.getExtension(module) ?: return null - return AndroidJpsUtil.getManifestFileForCompilationPath(extension)!!.absolutePath + return AndroidJpsUtil.getManifestFileForCompilationPath(extension) } companion object { private val JAR_FILE_NAME = "android-compiler-plugin.jar" private val ANDROID_COMPILER_PLUGIN_ID = "org.jetbrains.kotlin.android" - private val RESOURCE_PATH_OPTION_NAME = "androidRes" - private val MANIFEST_FILE_OPTION_NAME = "androidManifest" + private val VARIANT_OPTION_NAME = "variant" + private val PACKAGE_OPTION_NAME = "package" + + private fun getApplicationPackageFromManifest(manifestFile: File): String? { + try { + return manifestFile.parseXml().documentElement.getAttribute("package") + } + catch (e: Exception) { + return null + } + } + + private fun File.parseXml(): Document { + val factory = DocumentBuilderFactory.newInstance() + val builder = factory.newDocumentBuilder() + return builder.parse(this) + } private fun getPluginOptionString(pluginId: String, key: String, value: String): String { return "plugin:$pluginId:$key=$value" diff --git a/plugins/android-jps-plugin/testData/android/simple/res/layout/main.xml b/plugins/android-jps-plugin/testData/android/simple/res/layout/activity_main.xml similarity index 100% rename from plugins/android-jps-plugin/testData/android/simple/res/layout/main.xml rename to plugins/android-jps-plugin/testData/android/simple/res/layout/activity_main.xml diff --git a/plugins/android-jps-plugin/testData/android/simple/src/com/example/myapp/MyActivity.kt b/plugins/android-jps-plugin/testData/android/simple/src/com/example/myapp/MyActivity.kt index aabba6205e5..d090010f127 100644 --- a/plugins/android-jps-plugin/testData/android/simple/src/com/example/myapp/MyActivity.kt +++ b/plugins/android-jps-plugin/testData/android/simple/src/com/example/myapp/MyActivity.kt @@ -7,14 +7,14 @@ import android.view.LayoutInflater import android.view.ViewGroup import android.view.View -import kotlinx.android.synthetic.main.* +import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment.* import kotlinx.android.synthetic.main.fragment.view.* public class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.main) + setContentView(R.layout.activity_main) textField.setText("test") } }