Category: Android 開發 Q&A

[Q&A]How to specify multiple source directory for Android library project

ant.properties

1
source.dir=src:src1/subpath:src2

tools/ant/build.xml
comment or remove the last fileset tag in jar tag

1
2
3
4
5
6
<jar destfile="${out.library.jar.file}">                        
       <fileset dir="${out.classes.absolute.dir}"
            includes="**/*.class"
            excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/Manifest.class ${project.app.package.path}/Manifest$*.class ${project.app.package.path}/BuildConfig.class"/>
       <!--fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" /-->
    </jar>
Tags :

[Q&A] how to use android annotations in gradle

build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
buildscript {
    repositories {
        mavenCentral()
    }
 
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}
 
repositories {
    mavenCentral()
    mavenLocal()
}
 
apply plugin: 'android-apt'
 
def AAVersion = '3.3.2'
 
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}
 
dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
Tags : ,

[Q&A] How to solve java.lang.OutOfMemoryError: GC overhead limit exceeded error in android studio

in build.gradle

1
2
3
4
5
6
android {
...
   dexOptions {
      javaMaxHeapSize "2g"
   }
}
Tags :

[Android][Q&A] Proguard retrace missing line numbers (Unknown Source)

add this 2 lines in proguard-project.txt or proguard.txt

1
2
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Tags :

[Q&A]How to center icon using customView in actionbar

1
2
3
4
5
6
7
ActionBar.LayoutParams layoutParams = 
new ActionBar.LayoutParams(
   ActionBar.LayoutParams.MATCH_PARENT,
   ActionBar.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_HORIZONTAL;
View customView = getLayoutInflater().inflate(R.layout.actionbar_custom_view, null);
actionBar.setCustomView(customView, layoutParams);
Tags :

[Q&A] tabs appearing ABOVE actionbar with custom view in NAVIGATION_MODE_TABS

1
2
3
4
5
6
actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));
View homeIcon = findViewById(
   Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
   android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setLayoutParams(new LinearLayout.LayoutParams(0, 0));
((View) homeIcon).setVisibility(View.GONE);
Tags :

[Q&A] How to solve “Gradle project sync failed. Please fix your project and try again”

1
File -> Invalidate caches / Restart
Tags : ,

[Q&A] android Build.CPU_ABI returns wrong cpu type

there is an experience,
in certain android x86 phone, with certain android os version,

a bundle APK includes

/libs/armeabi/a.so
/libs/armeabi-v7a/a.so
/libs/armeabi-v7a/b.so
/libs/x86/a.so
/libs/x86/b.so

String cpu = Build.CPU_ABI; // armeabi-v7a is returned which is wrong, but in other x86 phones, “x86″ is returned correctly

however, if /libs/armeabi/b.so is added back, or /libs/armeabi is removed

String cpu = Build.CPU_ABI; // x86 is returned correctly

I have not figured out the cause yet,
just keep in mind, there must be same amount shared libraries in each arch folders.

Tags : ,

[QA]How to get mimeType of file in a Android

    public static String getMimeType(String url) {
        String type = "*/*";
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            type = mime.getMimeTypeFromExtension(extension.toLowerCase());
        }
        return type;
    }

[QA] android start activity with Intent URI

Uri uri = new Uri.Builder()
 .scheme("http")
 .authority("ynjan.com")
 .appendPath("mypath")
 .appendPath("mysubpath")
 .build();
 Log.i(TAG, "uri=" + uri); // http://www.mydomain.com/mypath/mysubpath
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(intent);
<intent-filter>
   <action android:name="android.intent.action.VIEW"/> 
   <category android:name="android.intent.category.DEFAULT"/>
   <data 
      android:scheme="http"
      android:host="ynjan.com"
      android:path="/mypath/mysubpath"/>
</intent-filter>
Tags : ,