google的android自帶的apps寫的是相當牛逼的,将其導入到eclipse中友善我們學習擴充。可惜關于導入的資料太少了,尤其是4.1之後的gallery和camera合二為一了。之前導4.0的camera那時不費吹灰之力就搞成了。下面的文字記錄是我摸索了近十天終于導成功的記錄,給大家分享了。
一、開發環境的搭建
sdk + adt + Eclipse的方式,改推adt-bundle的方式,故雜家以adt-bundle-windows-x86-20130522搭建的開發環境為基礎。(注:這裡預設JDK已經安裝配置好了。)
更新SDK-tools、Platform-tools、build-tools版本分别為22.0.5、18.0.1、18.0.1,
二、導入前的準備
1、 編譯Android4.2的源碼,目的是為了得到後面的四個classes檔案,如果通過其他途徑有這四個檔案也可以不編譯。4.2的源碼編譯需要在64位機子上。這四個檔案稍後會上傳到網上。
2、 将F:\AndroidSource4.2_r1\packages\apps下的Gallery2檔案夾和Camera檔案夾複制到桌面一份,後續将先導入Gallery2再導入Camera。
3、 在桌面建立一個Camera4.2Need檔案夾,将/out/target/commom/obj/JAVA_LIBRARIES/core_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/framework_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/mp4parser_intermediates/、/out/target/commom/obj/JAVA_LIBRARIES/
xmp_toolkit_intermediates這四個路徑下的classes.jar檔案分别重命名為core_intermediates、framework_intermediates、mp4parser_intermediates、xmp_toolkit_intermediates放到Camera4.2Need檔案夾,後續這個檔案夾可以放到工作空間以免丢失。
三、開始導入
1、 打開eclipse,點選File---New----Project---Android---Android Project from Existing Code,選擇桌面的那個Gallery2檔案夾,test不要勾選,copy
projects到工作空間要勾選。
導入後會報錯如下,不用管:

2、 導入後首先檢查gen檔案夾是否解析了rs檔案,正常解析的話會生成ScriptC_convolve3x3.java檔案和convolve3x3.d,如果沒有生成則說明目前開發環境無法解析rs檔案(com.android.gallery3d.filtershow.filters包下的convolve3x3.rs檔案),需要重新配置開發環境。
3、 右擊工程的屬性或按alt+enter快捷鍵,選中Java Build Path,點選Source,選擇Add
Folder,将gallerycommon和src_pd勾選上。如果這兩個檔案夾不勾選,後續會找不到很多類和包,而這些缺失并不是缺少.classes檔案導緻的。如果提示重複,就按如下步驟進行.
4、 将Android Dependenices檔案夾下的那兩個空包删除。找到源碼Gallery2下的gallerycommon檔案夾,需要将這裡的檔案按對應的包名,添加到gallerycommon檔案夾下。建立gallerycommon檔案夾,建立包名com.android.gallery3d.common;然後将gallerycommon檔案夾下的java檔案複制過去。其他的包類似。對源碼裡的src_pd也做同樣的操作。【注:在java
build path屬性裡,source目錄下src_pc和gallerycommon這兩個檔案夾必須勾選,否則建立的包會自動跑到src目錄下。】最終得到的結果如下:
[注:這個過程比較靈活,隻要最終将兩個檔案夾下的包導入進去即可,確定包名正确。]
5、 按alt+enter快捷鍵,選中java build path,點Libraries---Add Library,選擇User
Library,并将下面的那個選項勾上,表示它的級别很高。New一個Library,名字為Camera4.2Need,将剛準備的四個.jar檔案添加進來。
6、 然後點Order and Export,将Camera4.2Need這個庫的位置放在Android4.2.2的前面。調整後的順序如下:
7、 點選res檔案夾,點開values檔案夾,可以看到裡面的filtershow_strings.xml、photoeditor_strings.xml、strings.xml有錯誤,原因是裡面有重複定義。将報錯的地方删掉即可!每删掉一個地方,對應的另一個檔案報錯的地方就會恢複正常。可以按照這種方法對values-xx開頭的檔案夾都做相同的處理,但事實上,由于我們隻要簡體中文,是以隻保留values-zh-rCN檔案夾即可,對此檔案夾的報錯也作上述處理。其他的values-xx檔案夾統統删掉。
8、 在導入camera檔案之前需要逐一對照camera下的res檔案夾和目前gallery工程下的res檔案夾下的檔案有無重名。如兩者的values檔案夾下都有atrrs.xml檔案,需要将camera的那一份重命名為atrrs_camera.xml,否則會發生覆寫導緻出錯。導入時點選Gallery工程,注意一定是導到Gallery工程目錄下。右擊選擇—import---General---File
System,其中的.git檔案夾及test檔案夾、Android.mk、CleanSpec.mk不要選。
9、 點選Clean,重新編譯仍然會有錯。WidgetTypeChooser.java報錯,但實質上并不是這個檔案出了問題。這也正是ADT-Bundle安裝的eclipse沒有自己下載下傳的Eclipse好用的表現之一,當出現大量錯誤時,前者會亂報錯。這時gen檔案夾下com.android.gallery3d下的R檔案已經生成。整個工程的錯誤在包com.android.camera.ui下的各個java檔案中,這些java檔案都導入了com.android.camera.R,但實質上和camera相關的資源ID都被整合到gen檔案夾下的com.android.gallery3d.R檔案中了。也即本來應該是camera的R檔案整合到gallery的R檔案中去了,com.android.camera.R這個檔案根本沒有生成。是以凡是import
com.android.camera.R;有這句話的地方一定會報錯,奇怪的是eclipse還顯示不出來。用import com.android.gallery3d.R;來代替這句話即可。
10、 在修改了com.android.camera.ui包下的各個java檔案後,對com.android.camera包下的java檔案也要進行修改,凡是引用了R的地方都會報錯,但eclipse可能顯示不出來。一定要保證com.android.camera.ui包下凡是引用R檔案的地方都要導入Gallery3d.R包。
11、 在編譯的時候會将錯誤顯示到WidgetTypeChooser.java中,并在下方輸出錯誤為:The project was not built since its build path is incomplete.
package android.view;
import android.content.res.CompatibilityInfo;
/** @hide */
public class CompatibilityInfoHolder {
private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
public void set(CompatibilityInfo compatInfo) {
if (compatInfo != null && (compatInfo.isScalingRequired()
|| !compatInfo.supportsScreen())) {
mCompatInfo = compatInfo;
} else {
mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
}
}
public CompatibilityInfo get() {
return mCompatInfo;
public CompatibilityInfo getIfNeeded() {
CompatibilityInfo ci = mCompatInfo;
if (ci == null || ci == CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO) {
return null;
return ci;
}
12、 再次clean編譯會報錯:某個函數需要的api版本要大于11,目前最低版本為10.需修改AndroidManifest.xml檔案,最低api版本為11,目标版本為17,因為我們的手機是4.2對應級别為17。
13、 再次clean編譯會報錯:在ImageFilterSharpen.java函數裡找不到forEach_root()函數。通過對源碼編譯的輸出路徑out進行搜尋,在/out/target/common/obj/APPS/GALLERY2_intermediates/src/com/android/Gallery3d/filtershow/filters檔案夾下找到ScriptC_convolve3x3.java一共可以找到3份同名的檔案,隻有這個路徑下的代碼有forEach_root(Allocation,
Allocation)接口。将此檔案複制出來一份,粘貼到com.android.gallery3d.filtershow.filters下,将名字改為MyScriptC_convolve3x3.java,否則會跟gen檔案下的檔案重名,并在類的構造函數等其他地方更改名字,包名也要改。
/*
* Copyright (C) 2011-2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* This file is auto-generated. DO NOT MODIFY!
* The source Renderscript file: packages/apps/Gallery2/src/com/ku/cameragal/filtershow/filters/convolve3x3.rs
package com.android.gallery3d.filtershow.filters;
import android.renderscript.*;
import android.content.res.Resources;
/**
* @hide
public class MyScriptC_convolve3x3 extends ScriptC {
private static final String __rs_resource_name = "convolve3x3";
// Constructor
public MyScriptC_convolve3x3(RenderScript rs) {
this(rs,
rs.getApplicationContext().getResources(),
rs.getApplicationContext().getResources().getIdentifier(
__rs_resource_name, "raw",
rs.getApplicationContext().getPackageName()));
}
public MyScriptC_convolve3x3(RenderScript rs, Resources resources, int id) {
super(rs, resources, id);
__I32 = Element.I32(rs);
__ALLOCATION = Element.ALLOCATION(rs);
__F32 = Element.F32(rs);
__U8_4 = Element.U8_4(rs);
private Element __ALLOCATION;
private Element __F32;
private Element __I32;
private Element __U8_4;
private FieldPacker __rs_fp_ALLOCATION;
private FieldPacker __rs_fp_F32;
private FieldPacker __rs_fp_I32;
private final static int mExportVarIdx_gWidth = 0;
private int mExportVar_gWidth;
public synchronized void set_gWidth(int v) {
setVar(mExportVarIdx_gWidth, v);
mExportVar_gWidth = v;
public int get_gWidth() {
return mExportVar_gWidth;
public Script.FieldID getFieldID_gWidth() {
return createFieldID(mExportVarIdx_gWidth, null);
private final static int mExportVarIdx_gHeight = 1;
private int mExportVar_gHeight;
public synchronized void set_gHeight(int v) {
setVar(mExportVarIdx_gHeight, v);
mExportVar_gHeight = v;
public int get_gHeight() {
return mExportVar_gHeight;
public Script.FieldID getFieldID_gHeight() {
return createFieldID(mExportVarIdx_gHeight, null);
private final static int mExportVarIdx_gPixels = 2;
private Allocation mExportVar_gPixels;
public void bind_gPixels(Allocation v) {
mExportVar_gPixels = v;
if (v == null) bindAllocation(null, mExportVarIdx_gPixels);
else bindAllocation(v, mExportVarIdx_gPixels);
public Allocation get_gPixels() {
return mExportVar_gPixels;
private final static int mExportVarIdx_gIn = 3;
private Allocation mExportVar_gIn;
public synchronized void set_gIn(Allocation v) {
setVar(mExportVarIdx_gIn, v);
mExportVar_gIn = v;
public Allocation get_gIn() {
return mExportVar_gIn;
public Script.FieldID getFieldID_gIn() {
return createFieldID(mExportVarIdx_gIn, null);
private final static int mExportVarIdx_gCoeffs = 4;
private float[] mExportVar_gCoeffs;
public synchronized void set_gCoeffs(float[] v) {
mExportVar_gCoeffs = v;
FieldPacker fp = new FieldPacker(36);
for (int ct1 = 0; ct1 < 9; ct1++) {
fp.addF32(v[ct1]);
}
int []__dimArr = new int[1];
__dimArr[0] = 9;
setVar(mExportVarIdx_gCoeffs, fp, __F32, __dimArr);
public float[] get_gCoeffs() {
return mExportVar_gCoeffs;
public Script.FieldID getFieldID_gCoeffs() {
return createFieldID(mExportVarIdx_gCoeffs, null);
private final static int mExportForEachIdx_root = 0;
public Script.KernelID getKernelID_root() {
return createKernelID(mExportForEachIdx_root, 3, null, null);
public void forEach_root(Allocation ain, Allocation aout) {
// check ain
if (!ain.getType().getElement().isCompatible(__U8_4)) {
throw new RSRuntimeException("Type mismatch with U8_4!");
// check aout
if (!aout.getType().getElement().isCompatible(__U8_4)) {
// Verify dimensions
Type tIn = ain.getType();
Type tOut = aout.getType();
if ((tIn.getCount() != tOut.getCount()) ||
(tIn.getX() != tOut.getX()) ||
(tIn.getY() != tOut.getY()) ||
(tIn.getZ() != tOut.getZ()) ||
(tIn.hasFaces() != tOut.hasFaces()) ||
(tIn.hasMipmaps() != tOut.hasMipmaps())) {
throw new RSRuntimeException("Dimension mismatch between input and output parameters!");
forEach(mExportForEachIdx_root, ain, aout, null);
}
14、 在ImageFilterSharpen.java中,将ScriptC_convolve3x3 mScript改為MyScriptC_convolve3x3
mScript,并在相應地方同步更改類名。
經過上述步驟,Android4.2源碼Gallery2、Camera子產品成功導入Eclipse,并能正常編譯,稍後會将編譯出的apk共享出來。