12 Jul 2020

Create Android Apps without Android Studio -V

Android Apps without Android Studio - 5

We have edited the build files in the previous post. Now we will write the code for our app.

create a folder named ‘main’ in the ‘src’ folder.

This folder will contain 3 things :

  1. AndroidManifest.xml file we created earlier.
  2. java folder
  3. res folder

Move the ‘AndroidManifest.xml’ file to the ‘main’ folder. create two folders named ‘java’ and ‘res’ inside the ‘main’ folder.

In the ‘java’ folder, create ‘com’ folder. In the ‘com’ folder, create ‘example’ folder. In the ‘example’ folder, create ‘fromis’ folder. In the ‘fromis’ folder, create the file named ‘MainActivity.kt’.

If you are finding this confusing, just make your source tree layout as shown below.

Our final source tree

The final source tree will look like this :

fromis
|---| .gradle
|---| app\
|------| src\
|---------| main\
|------------| java\
|---------------| com\
|------------------| example\
|---------------------| fromis\
|------------------------| MainActivity.kt
|------------| res\
|---------------| layout\
|------------------| activity_main.xml
|------------| AndroidManifest.xml
|------| .gitignore
|------| build.gradle
|---| gradle\
|------| wrapper\
|---------| gradle-wrapper.jar
|---------| gradle-wrapper.properties
|---| .gitignore
|---| build.gradle
|---| gradle.properties
|---| gradlew
|---| gradlew.bat
|___| settings.gradle

The ‘MainActivity.kt’ file is where we will write our main code.

The ‘res’ folder will contain the Layout details of the widgets and different activities.

The ‘AndroidManifest.xml’ file contains basic details and permissions of our app.

Checking the setup with first build

In the activity_main.xml file, add this code.

app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

And Finally we are in the last file. MainActivity.kt .

Add the below code to this file and get ready for the build process.

app/src/main/java/com/example/fromis/MainActivity.kt
package com.example.fromis

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Building and Assembling using gradle

Now take a deep breathe … we are so close to the build process.

Open the command line / terminal in the root directory of our app i.e. fromis.

enter the command gradle build or gradle assembleDebug.

gradle build

or

gradle assembleDebug

The gradle build command will generate an apk file for our basic app. The gradle assembleDebug command will create the apk for debugging.

this will show you something like the below example:

F:\Codefolds\fromis>gradle build
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 6m 14s
51 actionable tasks: 50 executed, 1 up-to-date

OR


F:\Codefolds\fromis>gradle assembleDebug

BUILD SUCCESSFUL in 1m 43s
21 actionable tasks: 21 executed

Now go and check the output in build folder inside app.

fromis\app\build\outputs\apk\debug\app-debug.apk

Do run the command gradle clean every time after building your apk. So as to keep your repository clean.

For more gradle tasks, you can run the command gradle tasks . It will show you all the actions you can perform via gradle.

Finally, we have successfully set up our build environment and app sources. Now it is up to you how you want to build your app.

You can also create a new app project in Android Studio and then do the build process via command line and edit the files using VS Code.

Happy Coding …!


Share this

Tag :