Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzhenxing committed Jul 13, 2021
0 parents commit 839daff
Show file tree
Hide file tree
Showing 25 changed files with 802 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
/.idea
/app/build
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
## ids.xml概述

`ids.xml`:为应用的相关资源提供唯一的资源`id``id`是为了获得`xml`中的对象需要的参数,也就是 `Object = findViewById(R.id.id_name);` 中的`id_name`

这些值可以在代码中用`android.R.id`引用到。
若在`ids.xml`中定义了**ID**,则在`layout`中可如下定义`@id/price_edit`,否则`@+id/price_edit`

> 优点
1. 命名方便,我们可以把一些特定的控件先命好名,在使用的时候直接引用`id`即可,省去了一个命名环节。
2. 优化编译效率:
- 添加`id`后会在`R.java`中生成;
- 使用`ids.xml`统一管理,一次性编译即可多次使用.
但使用`"@+id/btn_next"`的形式,每次文件保存`(Ctrl+s`)`后R.java`都会重新检测,如果存在该`id`则不生成,如果不存在就需要添加该`id`。故编译效率降低。


`ids.xml`文件内容:

```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="forecast_list" type="id"/>
<!-- <item name="app_name" type="string" />-->
</resources>
```

也许有人很好奇上面有一行被注释的代码,打开注释会发现编译会报一下错误:

```java
Execution failed for task ':app:mergeDebugResources'.
> [string/app_name] /Users/tanzx/AndroidStudioProjects/AaptDemo/app/src/main/res/values/strings.xml [string/app_name] /Users/tanzx/AndroidStudioProjects/AaptDemo/app/src/main/res/values/ids.xml: Error: Duplicate resources
```

因为`app_name`对于的资源已经在`value`中被声明了。

## public.xml概述

这个没有找到官方的具体说明,现有网络上的解释为:文件**RES/value/public.xml**用于将固定资源ID分配给Android资源。

[stackoverfloew:What is the use of the res/values/public.xml file on Android?](https://stackoverflow.com/questions/9348614/what-is-the-use-of-the-res-values-public-xml-file-on-android%E3%80%82)

[官网:选择要设为公开的资源](https://developer.android.com/studio/projects/android-library#PrivateResources)

`public.xml`文件内容:

```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public name="forecast_list" id="0x7f040001" type="id" />
<public name="app_name" id="0x7f070002" type="string" />
<public name="string3" id="0x7f070003" type="string" />
</resources>
```

## `aapt`进行`id`的固定

> 项目环境配置(PS:吐槽一下aapt已经被aapt2代替了,aapt相关资料几乎没有,环境搭建太费劲了~!)
>
> `com.android.tools.build:gradle:2.2.0`
>
> `distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip`
>
> `compileSdkVersion 24`
>
> `buildToolsVersion '24.0.0'`
先在`value`文件下按照上面的`ids.xml``public.xml`的内容以及文件名,生成对应的文件。

> 什么都不修改的直接编译
<img src="png/changed.png" style="zoom:50%;" />

通过直接编译之后的`R文件`的内容,可以看到我们想要的设置的资源`id`并没有按照我们预期的生成。

> `public.xml`文件拷贝到`build/intermediates/res/merged`对应的目录
```groovy
afterEvaluate {
for (variant in android.applicationVariants) {
def scope = variant.getVariantData().getScope()
String mergeTaskName = scope.getMergeResourcesTask().name
def mergeTask = tasks.getByName(mergeTaskName)
mergeTask.doLast {
copy {
int i=0
from(android.sourceSets.main.res.srcDirs) {
include 'values/public.xml'
rename 'public.xml', (i++ == 0? "public.xml": "public_${i}.xml")
}
into(mergeTask.outputDir)
}
}
}
}
```

<img src="png/source.png" style="zoom:50%;" />

这次我们可以直接看到资源`id`按照我们需要生成了。

> 这是为什么呢?
1. `android gradle`插件`1.3一下`版本可以直接将`public.xml`放在源码`res`目录参与编译;

2. `android gradle`插件`1.3+`版本在执行`mergeResource`任务时忽略了`public.xml`,所以`merge`完成后的`build`目录下的`res`目录下没有`public.xml`相关的内容。所以需要在编译时通过脚本将`public.xml`插入到`merge`完成后的`build`目录下的`res`目录下。之所以这样做可行,是因为`aapt`本身是支持`public.xml`的,只是`gradle`插件在对资源做预处`(merge)`时对`public.xml`做了过滤。

参考文章:

[android public.xml 用法](https://www.cnblogs.com/linghu-java/p/9548039.html)

[Android-Gradle笔记](https://ljd1996.github.io/2019/08/21/Android-Gradle%E7%AC%94%E8%AE%B0/)

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'
apply from: '../public-xml.gradle'
android {
compileSdkVersion 24
buildToolsVersion '24.0.0'

defaultConfig {
applicationId "com.example.aaptdemo"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

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

dependencies {
}
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
20 changes: 20 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aaptdemo">

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

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

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);
}
}
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/ic_launcher.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 839daff

Please sign in to comment.