Android Studio use notes

1. Project related

Keep the developer's IDE version consistent.
IDE uniform encoding format is UTF-8

Package name classification

name Remarks
activity We continue to split according to modules, and divide the activities of different modules into different packages. Such as LoginActivity and so on;
fragment Similar to activity
adapter All adapters are put together.
entity Put all the entities together. There are corresponding setter and getter methods. The role of entity (entity class) is generally to map to the data table. The JSON data class .Entity is also like this. Entity should only have attributes, otherwise it is not called Entity. Only when there are hundreds of Entity, it needs to be divided into modules.
db Encapsulation of SQLLite related logic.
engine Put all business-related classes together.
ui Put all custom controls in this package.
utils Put all public methods here.
interfaces In the real sense of the interface, the name starts with I.
listener Based on the Listener interface, the name starts with On.
extensions Extension function

Comment

TODO needs to be implemented. The currently unimplemented
FIXME function code has a problem and needs to be repaired.

Checkstyle

version control

res/layouts subcontracting

When the res/layout xml is too many and messy, it can be dealt with.
Insert picture description here
There is no problem. Through the setting, it can be more intuitive.
Insert picture description here
Step 1: Right-click the res of the project, select New -> Directory, and name it layouts to avoid duplication.
Step 2: Select the layouts directory, right-click and select New -> Folder -> Res Folder, for example, name it home, chat....
Step 3: Add the layout directory (just like res/layout in Android). The
final step: Modify Gradle script, app/build.gradle

sourceSets {
    
    
    main {
    
    
        res.srcDirs = ['src/main/res',
                       'src/main/res/layouts/chat',
                       'src/main/res/layouts/home',
                       'src/main/res/layouts/profile'
        ]
    }
}

Solve the conflict between imported jar and aar


2. Compile, run and debug, performance

Analyze the crashed stack information

Copy the crash information, click the Analyze menu, select Analyze Stack Trace, the Insert picture description here
following content will be displayed, and you can click on the specific location for analysis
Insert picture description here

Associate and debug a running APP

Consider two situations:

  • There is a problem with a certain version or xxxx application running on the machine, and the cost of re-Run is too great.
  • A third-party APP cannot compile the project.

At this time, as long as you have the code.
Android Studio menu bar->Run->Attach Debugger to Android Process
or click the icon in the toolbar, you can also
Insert picture description here
select an APP you need to debug, and click show all processes to display a lot of running
Insert picture description here
clicks After that, the following interface will be displayed.
Insert picture description here
Put a breakpoint on the code and you can debug
Insert picture description here

signature

System signature (platform.x509.pem and platform.pk8), how to export to Android Studio signed APK;

openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out platform.pem
openssl pkcs12 -export -in platform.x509.pem -out platform.p12 -inkey  platform.pem -password pass:123456 -name android
keytool -importkeystore -deststorepass 123456 -destkeystore ./platform.jks -srckeystore ./platform.p12 -srcstoretype PKCS12 -srcstorepass 123456

debug {
    
    
	storeFile file("../platform.jks")
	storePassword "123456"
	keyAlias "android"
	keyPassword "123456"
}

Optimization acceleration

# 提升 JVM 的堆内存
org.gradle.jvmargs=-Xmx1536m
# 配置 可用内存

Performance testing related


3. grlade related

name Remarks
API Similar to compile, compile-time dependency and runtime dependency, support dependency transfer
Implementation Dependency transfer is not supported, and the underlying module cannot access the library dependent on the upper module
CompileOnly Similar to provided, the dependency is only performed at compile time, and the dependency is not packaged into the APP
RuntimeOnly Similar to APK, it only packs dependencies into APK, but the dependent classes cannot be obtained at compile time
AnnotationProcessor Similar to apt, it is an annotation processor dependency
Test Implementation The dependency of the Java test library only takes effect in the test environment
AndroidTestImplementation Android test library dependency, only effective in the test environment
[Flavor]+Api Only dependent on a Flavor, written as: Flavor + name + dependent
// api和compile关键字作用效果是一样的,在高版本的gradle,使用compile关键字会报错并提示使用api关键字代替
api 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:appcompat-v7:26.1.0'
api ('com.android.support:appcompat-v7:26.1.0') {
    
    
	transitive = true; // true 支持传递依赖, false关闭传递依赖
}
api("com.afollestad.material-dialogs:core:0.9.5.0") {
    
    
	exclude group: 'com.android.support' // 删除 group 指定的module
}

// 同一个模块下,使用效果一样的.
// 不同模块会有区别,比如 App 的 moduleA 使用下面的配置,app 引用 moduleA 后,
// implementation关键字的appcompat-v7包无法在app module中使用
// 使用compile关键字的design包则可以使用
implementation 'com.android.support:appcompat-v7:26.1.0' 
compile 'com.android.support:design:26.1.0'

// compileOnly经常用于解决依赖冲突等问题,一般第三方库中,比较常用的依赖,如support、gson、Eventbus等等
compileOnly 'com.android.support:cardview-v7:26+'

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

// 测试库
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

Combination dependency: related libraries become a group, it is easy to delete and add

implementation([
	'io.reactivex.rxjava2:rxjava:2.0.1',
	'io.reactivex.rxjava2:rxandroid:2.0.1'
])

Dependency management: the more libraries the project has, the more it becomes difficult to manage, unified management

// 根目录建议一个文件 xxx.gradle
ext {
    
    
	android = [
		compileSdkVersion = 28
    	buildToolsVersion = "28.0.3"
    	minSdkVersion = 21
    	targetSdkVersion = 28
	]
	
	dependencies = [
		"appcompat":"com.android.support:appcompat-v7:26.1.0"
	]
}

// 引用的 build.gradle 中加入 xxx.gradle
apply from:"xxx.gradle"

// 引用
android {
    
    
	compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion

	defaultConfig {
    
    
		minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
	}
}

dependencies {
    
    
	implementation rootProject.ext.dependencies["appcompat"]
}

4. Plug-in-efficiency improvement

Plug-in name Remarks
ADB Idea ADB tools
Material UI Theme Theme color
Rainbow Brackets Parentheses, square brackets and braces color
Butterknife Zelezny Butterknife quick import
Alibaba Java Coding Guidelines Alibaba Java Development Protocol
Git Commit Template git commit submit template plugin

ADB Idea

ADB Idea provides single-click shortcut commands to start, uninstall, terminate applications, revoke permissions and clear application data.
Take Android 4.0 and above as an example, Tools->ADB Ideayou can see the use menu
Insert picture description here

Material UI Theme

Use tutorial
Insert picture description here

Rainbow Brackets

It does solve the programmer's pain point, that is, nesting or multiple parentheses are not easy to find and locate;
this plug-in uses colors to mark parentheses, square brackets and braces with colors, which is clear and clear;
Insert picture description here

Butterknife Zelezny

Alibaba Java Coding Guidelines

Insert picture description here
How to use, click 编码规约扫描, it will list all irregularities
Insert picture description here Insert picture description here Insert picture description here

Git Commit Template

Insert picture description here

5. Settings related

Class comment template

Settings->Editor->File and Code Templates
Insert picture description here

/**
 *
 * @Author: hailong.qiu [email protected]
 * @Maintainer: hailong.qiu [email protected]
 * @Date: ${DATE}
 * @Copyright: ${YEAR} www.andriodtvdev.com Inc. All rights reserved.
 */

Automatic guide package

Insert picture description here
Sometimes it is necessary to manually guide the package, which is really troublesome. Automatically guide the package can easily solve this problem.
Insert picture description here

Style Code

It is recommended to use Square Android Code Style
or use grandcentrix.xml to
Insert picture description here
import SquareAndroid.xml using Import Scheme, and finally select Copy to Project... to avoid repeated importing each time.

When entering a variable, you can change it if there is no prompt.

  • Non-shared, non-static member variables start with'm'
  • Start static member variables start with's'.
    Insert picture description here
    If you feel Tab and indentation are uncomfortable, you can change it.
    Insert picture description here
    If the order of functions is not in accordance with the proximity, you can change
    Insert picture description here

If you find that the code is formatted and there are spaces between the variables, you can change it. Finally , you
Insert picture description here
need to pay attention

Use the shortcut key Ctrl + Alt + Shift + L to open the configuration dialog, check the following
Optimize imports: optimize import
Rearrange code: rearrange the code

Guess you like

Origin blog.csdn.net/qw85525006/article/details/107892319