网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程

来源:互联网 发布:淘宝店铺常用推广方式 编辑:程序博客网 时间:2024/04/30 14:14

   google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展。可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了。之前导4.0的camera那时不费吹灰之力就搞成了。下面的文字记录是我摸索了近十天终于导成功的记录,给大家分享了。

一、开发环境的搭建

1、      点击http://developer.android.com/sdk/index.html下载adt-bundle-windows-x86-20130522426M)。尽管部分机子上有非adt-bundle搭建的开发环境,但考虑到google已不支持以往android sdk + adt + Eclipse的方式,改推adt-bundle的方式,故杂家以adt-bundle-windows-x86-20130522搭建的开发环境为基础。(注:这里默认JDK已经安装配置好了。)

2、      点击http://developer.android.com/sdk/installing/installing-adt.html 选择在线或离线方式升级ADT版本为22.0.5。上面的adt-bundle自带的ADT版本是22.0.1,不支持解析rs文件,会导致后面的工作功亏一篑,因此一定要升级。如果在线安装ADT失败,考虑将C:\Windows\System32\drivers\etc\hosts文件添加一行:74.125.237.1       dl-ssl.google.com  就可以正常下载安装了,包括后续的更新build-toolsSDK-toolsPlatform-tools。具体参考前文

[android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found

更新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_intermediatesframework_intermediatesmp4parser_intermediatesxmp_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,将gallerycommonsrc_pd勾选上。如果这两个文件夹不勾选,后续会找不到很多类和包,而这些缺失并不是缺少.classes文件导致的。如果提示重复,就按如下步骤进行.

4、      将Android Dependenices文件夹下的那两个空包删除。找到源码Gallery2下的gallerycommon文件夹,需要将这里的文件按对应的包名,添加到gallerycommon文件夹下。新建gallerycommon文件夹,新建包名com.android.gallery3d.common;然后将gallerycommon文件夹下的java文件复制过去。其他的包类似。对源码里的src_pd也做同样的操作。【注:在java build path属性里,source目录下src_pcgallerycommon这两个文件夹必须勾选,否则新建的包会自动跑到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.xmlphotoeditor_strings.xmlstrings.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.mkCleanSpec.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文件中了。也即本来应该是cameraR文件整合到galleryR文件中去了,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. Cannot find the class file for android.view.CompatibilityInfoHolder. Fix thebuild path then try building this project GalleryUnknownJava Problem。即找不到android.view.CompatibilityInfoHolder,参考http://124.16.139.131:24080/lxr/source/frameworks/base/core/java/android/view/CompatibilityInfoHolder.java?v=android-4.0.4新建包android.view,然后将新建CompatibilityInfoHolder.java[备注:在有的版本上并没有遇到这个问题,有可能是android4.2将这个类去除了。]

[java] view plaincopy
  1. package android.view;  
  2.   
  3.  import android.content.res.CompatibilityInfo;  
  4.    
  5.  /** @hide */  
  6.  public class CompatibilityInfoHolder {  
  7.      private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;  
  8.    
  9.      public void set(CompatibilityInfo compatInfo) {  
  10.          if (compatInfo != null && (compatInfo.isScalingRequired()  
  11.                  || !compatInfo.supportsScreen())) {  
  12.              mCompatInfo = compatInfo;  
  13.          } else {  
  14.              mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;  
  15.          }  
  16.      }  
  17.    
  18.      public CompatibilityInfo get() {  
  19.          return mCompatInfo;  
  20.      }  
  21.    
  22.      public CompatibilityInfo getIfNeeded() {  
  23.          CompatibilityInfo ci = mCompatInfo;  
  24.          if (ci == null || ci  == CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO) {  
  25.              return null;  
  26.          }  
  27.          return ci;  
  28.      }  
  29.  }  


 

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文件下的文件重名,并在类的构造函数等其他地方更改名字,包名也要改。

[java] view plaincopy
  1. /* 
  2.  * Copyright (C) 2011-2012 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. /* 
  18.  * This file is auto-generated. DO NOT MODIFY! 
  19.  * The source Renderscript file: packages/apps/Gallery2/src/com/ku/cameragal/filtershow/filters/convolve3x3.rs 
  20.  */  
  21. package com.android.gallery3d.filtershow.filters;  
  22.   
  23. import android.renderscript.*;  
  24. import android.content.res.Resources;  
  25.   
  26. /** 
  27.  * @hide 
  28.  */  
  29. public class MyScriptC_convolve3x3 extends ScriptC {  
  30.     private static final String __rs_resource_name = "convolve3x3";  
  31.     // Constructor  
  32.     public  MyScriptC_convolve3x3(RenderScript rs) {  
  33.         this(rs,  
  34.              rs.getApplicationContext().getResources(),  
  35.              rs.getApplicationContext().getResources().getIdentifier(  
  36.                  __rs_resource_name, "raw",  
  37.                  rs.getApplicationContext().getPackageName()));  
  38.     }  
  39.   
  40.     public  MyScriptC_convolve3x3(RenderScript rs, Resources resources, int id) {  
  41.         super(rs, resources, id);  
  42.         __I32 = Element.I32(rs);  
  43.         __ALLOCATION = Element.ALLOCATION(rs);  
  44.         __F32 = Element.F32(rs);  
  45.         __U8_4 = Element.U8_4(rs);  
  46.     }  
  47.   
  48.     private Element __ALLOCATION;  
  49.     private Element __F32;  
  50.     private Element __I32;  
  51.     private Element __U8_4;  
  52.     private FieldPacker __rs_fp_ALLOCATION;  
  53.     private FieldPacker __rs_fp_F32;  
  54.     private FieldPacker __rs_fp_I32;  
  55.     private final static int mExportVarIdx_gWidth = 0;  
  56.     private int mExportVar_gWidth;  
  57.     public synchronized void set_gWidth(int v) {  
  58.         setVar(mExportVarIdx_gWidth, v);  
  59.         mExportVar_gWidth = v;  
  60.     }  
  61.   
  62.     public int get_gWidth() {  
  63.         return mExportVar_gWidth;  
  64.     }  
  65.   
  66.     public Script.FieldID getFieldID_gWidth() {  
  67.         return createFieldID(mExportVarIdx_gWidth, null);  
  68.     }  
  69.   
  70.     private final static int mExportVarIdx_gHeight = 1;  
  71.     private int mExportVar_gHeight;  
  72.     public synchronized void set_gHeight(int v) {  
  73.         setVar(mExportVarIdx_gHeight, v);  
  74.         mExportVar_gHeight = v;  
  75.     }  
  76.   
  77.     public int get_gHeight() {  
  78.         return mExportVar_gHeight;  
  79.     }  
  80.   
  81.     public Script.FieldID getFieldID_gHeight() {  
  82.         return createFieldID(mExportVarIdx_gHeight, null);  
  83.     }  
  84.   
  85.     private final static int mExportVarIdx_gPixels = 2;  
  86.     private Allocation mExportVar_gPixels;  
  87.     public void bind_gPixels(Allocation v) {  
  88.         mExportVar_gPixels = v;  
  89.         if (v == null) bindAllocation(null, mExportVarIdx_gPixels);  
  90.         else bindAllocation(v, mExportVarIdx_gPixels);  
  91.     }  
  92.   
  93.     public Allocation get_gPixels() {  
  94.         return mExportVar_gPixels;  
  95.     }  
  96.   
  97.     private final static int mExportVarIdx_gIn = 3;  
  98.     private Allocation mExportVar_gIn;  
  99.     public synchronized void set_gIn(Allocation v) {  
  100.         setVar(mExportVarIdx_gIn, v);  
  101.         mExportVar_gIn = v;  
  102.     }  
  103.   
  104.     public Allocation get_gIn() {  
  105.         return mExportVar_gIn;  
  106.     }  
  107.   
  108.     public Script.FieldID getFieldID_gIn() {  
  109.         return createFieldID(mExportVarIdx_gIn, null);  
  110.     }  
  111.   
  112.     private final static int mExportVarIdx_gCoeffs = 4;  
  113.     private float[] mExportVar_gCoeffs;  
  114.     public synchronized void set_gCoeffs(float[] v) {  
  115.         mExportVar_gCoeffs = v;  
  116.         FieldPacker fp = new FieldPacker(36);  
  117.         for (int ct1 = 0; ct1 < 9; ct1++) {  
  118.             fp.addF32(v[ct1]);  
  119.         }  
  120.   
  121.         int []__dimArr = new int[1];  
  122.         __dimArr[0] = 9;  
  123.         setVar(mExportVarIdx_gCoeffs, fp, __F32, __dimArr);  
  124.     }  
  125.   
  126.     public float[] get_gCoeffs() {  
  127.         return mExportVar_gCoeffs;  
  128.     }  
  129.   
  130.     public Script.FieldID getFieldID_gCoeffs() {  
  131.         return createFieldID(mExportVarIdx_gCoeffs, null);  
  132.     }  
  133.   
  134.     private final static int mExportForEachIdx_root = 0;  
  135.     public Script.KernelID getKernelID_root() {  
  136.         return createKernelID(mExportForEachIdx_root, 3nullnull);  
  137.     }  
  138.   
  139.     public void forEach_root(Allocation ain, Allocation aout) {  
  140.         // check ain  
  141.         if (!ain.getType().getElement().isCompatible(__U8_4)) {  
  142.             throw new RSRuntimeException("Type mismatch with U8_4!");  
  143.         }  
  144.         // check aout  
  145.         if (!aout.getType().getElement().isCompatible(__U8_4)) {  
  146.             throw new RSRuntimeException("Type mismatch with U8_4!");  
  147.         }  
  148.         // Verify dimensions  
  149.         Type tIn = ain.getType();  
  150.         Type tOut = aout.getType();  
  151.         if ((tIn.getCount() != tOut.getCount()) ||  
  152.             (tIn.getX() != tOut.getX()) ||  
  153.             (tIn.getY() != tOut.getY()) ||  
  154.             (tIn.getZ() != tOut.getZ()) ||  
  155.             (tIn.hasFaces() != tOut.hasFaces()) ||  
  156.             (tIn.hasMipmaps() != tOut.hasMipmaps())) {  
  157.             throw new RSRuntimeException("Dimension mismatch between input and output parameters!");  
  158.         }  
  159.         forEach(mExportForEachIdx_root, ain, aout, null);  
  160.     }  
  161.   
  162. }  


14、      在ImageFilterSharpen.java中,将ScriptC_convolve3x3 mScript改为MyScriptC_convolve3x3 mScript,并在相应地方同步更改类名。

经过上述步骤,Android4.2源码Gallery2Camera模块成功导入Eclipse,并能正常编译,稍后会将编译出的apk共享出来。

欢迎android爱好者加群248217350     备注:yanzi