星期三, 19. 九月 2018 02:19上午 - BEAUTIFULZZZZ

0)前言

本文不讨论用IDE和文本编辑器开发的优劣,是基于以下两点考虑去尝试用命令行编译安卓APP的:

  • 了解安卓APP的编译过程,了解IDE干了什么事;
  • 放在打包服务器上需要自动化生成APP的脚本;

1)安装配置环境

  • 安装java

      sudo apt-get install openjdk-8-jdk-headless

    **note: **安装之前先要卸载之前版本的java,否则会报错!!! [error-1].

  • 安装SDK tools

      wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip

    建议将其解压到/opt目录下:

      mkdir -p /opt/android-sdk
    unzip sdk-tools-linux-3859397.zip -d /opt/android-sdk
  • 用sdkmanager安装SDK(#1)

    sdkmanager是用来查看、安装、更新、卸载Android SDK的命令行工具,官方说明如下:

    The sdkmanager is a command line tool that allows you to view, install, update, and uninstall packages for the Android SDK.

    The sdkmanager tool is provided in the Android SDK Tools package (25.2.3 and higher) and is located in android_sdk/tools/bin/.

    列出Installed packages和Available Packages,查看包安装情况:

      cd /opt/android-sdk/tools/bin
    ./sdkmanager --list

    安装platform tools 19(写文章时最新的是26),该工具包含adb和fastboot,该工具对应的API级别也是19:

      ./sdkmanager "platform-tools" "platforms;android-19"

    安装build tools 26.0.1(最新的),该工具包含aapt、apksigner、zipalign等编译、认证、打包工具:

      ./sdkmanager "platform-tools" "build-tools;26.0.1"

    最后你会在/opt/android-sdk/中看到build-tools、paltforms、tools三个文件夹~


2)编写简单Hello World程序

创建工程文件夹

cd ~/Downloads/
mkdir HelloAndroid
cd HelloAndroid

创建工程文件tree

mkdir -p src/com/example/helloandroid
mkdir obj
mkdir bin
mkdir -p res/layout
mkdir res/values
mkdir res/drawable

Make the file src/com/example/helloandroid/MainActivity.java and put that inside:

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Make the strings.xml file in the res/values folder. It contains all the text that your application uses:

<resources>
<string name="app_name">A Hello Android</string>
<string name="hello_msg">Hello Android!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

The activity_main.xml is a layout file which have to be in res/layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_msg"
tools:context=".MainActivity" />
</RelativeLayout>

You also have to add the file AndroidManifest.xml at the root:

<?xml version='1.0'?>
<manifest xmlns:a='http://schemas.android.com/apk/res/android' package='com.example.helloandroid' a:versionCode='0' a:versionName='0'>
<application a:label='A Hello Android'>
<activity a:name='com.example.helloandroid.MainActivity'>
<intent-filter>
<category a:name='android.intent.category.LAUNCHER'/>
<action a:name='android.intent.action.MAIN'/>
</intent-filter>
</activity>
</application>
</manifest>

最终文件构成为:

➜  HelloAndroid  tree
.
├── AndroidManifest.xml
├── bin
├── libs
├── obj
├── res
│ ├── drawable
│ ├── layout
│ │ └── activity_main.xml
│ └── values
│ └── strings.xml
└── src
└── com
└── example
└── helloandroid
└── MainActivity.java

3)编译工程

将工程路径设置为变量(方便一会使用):

export PROJ=~/Downloads/HelloAndroid

First, we need generate the R.java file which is necessary for our code:

cd /opt/android-sdk/build-tools/26.0.1/
./aapt package -f -m -J $PROJ/src -M $PROJ/AndroidManifest.xml -S $PROJ/res -I /opt/android-sdk/platforms/android-19/android.jar

compile the .java files:

cd ~/Downloads/HelloAndroid
javac -d obj -classpath src -bootclasspath /opt/android-sdk/platforms/android-19/android.jar src/com/example/helloandroid/*.java

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:

cd /opt/android-sdk/build-tools/26.0.1/
./dx --dex --output=$PROJ/bin/classes.dex $PROJ/obj

We can now put everything in an APK:

./aapt package -f -m -F $PROJ/bin/hello.unaligned.apk -M $PROJ/AndroidManifest.xml -S $PROJ/res -I /opt/android-sdk/platforms/android-19/android.jar
cp $PROJ/bin/classes.dex .
./aapt add $PROJ/bin/hello.unaligned.apk classes.dex

If you want, you can check the content of the package like this:

./aapt list $PROJ/bin/hello.unaligned.apk

至此,我们生成了一个hello.unaligned.apk文件,但是,它是不能安装到安卓手机里面的!因为它unaligned && unsigned


4)Align and Sign the package

keytool -genkeypair -validity 365 -keystore mykey.keystore -keyalg RSA -keysize 2048

cd /opt/android-sdk/build-tools/26.0.1/
./zipalign -f 4 $PROJ/bin/hello.unaligned.apk $PROJ/bin/hello.apk
./apksigner sign --ks mykey.keystore $PROJ/bin/hello.apk

使用keytool创建一个keystore,只要依次回答其问题即可,输入密码自己别忘了,今后会用到!运行成功之后会生成一个mykey.keystore文件,用于今后给apk签名。

note: 记住务必要先Align,然后再Sign,否则会出错 [error-2].


5)真机安装测试

安装并运行比较简单:

adb install $PROJ/bin/hello.apk
adb shell am start -n com.example.helloandroid/.MainActivity

note: 一般运行安装前,建议先运行adb logcat看看安卓有没有连接并开启开发者模式


6)自动化脚本

为了不用每次都要手输上面的每一步,我们将上面的操作整理成一个脚本run.sh

➜  HelloAndroid  cat run.sh
#!/bin/bash set -e AAPT="/opt/android-sdk/build-tools/26.0.1/aapt"
DX="/opt/android-sdk/build-tools/26.0.1/dx"
ZIPALIGN="/opt/android-sdk/build-tools/26.0.1/zipalign"
APKSIGNER="/opt/android-sdk/build-tools/26.0.1/apksigner" # /!\ version 26
PLATFORM="/opt/android-sdk/platforms/android-19/android.jar" function build(){
echo "Generating R.java file..."
$AAPT package -f -m -J src -M AndroidManifest.xml -S res -I $PLATFORM echo "Compiling..."
javac -d obj -classpath src -bootclasspath $PLATFORM src/com/example/helloandroid/*.java echo "Translating in Dalvik bytecode..."
$DX --dex --output=classes.dex obj echo "Making APK..."
$AAPT package -f -m -F bin/hello.unaligned.apk -M AndroidManifest.xml -S res -I $PLATFORM
$AAPT add bin/hello.unaligned.apk classes.dex echo "Aligning and signing APK..."
$ZIPALIGN -f 4 bin/hello.unaligned.apk bin/hello.apk
$APKSIGNER sign --ks mykey.keystore bin/hello.apk
} function clean(){
echo "Cleaning..."
rm -rf classes.dex
rm -rf bin/*
rm -rf obj/*
rm -rf src/com/example/helloandroid/R.java
} function program(){
echo "Launching..."
adb install -r bin/hello.apk
adb shell am start -n com.example.helloandroid/.MainActivity
} if [ "$1" == "all" ]; then
clean
build
program
elif [ "$1" == "clean" ]; then
clean
elif [ "$1" == "build" ]; then
build
elif [ "$1" == "program" ]; then
program
else
echo "error"
fi

并编写一个makefile脚本,通过调用run.sh实现编译、清除、安装各种操作:

➜  HelloAndroid  cat makefile 

clean:
./run.sh clean build:
./run.sh build program:
./run.sh program all:
./run.sh all

至此,我们完成了一个简单的命令行版的Hello World工程!当然,大多数非常复杂的安卓工程需要用IDE去开发,或者开发用IDE打包用命令 ~ 下次我会把一个稍微复杂点的蓝牙安卓工程改造成命令行版。

常见错误

error-1:

➜  HelloAndroid  javac -d obj -classpath src -bootclasspath /opt/android-sdk/platforms/android-19/android.jar src/com/example/helloandroid/*.java

javac: option --boot-class-path not allowed with target 1.10

error-2:

➜  HelloAndroid  ./build.sh test
Cleaning...
Generating R.java file...
Compiling...
Translating in Dalvik bytecode...
Making APK...
'classes.dex'...
Aligning and signing APK...
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.android.apksigner.PasswordRetriever (file:/opt/android-sdk/build-tools/26.0.1/lib/apksigner.jar) to method java.io.Console.encoding()
WARNING: Please consider reporting this to the maintainers of com.android.apksigner.PasswordRetriever
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Keystore password for signer #1:
Launching...
Failed to install bin/hello.apk: Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES: Failed to collect certificates from /data/app/vmdl1775334521.tmp/base.apk: META-INF/MYKEY.SF indicates /data/app/vmdl1775334521.tmp/base.apk is signed using APK Signature Scheme v2, but no such signature was found. Signature stripped?]

需要If you need to run zipalign, do it before the APK is signed,因此将APKSIGNER放在ZIPALIG之后(#4)

error-3:

$ avdmanager
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 5 more

需要加:export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

LINKS

[1].sdkmanager 用法

[2].Build your app from the command line

[3].How to make Android apps without IDE from command line

[4].Android signing apk signature V2

[5].What are the Android SDK build-tools, platform-tools and tools? And which version should be used?

[6].Not able to build code after installing latest java version 1.9

[7].Android SDK is not installed or is not configured properly, environment looks ok

[8].Command line tools only Download

[9].GITHUB this project

@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975

[编译] 5、在Linux下搭建安卓APP的开发烧写环境(makefile版)—— 在Linux上用命令行+VIM开发安卓APP的更多相关文章

  1. [编译] 8、在Linux下搭建 stm8 单片机的开发烧写环境(makefile版)

    目录 一.SDCC(Small Device C Compiler)编译环境搭建 1.1.下载 1.2.编译 1.3.测试 二.Hex2Bin+命令行烧写工具配置使用 2.1.下载工具安装配置 2.2 ...

  2. [编译] 3、在Linux下搭建51单片机的开发烧写环境(makefile版)

    星期二, 10. 七月 2018 01:01上午 - beautifulzzzz 一.SDCC(Small Device C Compiler)编译环境搭建 SDCC是一个小型设备的C语言编译器,该编 ...

  3. 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境

    在Linux下没有像keli那样好用的IDE来开发51单片机,开发环境只能自己搭建了. 第一步:安装交叉编译工具 a) 安装SDCC sudo apt-get install sdcc b)测试SDC ...

  4. [编译] 7、在Linux下搭建安卓APP的开发烧写环境(makefile版-gradle版)—— 在Linux上用命令行+VIM开发安卓APP

    April 18, 2020 6:54 AM - BEAUTIFULZZZZ 目录 0 前言 1 gradle 安装配置 1.1 卸载系统默认装的gradle 1.2 下载对应版本的二进制文件 1.3 ...

  5. [编译] 9、在Linux下搭建 nordic 最新基于 zephyr 的开发烧写环境

    目录 前言 1.概述 2.安装工具 3.获取 nRF Connect SDK 源码 4.安装 Python modules 5.安装 toolchain 6.下载 nRF Command Line T ...

  6. [编译] 4、在Linux下搭建nRF51822的开发烧写环境(makefile版)

    星期日, 09. 九月 2018 07:51下午 - beautifulzzzz 1.安装步骤 1) 从GNU Arm Embedded Toolchain官网下载最新的gcc-arm工具链,写文章时 ...

  7. 在Linux下搭建nRF51822的开发烧写环境(makefile版)

    http://www.qingpingshan.com/m/view.php?aid=394836

  8. Linux下搭建 Cocos2d-x-2.1.4 编译环境

    [tonyfield 2013.09.04 ] 参考 Linux下搭建 Cocos2d-x-2.1.4 编译环境 导入 HelloCpp 例程 1. Java 入口 HelloCpp.java Hel ...

  9. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

随机推荐

  1. Go命令官方指南【原译】

    启动错误报告 编译包和依赖项 删除目标文件和缓存的文件 显示包或符号的文档 打印Go环境信息 更新包以使用新API Gofmt(重新格式化)包源 通过处理源生成Go文件 下载并安装包和依赖项 编译并安 ...

  2. mathJax基础语法-0基础开始,(这是网上抄来的如果有权限和版权问题联系本人处理,仅供学术参考)

  3. RROR: [XSIM 43-3238] Failed to link the design.

    仿真时遇到上述错误,在tcl下运行 set_property -name {xsim.elaborate.xelab.more_options} -value {-cc clang} -objects ...

  4. Django组件之用户认证组件

    一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenticate( ...

  5. java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'

    今天在跑项目时遇到java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'报错,自己也是摸索了很久,一 ...

  6. 基于.net的分布式系统限流组件(限流算法:令牌算法和漏斗算法)

    转载链接:https://www.cnblogs.com/vveiliang/p/9049393.html 1.令牌桶算法 令牌桶算法是比较常见的限流算法之一,大概描述如下: 1).所有的请求在处理之 ...

  7. urllib-Proxy

    代理的使用: 首先,当我们正确爬取一个网页时,发现代码没有错误,可就是不能爬取网站.原因是有些网站设置了反爬取手段,就是知道你就是用python代码爬取该网站,设置了屏蔽.如果我们又想爬取该网站,便要 ...

  8. Spring,Spring MVC及Spring Boot区别

    什么是Spring?它解决了什么问题? 我们说到Spring,一般指代的是Spring Framework,它是一个开源的应用程序框架,提供了一个简易的开发方式,通过这种开发方式,将避免那些可能致使代 ...

  9. Anconda 3.7安装以及使用详细教程

    Anconda 3.7安装以及使用详细教程 2019-04-17    22:42:03 一.下载anconda 3.7 链接地址:官方地址 二.安装 双击下载好的Anaconda3-2019.03- ...

  10. JavaWeb-SQL-Servlet-JSP学做购物系统——日志一

    一.JDBCUtil 本人自半路出家学码代码以来,bug不断,一直不顺利.路漫漫.话不多说,上bug:T_T; 代码是这样的: 自定义的方法,插入一个sql语句:execUpdate(String s ...