Android Studio 3.0 versions below configuration:
This uses the Android Studio 2.3 version, so you need to add the Kotlin plugin.
Add a Kotlin language dependency package in Android Studio:
The Kotlin standard library relies on the:file–> setting–>plugins–> search kotlin–> installation, as follows:
After installation, restart Android Studiio.
the original Androidstudio Java Grammar project is translated into a project that supports Kotlin syntax:
The following steps, Androistudio automatic download to configure, in order to avoid manual configuration error.
1. Convert Java files to Kotlin file::
Open the Java file, and then click Code–>convert Java files to Kotlin
2. Add the Kotlin configuration: configue–>android Gralde:
3. Select the added configuration, here to add a plugin to the project that currently contains Kotlin :
4. When configured, adding sync Now,androidstudio in Project Gradle will automatically download the configuration
5. The Gralde of the configured Projcet are as follows :
6. The Gradle of the configured module is as follows :
read the code in mainactivity, after Java turns into Kotlin :
/** * 一个类继承父类和实现接口的方式; class 类名 :超类名(),接口名 */class MainActivity : AppCompatActivity() { /** * override用于覆写继承父类或者实现接口中方法。 * * fun 用于标识方法 * * 参数形式: 参数名: 类型 * * ? 是用于指定可以为空对象 * */ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) }}
The results are as follows :
Android Studio 3.0 Create Kotlin Project:
Android Studio 3.0 and its version are supported by Kotlin development.
1. Download and install Android Studio 3.0:
Android Studio 3.0 Download: Go to download page
The pre-version of Android Studio can coexist with the Androidstudio version of your computer's existing installation, see multiple Androidstudio Coexistence installation tutorials for details.
2. Create a Kotlin project using Androidstudio:
In Androidstudio, on the welcom toandroid Studio page, click Start new Android Studio Project or click file– in the original workspace. >new–>new Projcet . Next, in creat New Projcet, tick the include Kotlin support, and then click Next to go all the way down.
The last Kotlin file is similar to the Java file storage path and is stored in the src/main/java directory.
3. Add Kotlin to the project:
In file–>new–>, select the file type in the menu as follows:
After the wizard has been shown, select Kotlin as the development language on the new Android activity page and then go all the way next.
Android Studio is very powerful and also provides other ways to create Kotlin files .
Click Flie–>new–>kotlin file/class to create a basic file. In the new Kotlin file/class pop-up window, enter a file name to select a type from the multi-medium kind. There is no need to worry about creating the wrong file type, and when declaring the type in code, the file automatically switches to the type that the declaration corresponds to.
modify the path stored by the Kotlin:
By default, the Kotlin file is stored in the src/main/java/ same folder as Java. If you need to separate the Java files from the Kotlin file, specify a folder dedicated to the Kotlin file. For example, src/main create a Kotlin directory under the path, and then you need to specify the Sourcesets configuration in Gralde:
android { sourceSets { ‘src/main/kotlin‘ }}
3. Convert Java code to Kotlin code
In the project, open a Java file and click on the top window in the code–>convert Java files to Kotlin file
One way is to copy the Java code in the Kotlin file, and then prompt, convert the code tokotlin, tick the option to not prompt for the popup next time, so that the Java code will be converted to Kotlin.
4. Using the Android API in the Kotlin language
Kotlin is interoperable with the Java language and is used interactively. As a result, Kotlin calls the Android API in a similar way as Java calls.
Here are a few examples to make a comparison (source official):
Write activity:
publicclass MyActivity extends AppCompatActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); }}
class MyActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity) }}
to set a click event for a view :
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ... }});
val fab = findViewById(R.id.fab) as FloatingActionButtonfab.setOnClickListener { ...}
Create Item Click event :
PrivateBottomnavigationview.onnavigationitemselectedlistener Monnavigationitemselectedlistener =NewBottomnavigationview.onnavigationitemselectedlistener () {@Override PublicBooleanonnavigationitemselected(@NonNull MenuItem Item) {Switch(Item.getitemid ()) { CaseR.id.navigation_home:mtextmessage.settext (R.string. title_home);return true; CaseR.id.navigation_dashboard:mtextmessage.settext (R.string. Title_dashboard);return true; }return false; }};
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.navigation_home -> { mTextMessage.setText(R.string.title_home) [email protected] true } R.id.navigation_dashboard -> { mTextMessage.setText(R.string.title_dashboard) [email protected] true } } false}
Kotlin programming Androidstudio (including versions 3.0 and 2.x) configuration and use