获取个人应用代码并显示

时间:2016-12-25 23:38:03

标签: android

我正在尝试获取应用代码并显示它,例如,如果按钮X启动新活动,则textView显示整个方法

我只了解如何以this question

显示HTML格式的代码

但是有没有办法让我的应用程序的代码出来,我认为有两种方式

  1. 通过应用程序本身获取内部
  2. 外部通过阅读java文件然后过滤它并获取方法的文本
  3. 对此有任何想法吗?

    提前致谢

2 个答案:

答案 0 :(得分:3)

以上目前不可能如其他人所说的那样是评论。我可以建议使用资源文件夹中的源代码运送您的应用程序,并使用辅助函数在运行时从源中提取某些方法(您的第二个提议的方法)。我已经编写了示例代码,但它是纯java,需要移植到android(几行)。

注意:根据您的使用情况,您可能需要在提取后重新格式化代码。

希望有所帮助:)

辅助方法的代码:

static String getTheCode(String classname ,String methodSignature ) throws FileNotFoundException {

     //**********************A few lines of code below need changing when porting ***********//

    // open file, your will be in the assets folder not in the home dir of user, don't forget the .java extension when porting

    File file = new File(System.getProperty("user.home") +"/"+ classname +".java");

    // get the source, you can use FileInputReader or some reader supported by android

    Scanner scanner = new Scanner(file);

    String source = "";
    while(scanner.hasNext()) {
       source += " "+ scanner.next();
    }


    //**********************The above code needs changing when porting **********//

    // extract code using the method signature

    methodSignature = methodSignature.trim();
    source = source.trim();

    //appending { to differentiate from argument as it can be matched also if in the same file
    methodSignature = methodSignature+"{";

    //making sure we find what we are looking for
    methodSignature = methodSignature.replaceAll("\\s*[(]\\s*", "(");
    methodSignature = methodSignature.replaceAll("\\s*[)]\\s*", ")");
    methodSignature = methodSignature.replaceAll("\\s*[,]\\s*", ",");
    methodSignature = methodSignature.replaceAll("\\s+", " ");


    source =source.replaceAll("\\s*[(]\\s*", "(");
    source = source.replaceAll("\\s*[)]\\s*", ")");
    source = source.replaceAll("\\s*[,]\\s*", ",");
    source = source.replaceAll("\\s+", " ");


    if(!source.contains(methodSignature)) return null;

    // trimming all text b4 method signature
    source = source.substring(source.indexOf(methodSignature));

    //getting last index, a methods ends when there are matching pairs of these {}
    int lastIndex = 0;

    int rightBraceCount = 0;
    int leftBraceCount = 0;

    char [] remainingSource = source.toCharArray();
    for (int i = 0; i < remainingSource.length ; i++
         ) {

        if(remainingSource[i] == '}'){

            rightBraceCount++;

            if(rightBraceCount == leftBraceCount){

                lastIndex = (i + 1);
                break;
            }

        }else if(remainingSource[i] == '{'){

            leftBraceCount++;
        }




    }


    return  source.substring(0 ,lastIndex);

}

示例用法(getTheCode方法是静态的,并且在名为GetTheCode的类中):

public static void main(String... s) throws FileNotFoundException {



    System.out.println(GetTheCode.getTheCode("Main", "private static void shoutOut()"));
    System.out.println(GetTheCode.getTheCode("Main", "private static void shoutOut(String word)"));


}

输出:

private static void shoutOut(){ // nothing to here }
private static void shoutOut(String word){ // nothing to here }

注意:开始新活动时,请创建一个方法,例如

 private void myStartActivty(){

 Intent intent = new Intent(MyActivity.this, AnotherActivity.class);

    startActivity(intent);

}

然后在你的onClick:

@Override
public void onClick(View v) {
    myStartActivity();

    myTextView.setText(GetTheCode.getTheCode("MyActivity","private void myStartActivity()"));
}

更新:为Android安装了代码:

    import android.content.Context;

    import java.io.IOException;
    import java.util.Scanner;

    public class GetTheCode {


static String getTheCode(Context context, String classname , String methodSignature ) {

   Scanner scanner = null;
    String source = "";
    try {
        scanner = new Scanner(context.getAssets().open(classname+".java"));



    while(scanner.hasNext()) {
       source += " "+ scanner.next();
    }

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
        scanner.close();

    // extract code using the method signature

    methodSignature = methodSignature.trim();
    source = source.trim();

    //appending { to differentiate from argument as it can be matched also if in the same file
    methodSignature = methodSignature+"{";

    //making sure we find what we are looking for
    methodSignature = methodSignature.replaceAll("\\s*[(]\\s*", "(");
    methodSignature = methodSignature.replaceAll("\\s*[)]\\s*", ")");
    methodSignature = methodSignature.replaceAll("\\s*[,]\\s*", ",");
    methodSignature = methodSignature.replaceAll("\\s+", " ");


    source =source.replaceAll("\\s*[(]\\s*", "(");
    source = source.replaceAll("\\s*[)]\\s*", ")");
    source = source.replaceAll("\\s*[,]\\s*", ",");
    source = source.replaceAll("\\s+", " ");


    if(!source.contains(methodSignature)) return null;

    // trimming all text b4 method signature
    source = source.substring(source.indexOf(methodSignature));

    //getting last index, a methods ends when there are matching pairs of these {}
    int lastIndex = 0;

    int rightBraceCount = 0;
    int leftBraceCount = 0;

    char [] remainingSource = source.toCharArray();
    for (int i = 0; i < remainingSource.length ; i++
         ) {

        if(remainingSource[i] == '}'){

            rightBraceCount++;

            if(rightBraceCount == leftBraceCount){

                lastIndex = (i + 1);
                break;
            }

        }else if(remainingSource[i] == '{'){

            leftBraceCount++;
        }




    }


    return  source.substring(0,lastIndex);

   }

}

<强>用法:

   // the method now takes in context as the first parameter, the line below was in an Activity
  Log.d("tag",GetTheCode.getTheCode(this,"MapsActivity","protected void onCreate(Bundle savedInstanceState)"));

答案 1 :(得分:2)

让我们从更广泛的问题概述开始:

  1. 显示应用代码

  2. 按X按钮

  3. 使用显示方法

  4. 的textview打开新活动

    目标是执行以下操作:

    1. 通过提取app然后构建&amp;来查看app方法跑吧。
    2. 我们可以使用一些方法动态运行Java / Android代码。我亲自这样做的方式是DexClassLoader和Reflection。

      如果您需要更多详情,请与我们联系。这是它的目的:

      1. 查看应用方法
      2. 按下X后,启动意图并使用额外的新活动
      3. 动态解析和编译代码,然后使用DexClassLoader和Reflection
      4. 运行它

        来源:

        Sample file loading Java method from TerminalIDE Android App

        Android Library I made for Auto-Updating Android Applications without needing the Play Store on non-root devices