Skip to content

Commit

Permalink
feat: 提交初始版本与说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbing16 committed Jan 31, 2023
0 parents commit 23d1c94
Show file tree
Hide file tree
Showing 72 changed files with 1,030 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# 手动打包apk

> 打包环境Windows电脑,不过命令应该都是一致的,需要提前把\SDK\build-tools\33.0.1添加到环境变量,或者下面的命名直接使用全路径
1、切换到项目根目录

```shell
cd ManualBuildApk
```

2、编译资源文件

```shell
aapt2 compile --dir .\app\src\main\res\ -o .\compiled\res.zip
```

3、链接资源文件

```shell
aapt2 link .\compiled\res.zip -o compiled\output.apk -I D:\DEV\SDK\platforms\android-31\android.jar --manifest .\app\src\main\AndroidManifest.xml --java app/src/main/java
```

4、java转class

```shell
javac -cp D:\DEV\SDK\platforms\android-31\android.jar -d compiled/ .\app\src\main\java\com\compose\manualbuildapk\*.java
```

5、class转dex

```shell
d8 --output compiled/ --lib D:\DEV\SDK\platforms\android-31\android.jar .\compiled\com\compose\manualbuildapk\*.class
```

6、合并dex与资源文件

```shell
zip -j compiled\output.apk compiled\classes.dex
```

如果使用window系统没有zip命令,**可以直接解压output.apk,然后把classes.dex放进去,然后压缩,改后缀为apk文件**

7、签名Apk

```shell
apksigner sign -ks .\debug.jks .\compiled\output.apk
```

本示例**签名文件密码为111111**

说明:

为了能打包通过,删除了非android.jar里面的属性、class

# 命令解析

## aapt2编译

[官方文档](https://developer.android.com/studio/command-line/aapt2?hl=zh-cn)

使用 `compile` 的一般语法如下:

```shell
aapt2 compile path-to-input-files [options] -o output-directory/
```

#### 示例:打包单个文件

```shell
aapt2 compile project_root/module_root/src/main/res/values-en/strings.xml -o compiled/
aapt2 compile project_root/module_root/src/main/res/drawable/myImage.png -o compiled/
```

aapt2 会自动将输出文件命名为 `values-en_strings.arsc.flat`。不过,存储在 drawable 目录中的已编译可绘制对象文件的文件名将为 `drawable_img.png.flat`

踩坑:**-o后面指定的目录必须存在,不然会提示下面两个错误**

```shell
error: 设备不识别此命令。 (22)
系统找不到指定的文件。 (2)
```

#### 示例:打包多个文件

当然我们也可以使用`--dir`来指定一个目录进行编译,这样每一个flat文件就会放在compiled目录下面

```shell
aapt2 compile --dir project_root/module_root/src/main/res/ -o compiled/
```

-o参数除了指定文件夹以外,还可以直接指定文件名,这样就会生成res.zip文件,解压以后就是所有的flat文件了这个zip文件(可以直接去链接)

```shell
aapt2 compile --dir project_root/module_root/src/main/res/ -o compiled/res.zip
```

## aapt2链接

使用`link`语法如下,**多个文件使用空格隔开**

```shell
aapt2 link path-to-input-files [options] -o outputdirectory/outputfilename.apk --manifest AndroidManifest.xml
```

#### 示例

```shell
aapt2 link .\compiled\res.zip -o compiled\output.apk -I D:\DEV\SDK\platforms\android-31\android.jar --manifest .\app\src\main\AndroidManifest.xml --java app/src/main/java
```

| 参数 | 说明 |
| ------ | ------------------------------------------------------------ |
| -I | 提供平台的 `android.jar` 或其他 APK(如 `framework-res.apk`)的路径,这在构建功能时可能很有用。 |
| --java | 指定R.java生成的位置 |

这个示例使用zip文件来链接多个flat文件,也可以使用-R参数来一个个文件指定

```shell
aapt2 link -R "@allFlatFile.txt" -o compiled\output.apk -I D:\DEV\SDK\platforms\android-31\android.jar --manifest .\app\src\main\AndroidManifest.xml --java app/src/main/java --auto-add-overlay
```

| 参数 | 说明 |
| ---- | ------------------------------------------------------------ |
| -R | 包含所有flat文件路径的txt文件,里面使用空格隔开,需要搭配--auto-add-overlay使用 |

**Windows电脑使用@allFlatFile.txt需要使用引号括起来**,**allFlatFile里面的内容得用空格隔开**

#### 资源id固定

当我们链接一次文件resources.arsc里面会分配一个id,当我们修改了资源文件,比如在colors.xml里面新增了一个图片,那么可能会导致两次链接的同一个资源的id发生变化

| 参数 | 说明 |
| ----------------- | ------------------------------------------------------------ |
| --emit-ids path | 在给定的路径下生成一个文件,该文件包含资源类型的名称及其 ID 映射的列表。它适合与 `--stable-ids` 搭配使用。 |
| --stable-ids path | 使用通过 `--emit-ids` 生成的文件,该文件包含资源类型的名称以及为其分配的 ID 的列表。 |

## javac

```
javac -cp android.jar -d compiled/ .\app\src\main\java\com\compose\manualbuildapk\*.java
```

| 参数 | 说明 |
| --------------- | ------------------------------------------------------------ |
| -cp(-classpath) | 当前要编译的 java 文件中引用了其它的类(比如说:继承),但该引用类的 .class 文件不在当前目录下,这种情况下就需要在 javac 命令后面加上 -classpath 参数 |
| -d | class输出路径 |

可以手动指定java文件的位置,空格键隔开,也可以直接传递文件的类型

```shell
javac -cp android.jar -d compiled/ "@allJavaFile.txt"
```

**allJavaFile.txt里面的内容一行一个**

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
39 changes: 39 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
plugins {
id 'com.android.application'
}

android {
namespace 'com.compose.manualbuildapk'
compileSdk 32

defaultConfig {
applicationId "com.compose.manualbuildapk"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.compose.manualbuildapk;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.compose.manualbuildapk", appContext.getPackageName());
}
}
31 changes: 31 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.compose.manualbuildapk">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ManualBuildApk"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>
14 changes: 14 additions & 0 deletions app/src/main/java/com/compose/manualbuildapk/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.compose.manualbuildapk;


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);
}
}
47 changes: 47 additions & 0 deletions app/src/main/java/com/compose/manualbuildapk/R.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package com.compose.manualbuildapk;

public final class R {
public static final class color {
public static final int black=0x7f010000;
public static final int black2=0x7f010007;
public static final int purple_200=0x7f010001;
public static final int purple_500=0x7f010002;
public static final int purple_700=0x7f010003;
public static final int teal_200=0x7f010004;
public static final int teal_700=0x7f010005;
public static final int white=0x7f010006;
}
public static final class drawable {
public static final int a=0x7f020001;
public static final int ic_launcher_background=0x7f020002;
public static final int ic_launcher_foreground=0x7f020003;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class mipmap {
public static final int ic_launcher=0x7f040000;
public static final int ic_launcher_round=0x7f040001;
}
public static final class string {
public static final int app_name=0x7f050000;
}
public static final class style {
/**
* Base application theme.
*/
public static final int Theme_ManualBuildApk=0x7f060000;
}
public static final class xml {
public static final int backup_rules=0x7f070000;
public static final int data_extraction_rules=0x7f070001;
public static final int themes=0x7f070002;
}
}
30 changes: 30 additions & 0 deletions app/src/main/res/drawable-v24/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
Binary file added app/src/main/res/drawable/a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 23d1c94

Please sign in to comment.