Android Studio使用,live tamplate配置
1、new Module mylibrary,编译时需要在app的build.gradle中配置:
implementation project(':mylibrary'),
//mylibrary的build.gradle中implementation的内容
2、出现以下错误
FAILURE: Build failed with an exception.
* Where:
Initialization script '/private/var/folders/48/kbm80yjd17g60jh0fv0vm5l80000gn/T/Test_main__.gradle' line: 20
* What went wrong:
A problem occurred configuring project ':app'.
> SourceSet with name 'main' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
解决方案:
项目.idea/gradle.xml:
<GradleProjectSettings>
标签下少了
<option name="delegatedBuild" value="false" />
这行配置,然后给新项目添加这行配置,重启
意思是不委托gradle进行构建。现实效果也是加了这行配置之后就是jvm执行Java类的main方法了,而不是gradle当做task来执行了。
Android Studio 3.0 无法运行Java代码main() 方法的解决方案
Android Studio 升级到3.0 以后发现 运行Java 代码中的main()方法的时候grandle 就会报错的问题
解决方法 :
在gradle.properties文件中添加android.enableAapt2=false
3、Live tamplate配置
1)单例
/**
* 1、给mInstance分配实例分配对象
* 2、调用Singleton1的构造方法,初始化成员字段
* 3、Singleton1对象赋值给mInstance
* 以上三个步骤在jdk中可以是乱序的,会指令重排,所以会导致DCL失效,
*/
private volatile static $CLASSNAME$ mInstance = null; //volatile,防止指令重排,让双重检查更安全
private $CLASSNAME$() { } // 私有的构造方法
public static $CLASSNAME$ getInstance() {
if (mInstance == null) {
synchronized ($CLASSNAME$.class) {//DCL
if (mInstance == null) {
mInstance = new $CLASSNAME$();
}
}
}
return mInstance;
}
2)fragment切换
private void switchFragment(Fragment from, Fragment to) {
if (from != to){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (!to.isAdded()){
if (from != null){
transaction.hide(from);
}
if (to != null){
transaction.add($FragmentID$, to);
}
} else {
if (from != null){
transaction.hide(from);
}
if (to != null){
transaction.show(to).commit();
}
}
}
}
3)tag
public static final String TAG = $CLASSNAME$.class.getSimpleName();