- How to make Android apps without IDE from command line
- How to do Android development faster without Gradle
- IntelliJ IDE, but not Gradle
- 1. Install Java
- 2. Install all SDK tools
- Download Android Studio and SDK Tools | Android Studio
- Download the official Android IDE and developer tools to build apps for Android phones, tablets, wearables, TVs, and…
- 3. Code the application
- How to use JavaMail on Android (without Gradle)
- Hello guys!
- 4. Build the code
- 5. Sign the package
- 6. Align the package
- 7. Test the application
- 8. Make a script
- Notes
- Prerequisites
- The «android» command
- List Targets
- List Devices
- The Android Emulator
- Creating an Android Virtual Device
- Running your virtual device
- Creating a new Android application
- Project Directory Contents
- Adjust the created project
- Compiling an Android Project
- The APK file
- The Android Debug Bridge
- Select the appropriate device
- Installing an Android app
- Replacing or reinstalling an app
- Debugging with adb logcat
- Filtering logs
- Interrupting logcat
- Congratulations!
How to make Android apps without IDE from command line
Nov 26, 2017 · 5 min read
A HelloWorld without Android Studio
Update: I’ve made a new course that explain how you can avoid Android Studio and Gradle, but still use IntelliJ iDE:
How to do Android development faster without Gradle
IntelliJ IDE, but not Gradle
In this tutorial, I will show you how you can build/compile an APK (an A n droid app) from your java code using terminal (on Linux) without IDE or in other words without Android Studio. At the end, I will also show you a script to automate the process. In this example, I will use Android API 19 (4.4 Kitkat) to make a simple HelloWorld. I want to say that I will do this tutorial without android command which is deprecated.
1. Install Java
First, you need to install java, in my case, I install the headless version because I don’t use graphics (only command line):
2. Install all SDK tools
Then download the last SDK tools of Android which you can find here:
Download Android Studio and SDK Tools | Android Studio
Download the official Android IDE and developer tools to build apps for Android phones, tablets, wearables, TVs, and…
I recommend to unzip it in the /opt directory inside another directory that we will call “android-sdk”:
Now, we have to install platform tools (which contain ADB), an Android API and build tools.
In fact, if you are on Debian, you can avoid installing platform-tools package and only install ADB like that:
3. Code the application
In this example, I want to compile a simple HelloWorld. So, first, we need to make a project directory:
Then we have to make the files tree:
If you use exernal libraries (.jar files), also make a folder for them:
You have an example here:
How to use JavaMail on Android (without Gradle)
Hello guys!
Make the file src/com/example/helloandroid/MainActivity.java and put that inside:
Make the strings.xml file in the res/values folder. It contains all the text that your application uses:
The activity_main.xml is a layout file which have to be in res/layout:
You also have to add the file AndroidManifest.xml at the root:
4. Build the code
Now, I recommend to store the project path in a variable:
First, we need generate the R.java file which is necessary for our code:
- -m instructs aapt to create directories under the location specified by -J
- -J specifies where the output goes. Saying -J src will create a file like src/com/example/helloandroid/R.java
- -S specifies where is the res directory with the drawables, layouts, etc.
- -I tells aapt where the android.jar is. You can find yours in a location like android-sdk/platforms/android-/android.jar
Now, we have to compile the .java files:
If you have use an external, add it the classpath:
The compiled .class files are in obj folder, but Android can’t read them. We have to translate them in a file called “classes.dex” which will be read by the dalvik Android runtime:
But if you use external libraries, do rather:
If you have the error UNEXPECTED TOP-LEVEL EXCEPTION , it can be because you use old build tools and DX try to translate java 1.7 rather than 1.8. To solve the problem, you have to specify 1.7 java version in the previous javac command:
The -source option specify the java version of your source files. Note that we can use previous versions of Java even we use OpenJDK 8 (or 1.8).
We can now put everything in an APK:
Be aware: until now, we used three AAPT commands, the first and the second one are similar but they don’t do the same. You have to copy the classes.dex file at the root of project like above! Otherwise, AAPT won’t put this file at right place in the APK archive (because an APK is like a .zip file).
The generated package can’t be installed by Android because it’s unaligned and unsigned.
If you want, you can check the content of the package like this:
5. Sign the package
To do so, we firstly create a new keystore with the command keytool given by Java:
Just answer the questions and put a password.
You can sign an APK like this:
Note that apksigner only exist since Build Tools 24.0.3.
6. Align the package
It’s as simple as that:
Alignment increase the performance of the application and may reduce memory use.
7. Test the application
To test the application, connect your smartphone with a USB cable and use ADB:
But before run this command, I recommend to run this one:
If there is an error during installation or running, you see it with that command.
Voila! Here’s the result:
8. Make a script
If you don’t want to run all these steps every time you would like to compile your app, make a script! Here’s mine:
Notes
- You can remove “test” if you just want to compile without testing.
- This script only compile and run the app on the phone. But I can also make a script to automatically generate a new project like this one. I think I have a good idea to do so, but I need to know if you are interested. If it’s the case, please leave a comment or send me an e-mail.
- I can also complete the script for external libraries. Likewise, let me know if you want this.
If you have any questions, don’t hesitate to ask them below or by e-mail ;-)! EDIT: Well I’m very busy actually…
Источник
Prerequisites
To properly follow this material, you must have the following in place:
(1) You have installed the latest Java Development Kit (JDK) from Oracle. (java.com/download)
(2) You must have installed Android SDK. If you have not done so yet, you can download the Android SDK here: developer.android.com/sdk
(The Android SDK can be downloaded separately as a stand alone installer, or as part of the bigger Android Studio installer)
The Android SDK and the associated commands work roughly exactly the same way on Windows, Mac OS X and Linux. Therefore, the instructions given here should be applicable regardless of your operating system.
The «android» command
If you have the Android SDK in place, you can locate the «android» command inside the tools directory. To try it, open up your command line, and issue the following kind of command (obviously replacing the beginning with the complete path to the actual directory where you have installed the SDK files):
Then press ENTER. By default, the Android SDK Manager will be displayed:
The Android SDK Manager lets you download and install, uninstall, and update necessary packages (SDK tools and platforms) that you need to develop an Android app. Full information about this GUI tool can be found on the Android developer site:
Going back to the command line, to list some of the things that can be done with the android tool, try this:
You should see commands with corresponding descriptions like the following below:
List Targets
To display the list of available Android target(s) / SDK versions that are available on the current system, try this:
You should see available targets like the following:
(Followed by other targets, if available)
Take note of the «id» line in this list, as that is the value that you can use in other places to refer to the specific version of an Android SDK target.
List Devices
To display a list of available Android device(s) that are currently configured, try this:
You should see available device definitions like the following:
(Followed by other devices, if available)
The Android Emulator
The Android Emulator emulates a complete Android device in software on a desktop computer. Therefore, to do Android development, you will not necessarily need to have an actual physical device at your disposal. You can locate the Android emulator tool as part of the Android SDK here:
Creating an Android Virtual Device
To run an instance of the Android operating system inside the emulator, you will first need to create an Android Virtual Device. You can do this with the following kind of a command:
Type the entire command on one line. Then press ENTER in the end. You should see something like this:
Cool! You have successfully created your Android Virtual Device.
Running your virtual device
To execute your new Android Virtual Device in an emulator, you can simply do this:
You should see and Android Emulator starting up and something like below in the terminal window:
The emulator itself should have opened in a new window, running a complete copy of the Android operating system.
Creating a new Android application
To create the initial source code for a new Android application, try this:
Again, the entire command would be written on one line. In the end, press ENTER. You should see something like this in your terminal window:
Awesome! You have successfully created an Android project.
Project Directory Contents
After successfully creating a project, navigate to your project directory. You should have the following files and directories in place:
Adjust the created project
It appears that the project creation script is stuck in an older version of Gradle, which is no longer compatible with newer versions of the overall Android build system. To address the problem, we will need to edit the «gradle-wrapper.properties» file under the «wrapper» subdirectory of the «gradle» directory in the created project source. In that file, by default, you find a line like this:
This can be changed to this:
(essentially, we change gradle version from 1.12 to 2.2.1, as it should be).
Likewise, in the generated «build.gradle» file, you should change the line that says «runProguard false» to «minifyEnabled true» (apparently, the syntax for this was changed at a point during the Android build system development cycle, but the «android create project» script is yet to be updated accordingly).
Compiling an Android Project
Before you compile your project, you must be connected to the internet in order for the gradle wrapper script to download the appropriate gradle software components. Once ready, to compile your Android project, navigate to your project directory on your command line (change directory), and do this:
For Mac or Linux:
The «gradlew» script is a wrapper script that then downloads all the necessary components that are needed to run the complete Gradle build. Once any necessary downloading is completed, the wrapper continues to execute the actual build. In the end, you should find the final APK installer in the «build» -> «outputs» -> «apk» directory under your project directory:
The installer to use is the second one in the list above (*-debug.apk).
The APK file
The Android Application Package, most commonly known as APK, is the installer and distribution file format for Android apps. Once your Gradle build has successfully completed, you should have an APK file that was generated from your source code.
The Android Debug Bridge
Android Debug Bridge (adb) is a command line tool used to communicate with a connected android device or emulator. It is commonly used for installation and debugging of apps during development.
You can locate the Android Debug Bridge executable (adb or adb.exe) here:
For complete official information about Android Debug Bridge, refer to the link below:
Bear in mind that before you install and debug an app, you must have one of the following in place:
(1) Connected Android device
(2) Running Android Emulator
Select the appropriate device
If you have a device connected, or an emulator running, to check if it is connected, try this:
This command will list all physical Android gadgets (tablets, phones, etc.) that you have connected to your development computer (via USB or otherwise), as well as any Android emulator instances that you may have running. If ever you plug in several devices, and/or run several emulators simultaneously, observing this list becomes increasingly important in selecting which one of them you would wish to interact with.
You should see something like the following:
To select a specific Android instance (whether device or emulator) to use, you can use the -s parameter of adb to specify which device to use, followed by the device ID from the list above. For example:
Installing an Android app
If this is your first time to install the particular app on the device or emulator, you can simply do this:
Then press ENTER and the following kind of information should be displayed in your terminal window:
Congratulations. You have successfully installed your Android app. It should now appear in the application menu of the device or emulator.
Replacing or reinstalling an app
To replace a previously installed app, the -r parameter of adb can be used. Try this:
Take note that the -r parameter means to replace an existing application. So, doing this ..
.. would generate an error, as shown below, since you are trying to install the same version of the app without using the -r parameter.
Debugging with adb logcat
The logcat command of adb is used to display system messages of a running Android OS (whether device or emulator). This is extremely useful to find out what is happening to an application or a device. Any and all messages generated by the device are shown here, giving a wealth of information for any kind of troubleshooting requirements.
You should see logs in your terminal window something like the following:
The logcat command of adb is commonly used by app developers and testers to trace the cause of crashes in an Android app during development.
Filtering logs
To filter logs using the logcat command of adb. You can do this:
The asterisk (*) character means ALL logs and the hash (#) character is the filter, which can be one of the following:
For example, doing this .
. would result to something like the following:
When filtering logs, a priority is followed from top to bottom. If *:W is used, Warnings are displayed first followed by Errors then Fatal logs. Like in the sample above. Thus, filtering with Verbose (default) will display the logs starting from Verbose down to Fatal.
Furthermore, you can also filter logs with a keyword. Try this (this only works on a Unix-like environment, such as Mac OS X or Linux):
For example, doing this .
. would result to something like the following:
The pipe (|) character means that logs from the logcat command of adb will be an input for grep (a command used to search for strings). The -i parameter of grep means ignore case.
Interrupting logcat
You can at any time interrupt the logcat command by pressing Ctrl+C in your terminal window. You should then have returned to your command line.
Congratulations!
You have completed this tutorial. You have successfully created an Android application purely on the command line.
Источник