简体   繁体   English

将现有的 Flutter Kotlin 项目转换为 Flutter ZD52387880E1EA22817A72D375921381 项目

[英]Convert an existing Flutter Kotlin project to Flutter Java project

I created a Flutter project using default values, which are Kotlin for Android and Swift for iOS. I created a Flutter project using default values, which are Kotlin for Android and Swift for iOS. Halfway through the project I needed to integrate a 3rd party Android SDK that requires Java.在项目进行到一半时,我需要集成需要 Java 的第 3 方 Android SDK。 Can I convert a Flutter project to Java for Android after creation?我可以在创建后将 Flutter 项目转换为 Java 用于 Android 吗?

I know I will need yo use Platform Channels to integrate native code with my Flutter app, this is not my concern.我知道我需要你使用平台通道将本机代码与我的 Flutter 应用程序集成,这不是我关心的问题。

I had the same problem, for me this solution works.我有同样的问题,对我来说这个解决方案有效。

  1. Move folder com.example.test_app (any name you have) from android/app/src/main/kotlin -> android/app/src/main/java从 android/app/src/main/kotlin -> android/app/src/main/java 移动文件夹 com.example.test_app(你有的任何名称)
  2. Replace MainActivity.kt with Java version, or copy down here将 MainActivity.kt 替换为 Java 版本,或者在这里复制下来

    package com.example.test_app; import androidx.annotation.NonNull; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.plugins.GeneratedPluginRegistrant; public class MainActivity extends FlutterActivity { @Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine); } }
  3. Remove following code android/app/build.grandle删除以下代码 android/app/build.grandle

     ... apply plugin: 'kotlin-android'... sourceSets { main.java.srcDirs += 'src/main/kotlin' }
  4. In the same place replace following:在同一个地方替换以下内容:

     dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' }

    to

    dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }

By default flutter template supports writing Android code using Kotlin, or iOS code using Swift. By default flutter template supports writing Android code using Kotlin, or iOS code using Swift. To use Java or Objective-C, use the -i and/or -a flags:要使用 Java 或 Objective-C,请使用 -i 和/或 -a 标志:

In a terminal run: flutter create -i objc -a java your_project_name .在终端运行中: flutter create -i objc -a java your_project_name

If you wanna change your existing app platform language choice , as a workaround you can delete the android/ directory and run flutter create -a java to get the directory re-created for the new language choice (same for ios/ and Swift).如果您想更改现有的应用程序平台语言选择,作为一种解决方法,您可以删除 android/ 目录并运行flutter create -a java以获取为新语言选择重新创建的目录(对于 ios/ 和 Swift 相同)。 You need to re-apply custom changes though.您需要重新应用自定义更改。

run flutter create -a java.运行flutter create -a java. inside your project directory在您的项目目录中

flutter create: If run on a project that already exists, this will repair the project, recreating any files that are missing. flutter 创建:如果在已经存在的项目上运行,这将修复项目,重新创建丢失的所有文件。

delete the kotlin directory in android/src/main if it only consists of the generated example code如果它只包含生成的示例代码,请删除android/src/main中的kotlin目录

If you are creating a new project from cmd:如果您从 cmd 创建一个新项目:

flutter create -i objc -a java project_name

Note: -a means android flag and -i means ios flag.注意:-a 表示 android 标志,-i 表示 ios 标志。 If you want just for java, you can remove -i objc如果你只想要 java,你可以去掉-i objc

If you want to convert an existing project:如果要转换现有项目:

flutter create -a java.

There is a dot at the end of the above line, when converting an existing project转换现有项目时,上述行末尾有一个点

Just wanted to follow up with this, as I had to change the default language in my Flutter project from Kotlin to Java today, and ran into a little pain point, where I found that running flutter create -i swift -a java. Just wanted to follow up with this, as I had to change the default language in my Flutter project from Kotlin to Java today, and ran into a little pain point, where I found that running flutter create -i swift -a java. in my project directory worked, but was causing my project name to change based on whatever directory it was in, and if that directory name was the last value of my "package name" (ie: com.flutter.app and the directory was "app"), this was then throwing a D8: Program type already present: error when compiling.在我的项目目录中工作,但导致我的项目名称根据它所在的目录而更改,如果该目录名称是我的“包名称”的最后一个值(即:com.flutter.app 并且目录是“ app"),然后抛出D8: Program type already present:编译时出错。

This is due to the MainActivity of the original Kotlin src.这是由于原始 Kotlin src 的 MainActivity。 What I discovered is that I had to (after checking out my source code into this "app" folder that I wanted to use in the package name) , first delete this existing android/app/src/main/kotlin folder AND THEN run the flutter create -i swift -a java.我发现我必须(在将我的源代码签入我想在 package 名称中使用的这个“app”文件夹之后) ,首先删除这个现有的android/app/src/main/kotlin文件夹,然后运行flutter create -i swift -a java. command.命令。

Note that I didn't have any unique code added to the Kotlin folder, so removal of this folder wasn't a concern.请注意,我没有向 Kotlin 文件夹添加任何唯一代码,因此无需担心删除此文件夹。

Hope this helps anyone else experiencing a similar issue.希望这可以帮助其他遇到类似问题的人。

Just in case after trying to do this then this , you found out that you cannot generate java class, suggestion and navigate declaration doesn't appear, etc.,以防万一在尝试执行操作,您发现无法生成 java class,建议和导航声明不出现等,

right click java folder, Mark Directory as > Source root.右键单击 java 文件夹,将目录标记为 > 源根目录。 Then, edit your java class by open new android studio window by choose android directory Then, edit your java class by open new android studio window by choose android directory

reference: Why Android Studio doesn't allow me to create Java Classes?参考: 为什么 Android Studio 不允许我创建 Java 类?

I am on Kotlin /Flutter too.我也在 Kotlin /Flutter 上。

With the latest Firebase Flutter rewrite, most of the kt files are no longer needed.使用最新的 Firebase Flutter 重写,不再需要大多数 kt 文件。 Switch to v2 embedding, and it wont matter you are using Kotlin...切换到 v2 嵌入,使用 Kotlin 并不重要...

https://firebase.flutter.dev/docs/migration/ https://firebase.flutter.dev/docs/migration/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM